python - 如何在 Python 中使用 OpenCV 和 Tesseract 处理信用卡字体

标签 python opencv image-processing tesseract python-tesseract

我正在尝试使用 OpenCV 读取卡并输出卡号和到期日期。

import cv2
import pytesseract

filename = 'image1.png'
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(gray, 50, 150, apertureSize=3)
result = pytesseract.image_to_string(canny)
print(f"OCR Results: {result}")

cv2.imshow('img', img)
cv2.imshow('canny', canny)

if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

enter image description here
  • 处理前的图像

  • enter image description here
  • 图片经过 Canny

  • 结果文本看起来不太好。请看下面的截图:

    enter image description here

    问题:如何正确处理卡片字体以获得更好的效果。任何想法都受到高度赞赏。

    谢谢。

    最佳答案

    通过文本边缘时,OCR 似乎无法正常工作。
    您最好应用阈值而不是使用 Canny。

    我建议以下几个阶段:

  • 从 BGR 转换为 HSV 颜色空间,得到 HSV 的 S(饱和度)颜色 channel 。
    S 中的所有灰色像素均为零,而彩色像素均高于零。
  • 使用自动阈值转换为二进制(使用 cv2.THRESH_OTSU )。
  • 以最大尺寸裁剪轮廓。
    因为您发布的图像包含一些背景。
  • 在裁剪区域应用 OCR。

  • 这是代码:
    import numpy as np
    import cv2
    import imutils  # https://pypi.org/project/imutils/
    import pytesseract
    
    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'  # I am using Windows
    
    img = cv2.imread('image1.png')  # Read input image
    
    # Convert from BGR to HSV color space
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    
    # Get the saturation color channel - all gray pixels are zero, and colored pixels are above zero.
    s = hsv[:, :, 1]
    
    # Convert to binary using automatic threshold (use cv2.THRESH_OTSU)
    ret, thresh = cv2.threshold(s, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    
    # Find contours (in inverted thresh)
    cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    cnts = imutils.grab_contours(cnts)
    
    # Find the contour with the maximum area.
    c = max(cnts, key=cv2.contourArea)
    
    # Get bounding rectangle
    x, y, w, h = cv2.boundingRect(c)
    
    # Crop the bounding rectangle out of thresh
    thresh_card = thresh[y:y+h, x:x+w].copy()
    
    # OCR
    result = pytesseract.image_to_string(thresh_card)
    print(f"OCR Results:\n {result}")
    
    
    # Show images for debugging
    cv2.imshow('s', s)
    cv2.imshow('thresh', thresh)
    cv2.imshow('thresh_card', thresh_card)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    OCR 结果:

     Visa Classic
    
    | By)
    
    4000 1234 Sb18 9010
    
    CARDHOLDER MARE
    VISA
    

    还是不完美……

    年代:
    enter image description here

    脱粒:
    enter image description here

    阈值卡:
    enter image description here

    关于python - 如何在 Python 中使用 OpenCV 和 Tesseract 处理信用卡字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60811777/

    相关文章:

    python - 计算高效的日期加法 (Python)

    python - 分水岭分割不包括单独的对象?

    java - 使用M种预定义颜色中的N种进行颜色量化

    python - 以编程方式访问 QT Designer 对象

    python - 用Python处理视频的好资源?

    opencv - 为什么 OpenCV Contrib Repo 3.0.0 不支持这么多 FeatureDetector 和 DescriptorExtractor?

    image-processing - 大小为 3x3 的对角索贝尔算子的矩阵

    swift avfoundation AVCapturePhotoCaptureDelegate 捕获方法

    python - Python 中使用 socket.send() 的 UDP 调用非常慢

    java - 图像的深度为零是什么意思?