pytesseract无法识别图像

时间:2020-12-20 04:08:00

标签: python opencv ocr python-tesseract

我正在构建一个图像分解程序,尽管我进行了多次尝试,但仍然没有得到任何文本输出。我放入其中的图像看起来尽可能简单。它们被二值化并且几乎没有偏差。我究竟做错了什么?有没有我没有导入的特殊神经网络库?Image

这是我的代码

import cv2
import numpy as np
import os
import imutils
from pytesseract import Output
import pytesseract
a1 = 0
a2 = 1

diflist = []
areas = []
images = []
rotated_shreds = []
cropped_shreds = []
idx = 0


scan = cv2.imread('test3.jpeg')

height = scan.shape[0]
width = scan.shape[1]
DrawnContours = np.zeros(shape=[height, width, 3], dtype=np.uint8)
blank_image2 = np.zeros(shape=[height, width, 3], dtype=np.uint8)

#grayscales the image
gray = cv2.cvtColor(scan, cv2.COLOR_BGR2GRAY)
gaus = cv2.GaussianBlur(gray, (3,3),0)
canny_output = cv2.Canny(gaus,50,50)


StripConts, hierarchy= cv2.findContours(canny_output, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(DrawnContours, StripConts, -1, (255,255,255),12)
DrawnContoursgray = cv2.cvtColor(DrawnContours, cv2.COLOR_BGR2GRAY)
DrawConts, hierarchy= cv2.findContours(DrawnContoursgray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)


sorted_contours= sorted(DrawConts, key=cv2.contourArea, reverse= True)

for contour in sorted_contours:
        measure = cv2.contourArea(contour)
        areas.append(measure)
        print(measure)


for area in areas:
    try:
        dif = areas[a1]-areas[a2]
        diflist.append(dif)
        a1 += 1
        a2 += 1
    except:
        break

maximum = max(diflist)

del diflist[0]


number = (diflist.index(maximum)+2)

print(number)


for c in sorted_contours:
    if idx >= number:
        break
    else:
        x,y,w,h = cv2.boundingRect(c)
        new_img=scan[y:y+h,x:x+w]
        images.append(new_img)
        idx+=1

for image in images:
        try:
            shred_gaus = cv2.GaussianBlur(image, (3,3),0)
            shred_canny_output = cv2.Canny(shred_gaus, 130, 130)
            ShredCont, hierarchy = cv2.findContours(shred_canny_output, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
            sorted_contours= sorted(ShredCont, key=cv2.contourArea, reverse= True)
            cnt = sorted_contours[1]
            rect = cv2.minAreaRect(cnt)
            angle = rect[2]
            if angle < 0:
                rotguide = -90 +abs(angle)
                print(rotguide)
            else:
                rotguide = 90 - angle
                print(rotguide)
            rotated_shred = imutils.rotate_bound(image, rotguide)
            rotated_shreds.append(rotated_shred)

        except:
            counter = 100


for rotated_shred in rotated_shreds:
    print("r")
    ret, thresh2 = cv2.threshold(rotated_shred, 120, 255, cv2.THRESH_BINARY)
    grayfinal = cv2.cvtColor(thresh2, cv2.COLOR_BGR2GRAY)
    strait_shred, hierarchy= cv2.findContours(grayfinal, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
    sorted_contours= sorted(strait_shred, key=cv2.contourArea, reverse= True)
    x,y,w,h = cv2.boundingRect(sorted_contours[0])
    new_img=thresh2[y:y+h,x:x+w]
    gaus_img = cv2.GaussianBlur(new_img, (3,3),0)
    cropped_shreds.append(gaus_img)



    cv2.imwrite('rotate.jpeg',cropped_shreds[0])


print(pytesseract.image_to_string(cropped_shreds[1]))

1 个答案:

答案 0 :(得分:0)

我尝试使用二值化值,通过对其进行模糊处理,然后使用我修复的低阈值对其进行二值化。