Esempio n. 1
0
        private static async void AnalyzeImage(string filePath)
        {
            string url;
            var    client = new VisionServiceClient(SubscriptionKey);

            using (Stream imageFileStream = File.OpenRead(filePath))
            {
                var resultHw = await client.CreateHandwritingRecognitionOperationAsync(imageFileStream);

                url = resultHw.Url;
            }

            var operation = new HandwritingRecognitionOperation {
                Url = url
            };
            var result = await client.GetHandwritingRecognitionOperationResultAsync(operation);

            foreach (var line in result.RecognitionResult.Lines)
            {
                foreach (var word in line.Words)
                {
                    Console.WriteLine(word.Text);
                }
            }
        }
        public void RecognizeHandwrittenTextAsync(MediaFile photo)
        {
            Task <HandwritingRecognitionOperation>       s;
            Task <HandwritingRecognitionOperationResult> f;
            HandwritingTextResult rs;
            var client = new VisionServiceClient("d641ed8db191419f86dbf38d68fbbc39", "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0");



            try
            {
                using (var photoStream = photo.GetStream())
                {
                    s = client.CreateHandwritingRecognitionOperationAsync(photoStream);

                    f  = client.GetHandwritingRecognitionOperationResultAsync(s.Result);
                    rs = f.Result.RecognitionResult;
                }

                foreach (var line in rs.Lines)
                {
                    foreach (var word in line.Words)
                    {
                        text = $"{text} {word.Text}";
                    }
                }
            }
            catch (System.Exception e)
            {
                ShowToast(e.Message + "\n" + e.ToString());
            }


            //return text;
        }
        private static async Task <string[]> TextExtractionCore(string fname, bool wrds, string method)
        {
            VisionServiceClient vsClient = new VisionServiceClient(API_Key, API_Location);

            string[] textres = null;

            if (System.IO.File.Exists(fname))
            {
                using (Stream stream = System.IO.File.OpenRead(fname))
                {
                    switch (method)
                    {
                    case "TextPic":
                        OcrResults ocr = await vsClient.RecognizeTextAsync(stream, "unk", false);

                        textres = GetExtracted(ocr, wrds);
                        break;

                    case "HandwrittenPic":
                        HandwritingRecognitionOperation hdro = await vsClient.CreateHandwritingRecognitionOperationAsync(stream);

                        HandwritingRecognitionOperationResult hresult = await vsClient.GetHandwritingRecognitionOperationResultAsync(hdro);

                        textres = GetHandWrittenExtracted(hresult, wrds);
                        break;
                    }
                }
            }
            return(textres);
        }
Esempio n. 4
0
        public async Task <ActionResult> Index()
        {
            if (Request.HttpMethod == "GET")
            {
                return(View());
            }

            var model = new HandwritingModel();
            HandwritingRecognitionOperation op = null;

            await RunOperationOnImage(async stream =>
            {
                op = await VisionServiceClient.CreateHandwritingRecognitionOperationAsync(stream);
            });

            while (true)
            {
                await Task.Delay(5000);

                var result = await VisionServiceClient.GetHandwritingRecognitionOperationResultAsync(op);

                if (result.Status == HandwritingRecognitionOperationStatus.NotStarted ||
                    result.Status == HandwritingRecognitionOperationStatus.Running)
                {
                    continue;
                }

                model.Result = result.RecognitionResult;
                break;
            }

            model.ImageDump = GetInlineImageWithLines(model.Result);

            return(View(model));
        }
Esempio n. 5
0
        public async Task<ActionResult> HandwritingRecognition()
        {
            var result = await HandwritingRecognitionTask(async (VisionServiceClient VisionServiceClient)
                => await VisionServiceClient.CreateHandwritingRecognitionOperationAsync(
                "https://www.nayuki.io/res/overwriting-confidential-handwritten-text/overwriting-handwriting.jpg"
                ));

            return View();
        }
Esempio n. 6
0
        public static async Task <string[]> HandwritingExtractionCore(string fname, bool wrds)
        {
            VisionServiceClient client = new VisionServiceClient(API_key, API_location);

            string[] textres = null;
            if (File.Exists(fname))
            {
                using (Stream stream = File.OpenRead(fname))
                {
                    HandwritingRecognitionOperation op = await client.CreateHandwritingRecognitionOperationAsync(stream);

                    HandwritingRecognitionOperationResult res = await client.GetHandwritingRecognitionOperationResultAsync(op);

                    textres = GetExtracted(res, wrds);
                }
            }
            return(textres);
        }
Esempio n. 7
0
        public async Task <string> OCRHandwriting(Stream photo)
        {
            try
            {
                var theFullReturn = new StringBuilder();

                HandwritingRecognitionOperationResult result;

                photo.Position = 0;

                var handwriteStart = await client.CreateHandwritingRecognitionOperationAsync(photo);

                result = await client.GetHandwritingRecognitionOperationResultAsync(handwriteStart);

                while (result.Status == HandwritingRecognitionOperationStatus.Running ||
                       result.Status == HandwritingRecognitionOperationStatus.NotStarted)
                {
                    await Task.Delay(500);

                    result = await client.GetHandwritingRecognitionOperationResultAsync(handwriteStart);
                }

                if (result.Status == HandwritingRecognitionOperationStatus.Succeeded)
                {
                    foreach (var line in result.RecognitionResult.Lines)
                    {
                        foreach (var word in line.Words)
                        {
                            theFullReturn.Append($"{word.Text} ");
                        }
                    }

                    return(theFullReturn.ToString());
                }

                return(could_not_analyze);
            }
            catch (Exception ex)
            {
                return(could_not_analyze);
            }
        }
Esempio n. 8
0
        public async Task <ActionResult> Index(HttpPostedFileBase file)
        {
            if (Request.HttpMethod == "GET")
            {
                return(View());
            }

            var model = new HandwritingModel();
            HandwritingRecognitionOperation op = null;

            await RunOperationOnImage(async stream =>
            {
                op = await VisionServiceClient.CreateHandwritingRecognitionOperationAsync(stream);
            });

            while (true)
            {
                await Task.Delay(5000);

                var result = await VisionServiceClient.GetHandwritingRecognitionOperationResultAsync(op);

                if (result.Status == HandwritingRecognitionOperationStatus.NotStarted ||
                    result.Status == HandwritingRecognitionOperationStatus.Running)
                {
                    continue;
                }

                model.Result = result.RecognitionResult;
                break;
            }

            model.ImageDump = GetInlineImageWithLines(model.Result);



            if (file != null && file.ContentLength > 0)
            {
                if (!file.ContentType.StartsWith("image"))
                {
                    TempData["Message"] = "Only image files may be uploaded";
                }
                else
                {
                    try
                    {
                        CloudStorageAccount account   = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
                        CloudBlobClient     client    = account.CreateCloudBlobClient();
                        CloudBlobContainer  container = client.GetContainerReference("pictures");
                        CloudBlockBlob      photo     = container.GetBlockBlobReference(Path.GetFileName(file.FileName));
                        await photo.UploadFromStreamAsync(file.InputStream);

                        using (var outputStream = new MemoryStream())
                        {
                            file.InputStream.Seek(0L, SeekOrigin.Begin);
                            var settings = new ResizeSettings {
                                MaxWidth = 192
                            };
                            ImageBuilder.Current.Build(file.InputStream, outputStream, settings);
                            outputStream.Seek(0L, SeekOrigin.Begin);
                            container = client.GetContainerReference("pictures");
                            CloudBlockBlob thumbnail = container.GetBlockBlobReference(Path.GetFileName(file.FileName));
                            await thumbnail.UploadFromStreamAsync(outputStream);
                        }
                    }
                    catch (Exception ex)
                    {
                        // In case something goes wrong
                        TempData["Message"] = ex.Message;
                    }
                }
            }



            return(View(model));
        }
 /// <summary>
 /// Sends a url to Project Oxford and performs Handwriting Recognition
 /// </summary>
 /// <param name="imageUrl">The url to perform recognition on</param>
 /// <returns></returns>
 private async Task <HandwritingRecognitionOperationResult> RecognizeUrl(string imageUrl)
 {
     return(await RecognizeAsync(async (VisionServiceClient VisionServiceClient) => await VisionServiceClient.CreateHandwritingRecognitionOperationAsync(imageUrl)));
 }
 /// <summary>
 /// Uploads the image to Project Oxford and performs Handwriting Recognition
 /// </summary>
 /// <param name="imageFilePath">The image file path.</param>
 /// <returns></returns>
 private async Task <HandwritingRecognitionOperationResult> UploadAndRecognizeImage(string imageFilePath)
 {
     using (Stream imageFileStream = File.OpenRead(imageFilePath))
     {
         return(await RecognizeAsync(async (VisionServiceClient VisionServiceClient) => await VisionServiceClient.CreateHandwritingRecognitionOperationAsync(imageFileStream)));
     }
 }