Python PyteSeract检测到错误的整数值
我试图检测在我的sqares中找到的数字,我想我可以使用libaryPython PyteSeract检测到错误的整数值,python,cv2,python-tesseract,Python,Cv2,Python Tesseract,我试图检测在我的sqares中找到的数字,我想我可以使用libarypytesseract,但出于某种原因,我读取了错误的值 这是控制台输出: 这里我有我所有的照片(它们是分开的,这只是为了展示它们) 将numpy导入为np 进口cv2 进口稀土 从PIL导入图像 导入pytesseract pytesseract.pytesseract.tesseract\u cmd=r'C:\Program Files\tesseract OCR\tesseract' img=cv2.imread('g
pytesseract
,但出于某种原因,我读取了错误的值
这是控制台输出:
这里我有我所有的照片(它们是分开的,这只是为了展示它们)
将numpy导入为np
进口cv2
进口稀土
从PIL导入图像
导入pytesseract
pytesseract.pytesseract.tesseract\u cmd=r'C:\Program Files\tesseract OCR\tesseract'
img=cv2.imread('gulRecNum.jpg')
灰色=cv2.CVT颜色(img,cv2.COLOR\U BGR2GRAY)
#转换为HSV,因为红色和黄色是色调最低的颜色,排在绿色之前
hsv=cv2.CVT颜色(img,cv2.COLOR\U BGR2HSV)
#在红色和黄色之间的色调上创建二值阈值图像
下限=(0240160)
上限=(30255255)
阈值=cv2。范围内(hsv、下部、上部)
#应用形态学
kernel=cv2.getStructuringElement(cv2.morp_ELLIPSE,(9,9))
clean=cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel)
kernel=cv2.getStructuringElement(cv2.morp_ELLIPSE,(15,15))
clean=cv2.morphologyEx(thresh,cv2.MORPH\u CLOSE,kernel)
#获取外部轮廓
轮廓=cv2.找到的轮廓(干净,cv2.外部翻新,cv2.链条近似简单)
等高线=等高线[0]如果len(等高线)==2其他等高线[1]
result1=img.copy()
result2=img.copy()
掩码=np.zero(result2.shape,dtype=np.uint8)
thresh=cv2.阈值(灰色,0,255,cv2.thresh\u二进制\u INV+cv2.thresh\u OTSU)[1]
投资回报率=0
对于等高线中的c:
cv2.绘制等高线(结果1,[c],0,(0,0,0),2)
#从轮廓获取旋转矩形
rot_rect=cv2.Minareact(c)
箱=cv2.箱点(旋转方向)
box=np.int0(box)
#在img副本上绘制旋转矩形
cv2.绘制等高线(结果2,[box],0,(0,0,0),2)
#Gør noget hvis区域位于større end 1。
#这个部件的面积是多少?
面积=cv2。轮廓面积(c)
如果(面积>1):
#求重心
M=cv2.力矩(c)
cx=int(M['m10']/M['m00']
cy=int(M['m01']/M['m00']
中心=(cx,cy)
打印(“\nx:,cx,”\ny:,cy)
颜色=(0,0,255)
cv2.圆(结果2,中心,3,颜色,-1)
cv2.putText(结果2,“中心”(int(cx)-10,int(cy)-20),
cv2.FONT\U HERSHEY\U SIMPLEX,1.2,彩色,2)
#看看这部分
x、 y,w,h=cv2.boundingRect(c)
ROI=255-阈值[y:y+h,x:x+w]
cv2.绘制轮廓(掩模,[c],-1,(255255),-1)
imwrite('ROI{}.png'。格式(ROI\u编号),ROI)
Number=pytesseract.image_to_string(ROI,config='--psm 13--oem 3-c tessedit_char_whitelist=0123456789')
打印(“数字”,数字)
ROI_数+=1
#保存结果
cv2.imwrite(“4cubes_result2.png”,result2)
#显示结果
imS=cv2.调整大小(结果2,(600400))
cv2.imshow(“结果2”,imS)
cv2.等待键(0)
cv2.destroyAllWindows()
我想我可以将Number=pytesseract.image\u写入字符串(ROI,config='--psm 13--oem 3-c tessedit\u char\u whitelist=0123456789')打印(Number)
然后从图像中得到数字,但我不知道,怎么可能
编辑新错误
我如何用这张图片解决它
从PIL导入图像
从运算符导入itemgetter
将numpy作为np导入
进口easyocr
进口cv2
进口稀土
导入imutils
导入pytesseract
pytesseract.pytesseract.tesseract\u cmd=r'C:\Program Files\tesseract OCR\tesseract'
reader=easyocr.reader(['chu sim','en'])#只需运行一次即可将模型加载到内存中
#定义空数组
多维数据集=[]
def getNumber(投资回报率):
img=cv2.imread(ROI)
灰色=cv2.CVT颜色(img,cv2.COLOR\U BGR2GRAY)
ret,thresh=cv2。阈值(灰色,127255,0)
#cv2.imshow(阈值)
#cv2.imshow(“阈值原稿”,阈值)
#cv2.等待键(0)
##获得轮廓
轮廓,h=cv2.找到的轮廓(脱粒,cv2.RETR\u CCOMP,cv2.链近似\u简单)
##仅绘制面积较大的等高线
imx=img.形状[0]
imy=img.形状[1]
lp_面积=(imx*imy)/10
tmp_img=img.copy()
对于轮廓中的cnt:
近似=cv2.近似聚合度(cnt,0.01*cv2.弧长(cnt,真),真)
如果cv2.轮廓面积(cnt)>lp_面积:
#绘制长方体角点和最小面积矩形
rect=cv2.minareact(cnt)
长方体=cv2.长方体点(矩形)
box=np.int0(box)
#cv2.图纸轮廓(tmp_img,[box],0,(0,50,255),3)
#cv2.圆(tmp_img,元组(框[0]),8,(0,255,0),-1)
#cv2.圆(tmp_img,元组(框[1]),8,(0,255,0),-1)
#cv2.圆(tmp_img,元组(框[2]),8,(0,255,0),-1)
#cv2.圆(tmp_img,元组(框[3]),8,(0,255,0),-1)
#cv2.imshow(tmp_img)
#cv2.imshow(“最小面积矩形”,tmp\U img)
#cv2.等待键(0)
##正确的方向和裁剪
#链接,https://jdhao.github.io/2019/02/23/crop_rotated_rectangle_opencv/
宽度=整数(矩形[1][0])
高度=整数(矩形[1][1])
src_pts=box.astype(“float32”)
dst_pts=np.数组([[0,高度-1],
[0, 0],
[宽度-1,0],
[width-1,height-1]],dtype=“float32”)
M=cv2.getPerspectiveTransform(src_pts,dst_pts)
翘曲=cv2。翘曲透视图(img,M,(宽度,高度))
#在裁剪的图像上运行OCR
#如果预测值为数字打印,则先旋转
结果=reader.readtext(扭曲)
打印(结果)
预测数字=结果[0][1]
如果np.char.isdigit(预测数字)=真:
cv2.imshow(“扭曲”+投资回报率,扭曲)
其他:
rot_img=扭曲的。复制()
对于范围(0,3)内的i:
旋转图像=cv2.旋转(旋转图像,cv2.cv2.顺时针旋转90°)
结果=reader.readtext(旋转图像)
!pip install easyocr
import easyocr
reader = easyocr.Reader(['ch_sim','en']) # need to run only once to load model into memory
"""
Based on my answer of rotated card detection,
https://stackoverflow.com/questions/64860785/opencv-using-canny-and-shi-tomasi-to-detect-round-corners-of-a-playing-card/64862448#64862448
"""
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
img = cv2.imread('1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,0)
#cv2_imshow(thresh)
#cv2.imshow('Thresholded original',thresh)
#cv2.waitKey(0)
## Get contours
contours,h = cv2.findContours(thresh,cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
## only draw contour that have big areas
imx = img.shape[0]
imy = img.shape[1]
lp_area = (imx * imy) / 10
#################################################################
# Four point perspective transform
# https://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/
#################################################################
def order_points(pts):
# initialzie a list of coordinates that will be ordered
# such that the first entry in the list is the top-left,
# the second entry is the top-right, the third is the
# bottom-right, and the fourth is the bottom-left
rect = np.zeros((4, 2), dtype = "float32")
# the top-left point will have the smallest sum, whereas
# the bottom-right point will have the largest sum
s = pts.sum(axis = 1)
rect[0] = pts[np.argmin(s)]
rect[2] = pts[np.argmax(s)]
# now, compute the difference between the points, the
# top-right point will have the smallest difference,
# whereas the bottom-left will have the largest difference
diff = np.diff(pts, axis = 1)
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
# return the ordered coordinates
return rect
def four_point_transform(image, pts):
# obtain a consistent order of the points and unpack them
# individually
rect = order_points(pts)
(tl, tr, br, bl) = rect
# compute the width of the new image, which will be the
# maximum distance between bottom-right and bottom-left
# x-coordiates or the top-right and top-left x-coordinates
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
# compute the height of the new image, which will be the
# maximum distance between the top-right and bottom-right
# y-coordinates or the top-left and bottom-left y-coordinates
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
# now that we have the dimensions of the new image, construct
# the set of destination points to obtain a "birds eye view",
# (i.e. top-down view) of the image, again specifying points
# in the top-left, top-right, bottom-right, and bottom-left
# order
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype = "float32")
# compute the perspective transform matrix and then apply it
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
# return the warped image
return warped
#################################################################
#print(len(contours))
tmp_img = img.copy()
for cnt in contours:
approx = cv2.approxPolyDP(cnt,0.01 * cv2.arcLength(cnt, True), True)
## calculate number of vertices
#print(len(approx))
## Get the largest contours only
## Side count cannot be used since contours are not all rectangular
if cv2.contourArea(cnt) > lp_area:
#if len(approx) == 4 and cv2.contourArea(cnt) > lp_area:
# print("\n\n")
# print("#################################################")
# print("rectangle")
# print("#################################################")
# print("\n\n")
#tmp_img = img.copy()
#cv2.drawContours(tmp_img, [cnt], 0, (0, 255, 0), 6)
#cv2_imshow(tmp_img)
#cv2.imshow('Contour Borders', tmp_img)
#cv2.waitKey(0)
# tmp_img = img.copy()
# cv2.drawContours(tmp_img, [cnt], 0, (255, 0, 255), -1)
# cv2_imshow(tmp_img)
# #cv2.imshow('Contour Filled', tmp_img)
# #cv2.waitKey(0)
# # Make a hull arround the contour and draw it on the original image
# tmp_img = img.copy()
# mask = np.zeros((img.shape[:2]), np.uint8)
# hull = cv2.convexHull(cnt)
# cv2.drawContours(mask, [hull], 0, (255, 255, 255), -1)
# cv2_imshow(mask)
# #cv2.imshow('Convex Hull Mask', mask)
# #cv2.waitKey(0)
# # Draw minimum area rectangle
# #tmp_img = img.copy()
# rect = cv2.minAreaRect(cnt)
# box = cv2.boxPoints(rect)
# box = np.int0(box)
# cv2.drawContours(tmp_img, [box], 0, (255, 0, 0), 2)
# #cv2_imshow(tmp_img)
# #cv2.imshow('Minimum Area Rectangle', tmp_img)
# #cv2.waitKey(0)
# Draw box corners and minimum area rectangle
#tmp_img = img.copy()
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
#print(rect)
#print(box)
cv2.drawContours(tmp_img, [box], 0, (0, 50, 255), 3)
cv2.circle(tmp_img, tuple(box[0]), 8, (0, 255, 0), -1)
cv2.circle(tmp_img, tuple(box[1]), 8, (0, 255, 0), -1)
cv2.circle(tmp_img, tuple(box[2]), 8, (0, 255, 0), -1)
cv2.circle(tmp_img, tuple(box[3]), 8, (0, 255, 0), -1)
#cv2_imshow(tmp_img)
#cv2.imshow('Minimum Area Rectangle', tmp_img)
#cv2.waitKey(0)
## Correct orientation and crop
# Link, https://jdhao.github.io/2019/02/23/crop_rotated_rectangle_opencv/
width = int(rect[1][0])
height = int(rect[1][1])
src_pts = box.astype("float32")
dst_pts = np.array([[0, height-1],
[0, 0],
[width-1, 0],
[width-1, height-1]], dtype="float32")
M = cv2.getPerspectiveTransform(src_pts, dst_pts)
warped = cv2.warpPerspective(img, M, (width, height))
#cv2_imshow(warped)
# Run OCR on cropped image
# If the predicted value is digit print else rotate first
result = reader.readtext(warped)
predicted_digit = result[0][1]
print("Detected Text:")
if np.char.isdigit(predicted_digit) == True:
print(result)
print(predicted_digit)
cv2_imshow(warped)
else:
rot_img = warped.copy()
for i in range(0, 3):
rotated_image = cv2.rotate(rot_img, cv2.cv2.ROTATE_90_CLOCKWISE)
result = reader.readtext(rotated_image)
#if np.array(result).size == 0:
# continue
if not result:
rot_img = rotated_image
continue
#if len(result) == 0:
# continue
predicted_digit = result[0][1]
#print(result)
#print(predicted_digit)
#cv2_imshow(rotated_image)
if np.char.isdigit(predicted_digit) == True:
print(result)
print(predicted_digit)
cv2_imshow(rotated_image)
break
rot_img = rotated_image
# # Draw bounding rectangle
# #tmp_img = img.copy()
# x, y, w, h = cv2.boundingRect(cnt)
# cv2.rectangle(tmp_img, (x, y), (x + w, y + h), (255, 0, 0), 2)
# #cv2_imshow(tmp_img)
# #cv2.imshow('Bounding Rectangle', tmp_img)
# #cv2.waitKey(0)
# # Bounding Rectangle and Minimum Area Rectangle
# #tmp_img = img.copy()
# rect = cv2.minAreaRect(cnt)
# box = cv2.boxPoints(rect)
# box = np.int0(box)
# cv2.drawContours(tmp_img, [box], 0, (0, 0, 255), 2)
# x, y, w, h = cv2.boundingRect(cnt)
# cv2.rectangle(tmp_img, (x, y), (x + w, y + h), (0, 255, 0), 2)
# #cv2_imshow(tmp_img)
# #cv2.imshow('Bounding Rectangle', tmp_img)
# #cv2.waitKey(0)
# # determine the most extreme points along the contour
# # https://www.pyimagesearch.com/2016/04/11/finding-extreme-points-in-contours-with-opencv/
# tmp_img = img.copy()
# extLeft = tuple(cnt[cnt[:, :, 0].argmin()][0])
# extRight = tuple(cnt[cnt[:, :, 0].argmax()][0])
# extTop = tuple(cnt[cnt[:, :, 1].argmin()][0])
# extBot = tuple(cnt[cnt[:, :, 1].argmax()][0])
# cv2.drawContours(tmp_img, [cnt], -1, (0, 255, 255), 2)
# cv2.circle(tmp_img, extLeft, 8, (0, 0, 255), -1)
# cv2.circle(tmp_img, extRight, 8, (0, 255, 0), -1)
# cv2.circle(tmp_img, extTop, 8, (255, 0, 0), -1)
# cv2.circle(tmp_img, extBot, 8, (255, 255, 0), -1)
# print("Corner Points: ", extLeft, extRight, extTop, extBot)
# cv2_imshow(tmp_img)
# #cv2.imshow('img contour drawn', tmp_img)
# #cv2.waitKey(0)
# #cv2.destroyAllWindows()
# ## Perspective Transform
# tmp_img = img.copy()
# pts = np.array([extLeft, extRight, extTop, extBot])
# warped = four_point_transform(tmp_img, pts)
# cv2_imshow(tmp_img)
# #cv2.imshow("Warped", warped)
# #cv2.waitKey(0)
cv2_imshow(tmp_img)
#cv2.destroyAllWindows()
Detected Text:
[([[85, 67], [131, 67], [131, 127], [85, 127]], '1', 0.9992043972015381)]
1
Detected Text:
[([[85, 65], [133, 65], [133, 125], [85, 125]], '2', 0.9991914629936218)]
2
Detected Text:
[([[96, 72], [144, 72], [144, 128], [96, 128]], '4', 0.9996564984321594)]
4
Detected Text:
[([[88, 76], [132, 76], [132, 132], [88, 132]], '3', 0.9973381161689758)]
3