/// <summary> /// Распознать печатный текст /// </summary> /// <param name="localImagePath"></param> /// <returns></returns> public static async Task <string> ReadPrintedText(string localImagePath) { string detectedText; ImageAnnotatorClient client = await ImageAnnotatorClient.CreateAsync(); IReadOnlyList <EntityAnnotation> textAnnotations = await client.DetectTextAsync(await Google.Cloud.Vision.V1.Image.FromFileAsync(localImagePath)); detectedText = textAnnotations[0].Description; if (detectedText != null) { return(detectedText); } else { throw new TextDetectorException(); } }
public async Task <ResultFromOCRBindingModel> GetData() { Environment.SetEnvironmentVariable( "GOOGLE_APPLICATION_CREDENTIALS", CommonSecurityConstants.PathToGoogleCloudJson); ResultFromOCRBindingModel result = new ResultFromOCRBindingModel(); Regex snRegex = SerialNumberRegexes .GetSNRegex("LG"); Regex modelRegex = UnitModels .GetModelRegex( "LG", "TV"); ImageAnnotatorClient client = await ImageAnnotatorClient.CreateAsync(); Image image = await Image .FromFileAsync( @"E:\ALEKS\Images\pictures-diploma-project\1.jpg"); IReadOnlyList <EntityAnnotation> annotations = await client.DetectTextAsync(image); foreach (EntityAnnotation annotation in annotations) { if (snRegex.Match(annotation.Description) .Success) { result.ApplianceSerialNumber = annotation.Description; } else if (modelRegex.Match(annotation.Description) .Success) { result.ApplianceModel = annotation.Description; } } return(result); }
public static Task <IReadOnlyList <Google.Cloud.Vision.V1.EntityAnnotation> > TextDetectionAsync(byte[] img_byte) { var newTask = Task.Factory.StartNew(async() => { IReadOnlyList <Google.Cloud.Vision.V1.EntityAnnotation> response; Channel channel = new Channel( ImageAnnotatorClient.DefaultEndpoint.Host, ImageAnnotatorClient.DefaultEndpoint.Port, Credential.ToChannelCredentials()); ImageAnnotatorClient client = ImageAnnotatorClient.Create(channel); using (MemoryStream imageStream = new MemoryStream(img_byte)) { var image = Google.Cloud.Vision.V1.Image.FromStream(imageStream); response = await client.DetectTextAsync(image); } return(response); }); return(newTask.Result); }
public static async Task <string> MakeOCRRequest(string imageFilePath) { string credentialsPath = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS"); var image = Image.FromFile(imageFilePath); ImageAnnotatorClientBuilder builder = new ImageAnnotatorClientBuilder { CredentialsPath = credentialsPath }; try { ImageAnnotatorClient client = builder.Build(); var response = await client.DetectTextAsync(image); string contentString = response.ToString(); return(JToken.Parse(contentString).ToString()); } catch (Exception e) { Console.WriteLine("\n" + e.Message); return("error"); } }