如何使用Google Cloud Vision维护文本结构

问题描述

您好,我目前正在构建一个可以逐行提取收款项目的应用程序,我将进行一些预处理,其中删除了收款不必要的数据,并且图像仅集中在收款主体上。目前,我正在Tesseract和Google Cloud Vision OCR之间进行选择,事实是,Tesseract能够捕获接收器的结构,但并不总是准确的,例如当接收器墨水褪色时,它并不完全准确。

但是 Google Cloud Vision是一个非常准确的框架,但是不能保留文本的结构。

这是图片。

My Reciept

这是Tesseract OCR的输出

enter image description here

这是 Google Cloud Vision OCR

的输出

enter image description here

现在我的问题是,我将如何使用Google Cloud Vision OCR产生相同的输出,它非常准确,但是收据的文本却非常不整齐。我将如何解决这个问题/它们是我可以研究的替代框架吗?

我的代码

*public class TesseractOCR  {
public String convert(String nameOfFile) {
    // Your file name here,for example Capture.PNG
    File imageFile = new File(nameOfFile);
    ITesseract instance = new Tesseract();  // JNA Interface Mapping
    // ITesseract instance = new Tesseract1(); // JNA Direct Mapping
    /*
    You need to point to your tessdata folder,here by default i have put it in our repo directory.
     */
    instance.setDatapath("C:\\Users\\Programming\\Desktop\\TextDetection\\tessdata"); // path to tessdata directory
    String result = "";
    try {
        result = instance.doOCR(imageFile);
        System.out.println(result);
    } catch (TesseractException e) {
        System.err.println(e.getMessage());
    }
    return result;
  }
}*


*public class DetectText {

/**
 *
 * @param filePath Your file path so in the format of C://Username//SomeFolder//SomeFolder//MyFile.png
 * @throws IOException
 */
// Detects text in the specified image.
public void detectText(String filePath) throws 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(Feature.Type.TEXT_DETECTION).build();
    AnnotateImageRequest request =
            AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    // Initialize client that will be used to send requests. This client only needs to be created
    // once,and can be reused for multiple requests. After completing all of your requests,call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                System.out.format("Error: %s%n",res.getError().getMessage());
                return;
            }
            // For full list of available annotations,see http://g.co/cloud/vision/docs
            for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
                System.out.format("Text: %s%n",annotation.getDescription());
                System.out.format("Position : %s%n",annotation.getBoundingPoly());
            }
        }
    }
}*

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)