如何提高识别度?

问题描述 投票:0回答:1

我给自己定下了认护照的任务,但是我不能完全认出所有的区域。告诉我,有什么可以帮助的?使用了不同的过滤和精明的算法,但缺少一些东西。

# import the necessary packages
    from PIL import Image
    import pytesseract
    import argparse
    import cv2
    import os
    import numpy as np
    
    # построить разбор аргументов и разбор аргументов
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image" )
    ap.add_argument("-p", "--preprocess", type=str, default="thresh")
    args = vars(ap.parse_args())
    # загрузить пример изображения и преобразовать его в оттенки серого
    image = cv2.imread ("pt.jpg")
    
    gray = cv2.cvtColor (image, cv2.COLOR_BGR2GRAY)
    gray = cv2.Canny(image,300,300,apertureSize = 3)
    
    # check to see if we should apply thresholding to preprocess the
    # image
    if args["preprocess"] == "thresh":
        gray = cv2.threshold (gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    # make a check to see if median blurring should be done to remove
    # noise
    elif args["preprocess"] == "blur":
        gray = cv2.medianBlur (gray, 3)
    # write the grayscale image to disk as a temporary file so we can
    # apply OCR to it
    filename = "{}.png".format (os.getpid ())
    cv2.imwrite (filename, gray)
    
    # load the image as a PIL/Pillow image, apply OCR, and then delete
    # the temporary file
    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
    text = pytesseract.image_to_string (image, lang = 'rus+eng')
    os.remove (filename)
    print (text)
    os.system('python gon.py > test.txt') # doc output file
    # show the output images
    cv2.imshow ("Image", image)
    cv2.imshow ("Output", gray)
    cv2.waitKey (0)

enter image description here

enter image description here

opencv
1个回答
0
投票

当你只提供包含你想要解释的文本的区域时,Tesseract 更容易(也更快)识别文本,在你的情况下,中间的大黑字母,例如:

我指的是仅在绿色区域运行 Tesseract。由于文档的结构是可预测的,您可以轻松找到这些区域,如下所示:

  1. 二值化和反转图像(黑色=空)
  2. 使用 opencv 的 connectedComponentsWithStats() 函数获取所有连接组件及其位置和大小的列表
  3. 您可以对阈值进行硬编码以仅过滤您想要的字符,或者获取区域直方图并使用统计数据动态定义阈值
  4. 在剩余的连接组件上使用形态学操作(例如,使用水平核进行膨胀)将字母水平连接在一起
  5. 获取最终连接组件的边界框
  6. 可选:后处理在这些孤立的盒子中效果更好

将每个边界框作为单独的 Mat 进行 tesseract,这将大大简化问题。

© www.soinside.com 2019 - 2024. All rights reserved.