如何提高识别度?

时间:2019-07-09 20:47:45

标签: opencv

我为自己设定了识别护照的任务,但是我无法完全识别所有区域。告诉我,什么可以帮助?使用了不同的过滤和Canny算法,但缺少某些内容。 Коднеможетраспознатьсериюиномердокумента,以及такжемелкиесимволы,

# 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

1 个答案:

答案 0 :(得分:0)

当仅在包含要解释文本的区域(例如,中间是大而黑的字母)的区域提供文本时,Tesseract会更容易(更快)识别文本。

passport

我指的是仅在绿色区域中运行Tesseract。由于文档的结构是可预测的,因此可以轻松找到以下区域:

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

将每个边界框作为单独的Mat馈送到tesseract,它将大大简化问题。

相关问题