Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python OpenCV单字母识别_Python_Opencv_Image Recognition - Fatal编程技术网

Python OpenCV单字母识别

Python OpenCV单字母识别,python,opencv,image-recognition,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 = ''

我正试图用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)
#(...)

谢谢。

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

图像_to_字符串表示可以向其传递命令行选项

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

'--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')

Image 00001.png:


命令行上的结果:0