[英]How can I detect and recognize the digits in a answer key like the given image?
I have this image and I have to detect and store the answers and the corresponding question number.我有这张图片,我必须检测并存储答案和相应的问题编号。 I tried using OCR but it wasn't recognising anything properly.我尝试使用 OCR,但无法正确识别任何内容。 Is there any other way?有没有其他办法?
import cv2
from imutils import contours
import numpy as np
import pytesseract
config = '-l eng+equ --oem 3 --psm 8'
# Load image, grayscale, and adaptive threshold
image = cv2.imread('answerkey.png')
original = image.copy()
original1= image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cv2.imshow("thresh",thresh)
cv2.waitKey(0)
# Filter out all numbers and noise to isolate only boxes
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
(cnts,_) = contours.sort_contours(cnts, method="top-to-bottom")
(cnts,_) = contours.sort_contours(cnts, method="left-to-right")
print(cnts)
completetext=[]
for c in cnts:
area = cv2.contourArea(c)
if 500 < area <5000:
# cv2.imshow("cnt", original)
# cv2.waitKey(0)
x,y,w,h = cv2.boundingRect(c)
crop = original1[y:y+h, x:x+w]
gray1 = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
# cv2.imshow("cropp",crop)
# cv2.waitKey(0)
kernel = np.zeros((2,2), np.uint8)
erode = cv2.erode(gray1, kernel,iterations=2)
cv2.imshow("cropp erode", erode)
cv2.waitKey(0)
text = pytesseract.image_to_string(erode, config=config)
print(text)
I am getting blunder texts using the ocr directly on the complete image.我直接在完整图像上使用 ocr 得到错误文本。 Then I tried cropping each block and then feeding into OCR still I am not getting good results.然后我尝试裁剪每个块,然后输入 OCR 仍然没有得到好的结果。 If someone knows better way please help me with this如果有人知道更好的方法,请帮助我
try to:尝试:
hope this solves your problem.希望这能解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.