Python OpenCV单字母识别

时间:2017-07-12 22:04:57

标签: python opencv image-recognition

我尝试使用OpenCV识别使用网络摄像头的单个字母。 只有两个或更多的字母才有效。 OpenCV有机会支持单字母识别吗?

import cv2
import numpy as np
import pytesseract
import threading
from PIL import Image

class EyeWatcher:

    #(...)

    def work(self, words, callback):
        lastresult = ''
        cap = cv2.VideoCapture(0)

        self.__class__.isOpened = cap.isOpened()

        while(self.__class__.isOpened):
            ret, capimg = cap.read()

            img = cv2.cvtColor(capimg, cv2.COLOR_BGR2GRAY)

            kernel = np.ones((1, 1), np.uint8)
            img = cv2.dilate(img, kernel, iterations=1)
            img = cv2.erode(img, kernel, iterations=1)

            arr = Image.fromarray(img)
            result = pytesseract.image_to_string(arr)

            if result and lastresult != result:
                if not words or any(result in s for s in words):
                    lastresult = result
                    callback(result)

        cap.release()

电话:

import eyewatcher

def hunted(r):    
    if r == 'H':
        print("Hi")
    # (...)
    else:
        print("nothing to do with you...")

eyewatcher.EyeWatcher.open(['A5', 'A4', 'H', 'S', 'U'], hunted)
#(...)

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以告诉tesseract,您期望图像中只有一个字符。查看docs并查找psm和oem模式。

image_to_string的definition表示您可以将命令行选项传递给它。

您的配置字符串应如下所示

'--tessdata-dir "/home/rvq/github/tesseract/tessdata/" --psm 10  --oem 2 '

我实施了快速最小化测试,效果非常好。

<强>码

import cv2
import numpy as np
import pytesseract
from PIL import Image


def work(image):
    lastresult = ''
    image = cv2.imread(image)

    img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    kernel = np.ones((1, 1), np.uint8)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)
    tessdata_dir_config = '--tessdata-dir "/home/rvq/github/tesseract/tessdata/" --psm 10  --oem 2 '
    arr = Image.fromarray(img)
    result = pytesseract.image_to_string(arr, config = tessdata_dir_config)
    print(result)

work('./00001.png')

图片00001.png:

enter image description here

命令行上的

结果:0