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

时间:2019-10-12 10:40:45

标签: google-cloud-platform ocr google-cloud-vision

我正在使用Cloud Vision API-DOCUMENT_TEXT_DETECTION功能来检测图像中的手写文本。尽管它为我提取了手写数据,但是当涉及到带有打印文本和手写文本的图像时,它并没有响应标识符,该标识符表示此位是手写的且已打印。直截了当地问我是要确认图像是否带有手写文字。注意-图片可能只包含手写文字或打印和手写文字的组合。

如果有人可以详细说明我需要传递给Cloud视觉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());
            }
        }
    }

}

图片-enter image description here

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助,这是工作示例。

    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;
        
    }