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 ResultFromOCRBindingModel GetData( ResultFromOCRBindingModel model, IMatFileUploadEntry file) { ResultFromOCRBindingModel result = new ResultFromOCRBindingModel(); Regex snRegex = SerialNumberRegexes .GetSNRegex(model.ApplianceBrand); Regex modelRegex = LGModels .GetModelRegex(model.ApplianceType); ImageAnnotatorClient client = ImageAnnotatorClient.Create(); MemoryStream stream = new MemoryStream(); file.WriteToStreamAsync(stream); Google.Cloud.Vision.V1.Image image = Google.Cloud.Vision.V1.Image.FromStream(stream); var annotations = client.DetectText(image); foreach (var 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); }