pytesseract识别数字

针对工程需求精度不够,目前只能识别率为86%左右。

用tesseract的深度学习可能会好一点,没搞懂怎么用?

单张图片

import cv2
import numpy as np
import matplotlib.pyplot as plt
import pytesseract
import os

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"

img_orig = cv2.imread('images/OCR/OCRS116.jpg')

image_RGB = cv2.cvtColor(img_orig, cv2.COLOR_BGR2RGB)

# 提取感兴趣区域
# cv2.rectangle(image_RGB, (1150,820),(1400,950),(0,0,255),1)
ROI = image_RGB[820:950, 1166:1380]
img = ROI
print(img.shape)
# print(pytesseract.image_to_boxes(ROI))

# cv2.imshow("roi", ROI)
# cv2.waitKey(0)

kernel = np.ones((5, 5), np.uint8)
open_img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

# 二值化
left_thred = 240
gray_img = cv2.cvtColor(open_img, cv2.COLOR_RGB2GRAY)
ret, thresh1 = cv2.threshold(gray_img, left_thred, 255, cv2.THRESH_BINARY)

# 根据自己的图像情况处理
# thresh1[0:20, 0:100] = 255
# thresh1[100:129, 0:210] = 255
# 模糊操作
blur = cv2.blur(thresh1,(3,3))


cv2.imshow("blur",blur)
cv2.waitKey(0)

img_strings = pytesseract.image_to_string(blur)
# 输入想要的字符段
print(img_strings[:6])

文件夹中多张图片

import cv2
import numpy as np
import matplotlib.pyplot as plt
import pytesseract
import os

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"

config='--psm 6 --oem 1 -c tessedit_char_whitelist=0123456789'

path = r"D:\BUFFER\Pycharm\OpencvLearn\images\OCR"
for filename in os.listdir(path):  # listdir的参数是文件夹的路径
    filenames = path + '\\' + filename
    # print(filenames)
    img_orig = cv2.imread(filenames, 1)
    # print(filenames)

    image_RGB = cv2.cvtColor(img_orig, cv2.COLOR_BGR2RGB)
    # cv2.rectangle(image_RGB, (1150,820),(1400,950),(0,0,255),1)
    ROI = image_RGB[820:950, 1165:1380]
    img = ROI
    # print(pytesseract.image_to_boxes(ROI))

    # cv2.imshow("roi", ROI)
    # cv2.waitKey(0)

    kernel = np.ones((5,5), np.uint8)
    open_img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

    # cv2.imshow("images", open_img)
    left_thred = 240
    gray_img = cv2.cvtColor(open_img,cv2.COLOR_RGB2GRAY)
    ret, thresh1 = cv2.threshold(gray_img, left_thred, 255, cv2.THRESH_BINARY)

    thresh1[0:20, 0:100] = 255
    thresh1[105:129, 0:210] = 255
    blur = cv2.blur(thresh1, (3,3))
    # cv2.imshow("thresh",thresh1)
    # cv2.waitKey(0)
    img_strings = pytesseract.image_to_string(blur)
    print(img_strings[:6])

Guess you like

Origin blog.csdn.net/moonlightpeng/article/details/121414165