首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何使用Google Cloud Vision API确认图像(包含手写和打印文本)是否包含手写文本?

如何使用Google Cloud Vision API确认图像(包含手写和打印文本)是否包含手写文本?
EN

Stack Overflow用户
提问于 2019-10-12 10:40:45
回答 1查看 358关注 0票数 0

我正在使用Cloud Vision API - DOCUMENT_TEXT_DETECTION功能从图像中检测手写文本。虽然它为我提取了手写数据,但当涉及到同时包含打印文本和手写文本的图像时,它不会使用标识符进行响应,该标识符表示此位是手写的,而此位是打印的。直截了当地说,问题是我想确认图像是否有手写文本。注意-图像可以只包含手写文本,也可以包含打印文本和手写文本的组合。

如果有人能详细说明我需要传递给cloud vision api的所有其他属性以实现此结果,将不胜感激?或者,有没有办法让Cloud Vision API标记出我的图像是否包含手写数据。

示例代码

代码语言:javascript
运行
AI代码解释
复制
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());
            }
        }
    }

}

图像-

EN

回答 1

Stack Overflow用户

发布于 2020-06-26 06:15:21

这可能是有帮助的,这是工作样本。

代码语言:javascript
运行
AI代码解释
复制
    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;
        
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58353278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文