Ejemplo n.º 1
0
        /*
         * END - BATCH READ FILE - LOCAL IMAGE
         */

        /*
         * RECOGNIZE PRINTED TEXT - URL IMAGE
         */
        public static async Task RecognizePrintedTextUrl(ComputerVisionClient client, string imageUrl)
        {
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("RECOGNIZE PRINTED TEXT - URL IMAGE");
            Console.WriteLine();
            Console.WriteLine($"Performing OCR on URL image {Path.GetFileName(imageUrl)}...");
            Console.WriteLine();

            // Perform OCR on image
            OcrResult remoteOcrResult = await client.RecognizePrintedTextAsync(true, imageUrl);

            // Print the recognized text
            Console.WriteLine("Text:");
            Console.WriteLine("Language: " + remoteOcrResult.Language);
            Console.WriteLine("Text Angle: " + remoteOcrResult.TextAngle);
            Console.WriteLine("Orientation: " + remoteOcrResult.Orientation);
            Console.WriteLine();
            Console.WriteLine("Text regions: ");
            foreach (var remoteRegion in remoteOcrResult.Regions)
            {
                Console.WriteLine("Region bounding box: " + remoteRegion.BoundingBox);
                foreach (var line in remoteRegion.Lines)
                {
                    Console.WriteLine("Line bounding box: " + line.BoundingBox);

                    foreach (var word in line.Words)
                    {
                        Console.WriteLine("Word bounding box: " + word.BoundingBox);
                        Console.WriteLine("Text: " + word.Text);
                    }
                    Console.WriteLine();
                }
            }
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] dynamic req,
            ILogger log)
        {
            log.LogInformation("ExtractImageText trigger function processed a request.");

            client = new ComputerVisionClient(new Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ApiKeyServiceClientCredentials(subscriptionKey))
            {
                Endpoint = endpoint
            };

            string url = req.url;

            var response = await client.RecognizePrintedTextAsync(true, url, Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models.OcrLanguages.De);

            string result = string.Empty;

            foreach (var region in response.Regions)
            {
                foreach (var line in region.Lines)
                {
                    foreach (var word in line.Words)
                    {
                        result += word.Text + " ";
                    }
                }
            }

            return(new OkObjectResult(result));
        }
Ejemplo n.º 3
0
        // Retuns array of words.
        public static async Task <string[]> RecognizePrintedTextUrl(ComputerVisionClient client, string imageUrl)
        {
            // Perform OCR on image
            OcrResult remoteOcrResult = null;

            try
            {
                remoteOcrResult = await client.RecognizePrintedTextAsync(true, imageUrl);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }

            List <string> result = new List <string>();

            foreach (var remoteRegion in remoteOcrResult.Regions)
            {
                foreach (var line in remoteRegion.Lines)
                {
                    result.Add(string.Join(' ', line.Words.Select(w => w.Text)));
                }
            }

            return(result.ToArray());
        }
        /// <summary>
        /// Sends a URL to Cognitive Services and performs OCR.
        /// </summary>
        /// <param name="imageUrl">The image URL for which to perform recognition.</param>
        /// <param name="language">The language code to recognize.</param>
        /// <returns>Awaitable OCR result.</returns>
        private async Task <OcrResult> RecognizeUrlAsync(string imageUrl, OcrLanguages language)
        {
            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE STARTS HERE
            // -----------------------------------------------------------------------

            //
            // Create Cognitive Services Vision API Service client.
            //
            using (var client = new ComputerVisionClient(Credentials)
            {
                Endpoint = Endpoint
            })
            {
                Log("ComputerVisionClient is created");

                //
                // Perform OCR on the given URL.
                //
                Log("Calling ComputerVisionClient.RecognizePrintedTextAsync()...");
                OcrResult ocrResult = await client.RecognizePrintedTextAsync(!DetectOrientation, imageUrl, language);

                return(ocrResult);
            }

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
Ejemplo n.º 5
0
        public async Task <OcrResult> OcrUrl(string imageUrl)
        {
            ComputerVisionClient client = await GetClient();

            OcrResult remoteOcrResult = await client.RecognizePrintedTextAsync(true, imageUrl);

            return(remoteOcrResult);
        }
        /*
         * READ FILE - URL
         * Extracts text.
         */
        public static async Task <OcrResult> RecognizeTextFromImageUrl(ComputerVisionClient client, string urlFile, bool detectOrientation, OcrLanguages language = OcrLanguages.En)
        {
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("READ FILE FROM URL");
            Console.WriteLine();

            // Read text from URL
            var ocrResults = await client.RecognizePrintedTextAsync(detectOrientation, urlFile, language);

            return(ocrResults);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Sends a url to Project Oxford and performs OCR
        /// </summary>
        /// <param name="imageUrl">The url to perform recognition on</param>
        /// <param name="language">The language code to recognize for</param>
        /// <returns></returns>
        private async Task <OcrResult> RecognizeUrl(string imageUrl, OcrLanguages language)
        {
            Log("Calling VisionServiceClient.RecognizeTextAsync()...");
            var ocrResult = await VisionServiceClient.RecognizePrintedTextAsync(true, imageUrl, language);

            return(ocrResult);

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
Ejemplo n.º 8
0
        // Analyze a remote image
        private static async Task OCRFromUrlAsync(ComputerVisionClient computerVision, string imageUrl)
        {
            if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
            {
                Console.WriteLine("\nInvalid remote image url:\n{0} \n", imageUrl);
                return;
            }

            OcrResult analysis = await computerVision.RecognizePrintedTextAsync(true, imageUrl);

            Console.WriteLine(imageUrl);
            DisplayResults(analysis);
        }
Ejemplo n.º 9
0
        // Retuns array of words.
        public static async Task <string[]> RecognizePrintedTextUrl(ComputerVisionClient client, string imageUrl)
        {
            // Perform OCR on image
            OcrResult remoteOcrResult = await client.RecognizePrintedTextAsync(true, imageUrl);

            string[] result = null;

            foreach (var remoteRegion in remoteOcrResult.Regions)
            {
                foreach (var line in remoteRegion.Lines)
                {
                    result = line.Words.Select(w => w.Text).ToArray();
                }
            }

            return(result);
        }
Ejemplo n.º 10
0
        public async Task <ConvertResponse> ExtractTextComputerVision(ConvertResponse response)
        {
            try
            {
                var key      = _config["computerVision:key"];
                var endpoint = _config["computerVision:endpoint"];
                ComputerVisionClient computerVision = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
                {
                    Endpoint = endpoint
                };

                var analysis = await computerVision.RecognizePrintedTextAsync(true, response.Request.UploadBlobUrl + _config["storage:sas"]);

                var text = new StringBuilder();
                foreach (var region in analysis.Regions)
                {
                    foreach (var line in region.Lines)
                    {
                        foreach (var word in line.Words)
                        {
                            text.Append(word.Text + " ");
                        }
                        text.AppendLine();
                    }
                }

                var transcriptBlobName = Path.GetFileNameWithoutExtension(response.Request.BlobName) + ".txt";

                var blobClient = new BlobStorageClient(_config);
                var textBlob   = blobClient.GetBlobBlock("transcripts", transcriptBlobName);

                response.TranscriptBlobUrl = textBlob.Uri.AbsoluteUri;
                response.Transcript        = text.ToString().Trim();

                await textBlob.UploadTextAsync(response.Transcript);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.WriteLine(ex.Message);
                response.ErrorMessage = ex.ToString();
            }

            return(response);
        }
        /// <summary>
        /// URL先の画像からテキストをOCRして抽出します。
        /// </summary>
        /// <returns>OCRした結果の各地域のテキストを返します。</returns>
        /// <param name="imageUrl">Image URL.</param>
        public async Task <List <string> > ExtractRemoteTextAsync(string imageUrl)
        {
            if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
            {
                return new List <string> {
                           $"Invalid image URL: {imageUrl}"
                }
            }
            ;

            // ComputerVisionClient の準備
            var computerVisionClient = new ComputerVisionClient(new ApiKeyServiceClientCredentials(Secrets.ComputerVisionApiKey),
                                                                new System.Net.Http.DelegatingHandler[] { })
            {
                Endpoint = Secrets.ComputerVisionEndpoint
            };

            try
            {
                // API 呼び出し、結果取得
                var ocrResult = await computerVisionClient.RecognizePrintedTextAsync(true, imageUrl, OcrLanguages.Ja);

                return(GetRegionTextAsync(ocrResult));
            }
            catch (ComputerVisionErrorException e)
            {
                Debug.WriteLine($"imageUrl: {e.Message}");
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Unknown error: {e.Message}");
            }

            return(new List <string> {
                "Could not recognize text."
            });
        }
 private async Task <OcrResult> ComputerVisionRecognizedPrintedTextByUrlAsync(string imageUrl, OcrLanguages ocrLanguage)
 {
     return(await _computerVisionClient.RecognizePrintedTextAsync(true, imageUrl, ocrLanguage));
 }
Ejemplo n.º 13
0
        public static async Task AnalyzeImageUrl(ComputerVisionClient client, string imageUrl)
        {
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("ANALYZE IMAGE - URL");
            Console.WriteLine();

            // Creating a list that defines the features to be extracted from the image.
            List <VisualFeatureTypes> features = new List <VisualFeatureTypes>()
            {
                VisualFeatureTypes.Categories,
                VisualFeatureTypes.Description,
                VisualFeatureTypes.Tags
            };

            Console.WriteLine($"Analyzing the image {Path.GetFileName(ANALYZE_URL_IMAGE)}...");
            Console.WriteLine();
            // Analyze the URL image
            ImageAnalysis results = await client.AnalyzeImageAsync(ANALYZE_URL_IMAGE, features, language : "en");

            // Sunmarizes the image content.
            Console.WriteLine("Summary:");
            foreach (var caption in results.Description.Captions)
            {
                Console.WriteLine($"{caption.Text} with confidence {caption.Confidence}");
            }
            Console.WriteLine();

            // Display categories the image is divided into.
            Console.WriteLine("Categories:");
            foreach (var category in results.Categories)
            {
                Console.WriteLine($"{category.Name} with confidence {category.Score}");
            }
            Console.WriteLine();

            // Image tags and their confidence score
            Console.WriteLine("Tags:");
            foreach (var tag in results.Tags)
            {
                Console.WriteLine($"{tag.Name} {tag.Confidence}");
            }
            Console.WriteLine();

            // // Objects
            // Console.WriteLine("Objects:");
            // foreach (var obj in results.Objects)
            // {
            //     Console.WriteLine($"{obj.ObjectProperty} with confidence {obj.Confidence} at location {obj.Rectangle.X}, " +
            //     $"{obj.Rectangle.X + obj.Rectangle.W}, {obj.Rectangle.Y}, {obj.Rectangle.Y + obj.Rectangle.H}");
            // }
            // Console.WriteLine();
            //
            // // Well-known (or custom, if set) brands.
            // Console.WriteLine("Brands:");
            // foreach (var brand in results.Brands)
            // {
            //     Console.WriteLine($"Logo of {brand.Name} with confidence {brand.Confidence} at location {brand.Rectangle.X}, " +
            //     $"{brand.Rectangle.X + brand.Rectangle.W}, {brand.Rectangle.Y}, {brand.Rectangle.Y + brand.Rectangle.H}");
            // }
            // Console.WriteLine();
            //
            // // Adult or racy content, if any.
            // Console.WriteLine("Adult:");
            // Console.WriteLine($"Has adult content: {results.Adult.IsAdultContent} with confidence {results.Adult.AdultScore}");
            // Console.WriteLine($"Has racy content: {results.Adult.IsRacyContent} with confidence {results.Adult.RacyScore}");
            // Console.WriteLine();
            //
            // // Identifies the color scheme.
            // Console.WriteLine("Color Scheme:");
            // Console.WriteLine("Is black and white?: " + results.Color.IsBWImg);
            // Console.WriteLine("Accent color: " + results.Color.AccentColor);
            // Console.WriteLine("Dominant background color: " + results.Color.DominantColorBackground);
            // Console.WriteLine("Dominant foreground color: " + results.Color.DominantColorForeground);
            // Console.WriteLine("Dominant colors: " + string.Join(",", results.Color.DominantColors));
            // Console.WriteLine();
            //
            // // Celebrities in image, if any.
            // Console.WriteLine("Celebrities:");
            // foreach (var category in results.Categories)
            // {
            //     if (category.Detail?.Celebrities != null)
            //     {
            //         foreach (var celeb in category.Detail.Celebrities)
            //         {
            //             Console.WriteLine($"{celeb.Name} with confidence {celeb.Confidence} at location {celeb.FaceRectangle.Left}, " +
            //             $"{celeb.FaceRectangle.Top}, {celeb.FaceRectangle.Height}, {celeb.FaceRectangle.Width}");
            //         }
            //     }
            // }
            // Console.WriteLine();
            //
            // // Popular landmarks in image, if any.
            // Console.WriteLine("Landmarks:");
            // foreach (var category in results.Categories)
            // {
            //     if (category.Detail?.Landmarks != null)
            //     {
            //         foreach (var landmark in category.Detail.Landmarks)
            //         {
            //             Console.WriteLine($"{landmark.Name} with confidence {landmark.Confidence}");
            //         }
            //     }
            // }
            // Console.WriteLine();
            //
            // // Detects the image types.
            // Console.WriteLine("Image Type:");
            // Console.WriteLine("Clip Art Type: " + results.ImageType.ClipArtType);
            // Console.WriteLine("Line Drawing Type: " + results.ImageType.LineDrawingType);
            // Console.WriteLine();
            //
            // // Read the batch text from an image (handwriting and/or printed).
            // BatchReadFileUrl(client, EXTRACT_TEXT_URL_IMAGE).Wait();
            // BatchReadFileLocal(client, EXTRACT_TEXT_LOCAL_IMAGE).Wait();
            var ocrResults = await client.RecognizePrintedTextAsync(true, ANALYZE_URL_IMAGE, OcrLanguages.Ja);

            foreach (var region in ocrResults.Regions)
            {
                foreach (var line in region.Lines)
                {
                    Console.WriteLine($"Characters in {line}:");
                    foreach (var word in line.Words)
                    {
                        Console.WriteLine(word.Text);
                    }
                    Console.WriteLine();
                }
            }

            Console.ReadLine();
        }
Ejemplo n.º 14
0
        public static async void Run([BlobTrigger("uploads/{name}", Connection = "StorageAccountConnection")]Stream myBlob, string name, ILogger log)
        {
            // Process the image using Cognitive Services Computer Vision!
            string subscriptionKey = "[Cognitive Vision API Key]";
            string endpoint = "[Cognitive Vision Endpoint]";

            bool blnSuccess = false;


            // Create a client
            ComputerVisionClient client = Authenticate(endpoint, subscriptionKey);
            try
            {
                OcrResult result = await client.RecognizePrintedTextAsync(true, "[Azure Storage Account Root URL]" + name, OcrLanguages.En);

                if (result != null)
                {
                    // Get the image text
                    // THIS WILL BE REPLACED BY 3rd PARTY OCR
                    StringBuilder sb = new StringBuilder();
                    foreach (OcrRegion region in result.Regions)
                    {
                        foreach (OcrLine line in region.Lines)
                        {
                            foreach (OcrWord word in line.Words)
                            {
                                sb.Append(word.Text + " ");
                            };
                        };
                    };

                    // Mkae sure the plate was recognized.
                    if (sb.ToString() != "")
                    {

                        var arr1 = new string[] { "Illinois Ave", "Marvin Gardens", "Park Place", "Baltic Ave" };
                        Random random = new Random();

                        /// Create a new CosmosDB document 
                        LicenseRecord item = new LicenseRecord()
                        {
                            Id = Guid.NewGuid().ToString(),
                            PlateNumber = sb.ToString(), // Extracted from OCR
                            Location = arr1[random.Next(arr1.Length)], // Extracted from OCR
                            PhotoURL = "[Azure Storage Account Root URL] + name, // Uploaded Photo
                            DateCaptured = DateTime.Now, // Extracted from OCR / Upload time
                            IsProcessed = false
                        };

                        using (CosmosClient cosmosdbclient = new CosmosClient("AccountEndpoint=[Azure CosmosDB endpoint];AccountKey=[Azure CosmosDB account key]"))
                        {
                            Container container = cosmosdbclient.GetContainer("[Azure CosmosDB database name]", "[Azure CosmosDB container name]");
                            if (container != null)
                            {
                                ItemResponse<LecnseRecord> response = await container.CreateItemAsync(item, new PartitionKey(item.Id));
                                blnSuccess = true;
                            }
                        }
                    }


                    // Move the original blob
                    // If successful, move to Processed container
                    // If failure, move to Unprocessed container
                    var cred = new StorageCredentials("[Azure Stoage Account name]", "[Azure Storage account key]");
                    var account = new CloudStorageAccount(cred, true);
                    var storageclient = account.CreateCloudBlobClient();
                    var sourceContainer = storageclient.GetContainerReference("uploads");
                    var sourceBlob = sourceContainer.GetBlockBlobReference(name);
                    string strDestinationContainer = "processed";
                    if (!blnSuccess)
                    {
                        strDestinationContainer = "unprocessed";
                    }
                    var destinationContainer = storageclient.GetContainerReference(strDestinationContainer);
                    var destinationBlob = destinationContainer.GetBlockBlobReference(name);
                    await destinationBlob.StartCopyAsync(sourceBlob);
                    await sourceBlob.DeleteIfExistsAsync();
                };

            }
            catch (Exception ex)
            {
                log.LogInformation(ex.Message);
            };
        }
Ejemplo n.º 15
0
        private async Task MakeRequest(Plugin.Media.Abstractions.MediaFile myFile)
        {
            IsLoading = true;
            ApiKeyServiceClientCredentials mykey = new ApiKeyServiceClientCredentials(SubscriptionKey);
            var visionApiClient = new ComputerVisionClient(mykey);
            //visionApiClient.AzureRegion = AzureRegions.Northeurope;
            var recognizedResult = await visionApiClient.RecognizePrintedTextAsync(false, "https://b.zmtcdn.com/data/menus/804/17883804/ccb6c1fbcc945d2824e9f5508cda9098.jpg");

            //var recognizedResult = await visionApiClient.RecognizePrintedTextInStreamAsync(false, myFile.GetStream);


            List <Model.MenuItem> menuItems = new List <Model.MenuItem>();

            foreach (var region in recognizedResult.Regions)
            {
                foreach (var line in region.Lines)
                {
                    string item1 = "";
                    foreach (var word in line.Words)
                    {
                        item1 += word;
                    }
                    menuItems.Add(new Model.MenuItem()
                    {
                        Title = item1
                    });
                }
            }
            IsLoading = false;
            await App.NavigationService.PushAsync(new MenuListingPage());

            /*var client = new HttpClient();
             * var queryString =  HttpUtility.ParseQueryString(string.Empty);
             *
             * // Request headers
             * client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", SubscriptionKey);
             *
             * // Request parameters
             * string queryparam = "mode=printed";
             * var uri = "https://northeurope.api.cognitive.microsoft.com/vision/v1.0/OCR?" + queryparam;
             *
             * HttpResponseMessage response;*/
            /*   //to use for sending files
             * using (var memoryStream = new System.IO.MemoryStream())
             * {
             *  myFile.GetStream().CopyTo(memoryStream);
             *  myFile.Dispose();
             *  using (var content = new ByteArrayContent(memoryStream.ToArray()))
             *  {
             *      content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
             *      response = await client.PostAsync(ComputerVisionApiURL, content);
             *  }
             * }
             *//*
             * string urlbody = "{'url':'https://i.pinimg.com/originals/bc/f4/c2/bcf4c20c441c9b67ac43ff399992adf7.jpg'}";
             * using (var content = new StringContent(urlbody))
             * {
             *  content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
             *  response = await client.PostAsync(ComputerVisionApiURL, content);
             *  if (response.StatusCode == System.Net.HttpStatusCode.Accepted)
             *  {
             *      var test = new List<string>(response.Headers.GetValues("Operation-Location"));
             *      var test2 = test[0];
             *      response = await client.PostAsync(test2, new StringContent(""));
             *      string contentString = await response.Content.ReadAsStringAsync();
             *  }
             *  else
             *  { string contentString = await response.Content.ReadAsStringAsync(); }
             * }*/
        }