如何使用 Google Cloud Vision API 确认图像(包含手写和打印文本)是不是包含手写文本?
Posted
技术标签:
【中文标题】如何使用 Google Cloud Vision API 确认图像(包含手写和打印文本)是不是包含手写文本?【英文标题】:How to confirm whether an image (containing both handwritten and printed texts) contains handwritten text using Google Cloud Vision API?如何使用 Google Cloud Vision API 确认图像(包含手写和打印文本)是否包含手写文本? 【发布时间】:2020-02-09 16:35:26 【问题描述】:我正在使用 Cloud Vision API - DOCUMENT_TEXT_DETECTION 功能从图像中检测手写文本。尽管它为我提取了手写数据,但是当涉及到同时具有打印文本和手写文本的图像时,它没有响应标识符,该标识符表示该位是手写的并且该位是打印的。直截了当地问,我想确认一张图片是否有手写文字。注意 - 图像可能仅包含手写文本或打印和手写文本的组合。
如果有人可以详细说明我需要传递给云视觉 api 以实现结果的所有其他属性,将不胜感激?或者有没有办法让 Cloud Vision API 标记出我的图像是否包含手写数据。
示例代码
public class Detect
public static void main(String args[])
String filePath = "C:\\Development_Avecto\\images.jpg";
try
detectDocumentText(filePath, System.out);
catch (IOException e)
e.printStackTrace();
catch (Exception e)
e.printStackTrace();
public static void detectDocumentText(String filePath, PrintStream out) throws Exception, IOException
List<AnnotateImageRequest> requests = new ArrayList<>();
ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_TEXT_DETECTION).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);
try (ImageAnnotatorClient client = ImageAnnotatorClient.create())
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
client.close();
for (AnnotateImageResponse res : responses)
if (res.hasError())
out.printf("Error: %s\n", res.getError().getMessage());
return;
TextAnnotation annotation = res.getFullTextAnnotation();
for (Page page : annotation.getPagesList())
String pageText = "";
for (Block block : page.getBlocksList())
String blockText = "";
for (Paragraph para : block.getParagraphsList())
String paraText = "";
for (Word word : para.getWordsList())
String wordText = "";
for (Symbol symbol : word.getSymbolsList())
wordText = wordText + symbol.getText();
out.format("Symbol text: %s (confidence: %f)\n",
symbol.getText(),symbol.getConfidence());
out.format("Word text: %s (confidence: %f)\n\n",wordText, word.getConfidence());
paraText = String.format("%s %s", paraText,wordText);
// Output Example using Paragraph:
out.println("\nParagraph: \n" + paraText);
out.format("Paragraph Confidence: %f\n",para.getConfidence());
blockText = blockText + paraText;
pageText = pageText + blockText;
out.println("\nComplete annotation:");
out.println(annotation.getText());
图片 -
【问题讨论】:
【参考方案1】:这可能会有所帮助,这是工作示例。
public async Task<string> GetText(string imgPath,string language,string type)
TextResult = JsonResult = "";
var credential = CreateCredential();
var service = CreateService(credential);
service.HttpClient.Timeout = new TimeSpan(1,1,1);
byte[] file = File.ReadAllBytes(imgPath);
BatchAnnotateImagesRequest batchRequest = new BatchAnnotateImagesRequest();
batchRequest.Requests= new List<AnnotateImageRequest>();
batchRequest.Requests.Add(new AnnotateImageRequest()
Features = new List<Feature>() new Feature() Type = type, MaxResults =
1 , ,
ImageContext = new ImageContext() LanguageHints = new List<string>()
language ,
Image = new Image() Content = Convert.ToBase64String(file)
);
var annotate = service.Images.Annotate(batchRequest);
BatchAnnotateImagesResponse batchAnnotateImagesResponse = annotate.Execute();
if (batchAnnotateImagesResponse.Responses.Any())
AnnotateImageResponse annotateImageResponse =
batchAnnotateImagesResponse.Responses[0];
if (annotateImageResponse.Error != null)
if (annotateImageResponse.Error.Message != null)
Error = annotateImageResponse.Error.Message;
else
switch (type)
case "TEXT_DETECTION":
if (annotateImageResponse.TextAnnotations != null &&
annotateImageResponse.TextAnnotations.Any())
TextResult =
annotateImageResponse.TextAnnotations[0].Description.Replace("\n", "\r\n");
break;
case "DOCUMENT_TEXT_DETECTION":
if (annotateImageResponse.TextAnnotations != null &&
annotateImageResponse.TextAnnotations.Any())
TextResult =
annotateImageResponse.TextAnnotations[0].Description.Replace("\n", "\r\n");
break;
case "FACE_DETECTION":
if (annotateImageResponse.FaceAnnotations != null &&
annotateImageResponse.FaceAnnotations.Any())
TextResult =
JsonConvert.SerializeObject(annotateImageResponse.FaceAnnotations[0]);
break;
case "LOGO_DETECTION":
if (annotateImageResponse.LogoAnnotations != null &&
annotateImageResponse.LogoAnnotations.Any())
TextResult =
JsonConvert.SerializeObject(annotateImageResponse.LogoAnnotations[0]);
break;
case "LABEL_DETECTION":
if (annotateImageResponse.LabelAnnotations != null &&
annotateImageResponse.LabelAnnotations.Any())
TextResult =
JsonConvert.SerializeObject(annotateImageResponse.LabelAnnotations[0]);
break;
case "LANDMARK_DETECTION":
if (annotateImageResponse.LandmarkAnnotations != null &&
annotateImageResponse.LandmarkAnnotations.Any())
TextResult =
JsonConvert.SerializeObject(annotateImageResponse.LandmarkAnnotations[0]);
break;
case "SAFE_SEARCH_DETECTION":
if (annotateImageResponse.SafeSearchAnnotation != null)
TextResult =
JsonConvert.SerializeObject(annotateImageResponse.SafeSearchAnnotation);
break;
case "IMAGE_PROPERTIES":
if (annotateImageResponse.ImagePropertiesAnnotation != null)
TextResult =
JsonConvert.SerializeObject(annotateImageResponse.ImagePropertiesAnnotation);
break;
return TextResult;
【讨论】:
以上是关于如何使用 Google Cloud Vision API 确认图像(包含手写和打印文本)是不是包含手写文本?的主要内容,如果未能解决你的问题,请参考以下文章
Google Cloud Vision - 如何使用 Node.js 发送请求属性
如何使用 Google Cloud Vision API 读取一列文本
如何修复 Google Cloud Vision 的分段错误?
如何使用 Google Cloud Vision API 确认图像(包含手写和打印文本)是不是包含手写文本?