python - pytesseract 无法识别图像
问题描述
我正在构建一个图像去碎程序,尽管我进行了一些尝试,但我仍然没有得到任何文本输出。我放入其中的图像看起来尽可能简单。它们是二值化的,几乎没有偏差。我究竟做错了什么?有没有我没有导入的特殊神经网络库?图片
这是我的代码
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]))
解决方案
我尝试使用二值化值,通过模糊它然后用低阈值二值化它,我修复了它。
推荐阅读
- c# - c#无法从另一个表单与列表框交互
- python-3.x - 尝试使用 docker 安装 scipy 时出错
- arduino - 改进arduino for循环和void循环
- google-analytics - 拆分 GoogleTag Manager 标签变量
- php - How to make if else PHP cleaner using a case/switch?
- javascript - Vue 中可能有重复的插槽吗?
- html - 如何使用 firebase 在我的网站上托管 SVG 图像?
- java - Android 日期选择器片段更改为微调器
- python - Pandas:将列表值替换为来自另一个数据帧的一串值
- assembly - 为什么用零填充扇区的其余部分不适用于 MASM?