Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // Google Places Search
            // Funtion 1 - FindPlacesUsingTextQuery - Tested
            String API_KEY = Console.ReadLine();

            GC.Places.PlacesSearch placesSearch = new GC.Places.PlacesSearch();
            Task <List <GC.Places.FindPlaceCandidates> > candidates = placesSearch.FindPlacesUsingTextQuery(API_KEY,
                                                                                                            "720 Northwestern Ave");

            candidates.Wait();

            // Function 2 - GetNearbySearchResultsRankByProminence - Tested
            GC.Places.Location location = new GC.Places.Location(-33.8670522, 151.1957362);
            Task <List <GC.Places.NearbySearchResult> > results = placesSearch.GetNearbySearchResultsRankByProminence(API_KEY, location, 50);

            results.Wait();

            // Natural Language Intelligence
            // Function 3 - AnalyzeEntitySentiment - Tessted
            NL.NaturalLanguageIntelligence nli = new NL.NaturalLanguageIntelligence();
            NL.Document document = new NL.Document((NL.DocumentType.PLAIN_TEXT), "en", "gs://\"gccl_dd_01/Case Response - Masterpiece Cakeshop.pdf\"");
            Task <NL.AnalyzeEntitiesResponse> response = nli.AnalyzeEntitySentiment(API_KEY, document, NL.EncodingType.UTF8);

            response.Wait();

            // Video Intelligence
            // Function 4 - AnnotateVideoWithLabelDetection - Tested
            VI.VideoIntelligence videoIntelligence             = new VI.VideoIntelligence();
            Task <VI.VideoAnnotationResponse> annotateResponse =
                videoIntelligence.AnnotateVideoWithLabelDetection(API_KEY, "gs://gccl_dd_01/Video1", "", null);

            annotateResponse.Wait();

            // Image Intelligence
            // Function 5 - AnnotateImage - Tested
            II.ImageIntelligence imageIntelligence = new II.ImageIntelligence();

            II.ImageSource imageSource = new II.ImageSource("gs://gccl_dd_01/Image1");
            II.Image       image       = new II.Image(source: imageSource);

            List <II.ImageFeatures> imageFeatures = new List <II.ImageFeatures>();

            II.ImageFeatures faceDetection = new II.ImageFeatures(II.ImageType.FACE_DETECTION, 10, "builtin/stable");
            imageFeatures.Add(faceDetection);
            II.ImageFeatures landmarkDetection = new II.ImageFeatures(II.ImageType.LANDMARK_DETECTION, 10, "builtin/stable");
            imageFeatures.Add(landmarkDetection);
            II.ImageFeatures imageProps = new II.ImageFeatures(II.ImageType.IMAGE_PROPERTIES, 10, "builtin/stable");
            imageFeatures.Add(imageProps);

            II.AnnotateImageRequests        request      = new II.AnnotateImageRequests(image, imageFeatures, null);
            List <II.AnnotateImageRequests> requestsList = new List <II.AnnotateImageRequests>();

            requestsList.Add(request);
            II.AnnotateImageRequestList imageRequestList = new II.AnnotateImageRequestList(requestsList);

            String json = JsonConvert.SerializeObject(imageRequestList);

            Console.WriteLine(json);

            Task <List <II.AnnotateImageResponse> > responses = imageIntelligence.AnnotateImage(API_KEY, imageRequestList);

            responses.Wait();

            //String file = "C:\\Users\\Dhairya\\Desktop\\json_out_NL.txt";
            //String json = System.IO.File.ReadAllText(file);
            //Console.WriteLine(json);

            //II.AnnotateImageResponseList list = JsonConvert.DeserializeObject<II.AnnotateImageResponseList>(json);
            return;
        }
Ejemplo n.º 2
0
        /*
         * Method: AnnotateImages
         *
         * Description: This method can be used to query the Google Cloud Image Intelligence API with a batch of
         *   images and run image detection and annotation on these images.
         *
         * Parameters:
         *  - imageRequests (AnnotateImageRequestList): List of all the individual AnnotateImageRequests that will
         *      be sent to the Image Intelligence API.
         *
         *  - APIKey (String): Implicity required paramter which should be set through the constructor when
         *      creating an object of this class. For more details about the Google API Key please see:
         *      https://developers.google.com/places/web-service/get-api-key
         *
         *
         * Return: If the query is successful, then the method will return a tuple of two items. The first is an
         *   AnnotateImageResponseList object with image identification and annotation corresponding to each image
         *   request from the request list. The second element is a ResponseStatus object indicating the status of
         *   the query along with the appropiate HTTP code. Since the HTTP query and the indentification are both
         *   performed asynchronously, the return object is wrapped in Task<>.
         */
        public async Task <Tuple <AnnotateImageResponseList, ResponseStatus> > AnnotateImages(AnnotateImageRequestList imageRequests, String input = "")
        {
            if (BasicFunctions.isEmpty(APIKey))
            {
                return(new Tuple <AnnotateImageResponseList, ResponseStatus>(null, ImageAnnotationStatus.MISSING_API_KEY));
            }
            if (imageRequests == null || imageRequests.Requests.Count == 0)
            {
                return(new Tuple <AnnotateImageResponseList, ResponseStatus>(null, ImageAnnotationStatus.MISSING_REQUEST_LIST));
            }

            // Preparing the header to accept the JSON request body
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // The API address to which we will make the HTTP POST query
            String request_query         = "v1/images:annotate?" + $"key={APIKey}";
            HttpResponseMessage response = await httpClient.PostAsJsonAsync(request_query, imageRequests);

            Stream stream = await response.Content.ReadAsStreamAsync();

            StreamReader streamReader = new StreamReader(stream);
            String       response_str = streamReader.ReadToEnd();

            /*
             * Similar two-step hop as we have seen before. We try to deserialize the response string, expecting
             * an object of AnnotateImageResponseList If the response is not an AnnotateImageResponseList object,
             * then we will encounter a JSONSerialization error and return null. If it is as we expect, then we
             * just return the AnnotateImageResponseList object.
             */
            if (response.IsSuccessStatusCode)
            {
                AnnotateImageResponseList imageResponseList;

                try {
                    imageResponseList = JsonConvert.DeserializeObject <AnnotateImageResponseList>(response_str);

                    if (imageResponseList == null || imageResponseList.Responses.Count == 0)
                    {
                        return(new Tuple <AnnotateImageResponseList, ResponseStatus>(null, ImageAnnotationStatus.ZERO_RESULTS));
                    }
                } catch (JsonSerializationException e) {
                    Debug.WriteLine(e.StackTrace);
                    return(new Tuple <AnnotateImageResponseList, ResponseStatus>(null, ImageAnnotationStatus.DESERIALIZATION_ERROR));
                }

                return(new Tuple <AnnotateImageResponseList, ResponseStatus>(imageResponseList, ImageAnnotationStatus.OK));
            }
            else
            {
                // If the query is not successful, then we try to extract details about the error from the response
                AnnotateImageResponseList annotateResponse;

                try {
                    annotateResponse = JsonConvert.DeserializeObject <AnnotateImageResponseList>(response_str);

                    if (annotateResponse == null)
                    {
                        return(new Tuple <AnnotateImageResponseList, ResponseStatus>(null, ImageAnnotationStatus.INTERNAL_SERVER_ERROR));
                    }
                    if (annotateResponse.Error == null)
                    {
                        return(new Tuple <AnnotateImageResponseList, ResponseStatus>(null, ImageAnnotationStatus.ProcessErrorMessage(response.StatusCode.ToString(), response.ReasonPhrase)));
                    }
                } catch (JsonSerializationException e) {
                    Debug.WriteLine(e.StackTrace);
                    return(new Tuple <AnnotateImageResponseList, ResponseStatus>(null, ImageAnnotationStatus.DESERIALIZATION_ERROR));
                }

                ResponseStatus status = ImageAnnotationStatus.ProcessErrorMessage(annotateResponse.Error.Code.ToString(), annotateResponse.Error.Message);
                return(new Tuple <AnnotateImageResponseList, ResponseStatus>(null, status));
            }
        }