【发布时间】:2019-10-30 04:13:33
【问题描述】:
需要从带有为每个字符输入给出的框的表单中识别文本。
我尝试为每个输入使用边界框并裁剪该特定输入,即我可以获得所有用于在“名称”字段中输入的框。但是当我尝试检测一组盒子中的单个盒子时,我无法做到这一点,opencv 只为所有盒子返回一个轮廓。 for 循环中引用的文件是包含边界框坐标的文件。 cropped_img 是属于单个字段输入的图像(例如名称)。
完整的图片 这是表单的图像。
每个字段的裁剪图像
它包含许多用于输入字符的框。此处检测到的轮廓数始终为 1。为什么我无法检测到所有单个盒子? 简而言之,我想要cropped_img 中的所有单个框。
此外,非常感谢任何其他处理表单 ocr 任务的想法!
for line in file.read().split("\n"):
if len(line)==0:
continue
region = list(map(int,line.split(' ')[:-1]))
index=line.split(' ')[-1]
text=''
contentDict={}
#uzn in format left, up, width, height
region[2] = region[0]+region[2]
region[3] = region[1]+region[3]
region = tuple(region)
cropped_img = panimg[region[1]:region[3],region[0]:region[2]]
index=index.replace('_', ' ')
if index=='sign' or index=='picture' or index=='Dec sign':
continue
kernel = np.ones((50,50),np.uint8)
gray = cv2.cvtColor(cropped_img, cv2.COLOR_BGR2GRAY)
ret, threshold = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
threshold = cv2.bitwise_not(threshold)
dilate = cv2.dilate(threshold,kernel,iterations = 1)
ret, threshold = cv2.threshold(dilate,127,255,cv2.THRESH_BINARY)
dilate = cv2.dilate(threshold,kernel,iterations = 1)
contours, hierarchy = cv2.findContours(dilate,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
contours.sort(key=lambda x:get_contour_precedence(x, panimg.shape[1]))
print("Length of contours detected: ", len(contours))
for j, ctr in enumerate(contours):
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)
# Getting ROI
roi = cropped_img[y:y+h, x:x+w]
# show ROI
cv2.imshow('segment no:'+str(j-1),roi)
cv2.waitKey(0)
文件'file'的内容如下:
462 545 468 39 AO_Office
450 785 775 39 Last_Name
452 836 770 37 First_Name
451 885 772 39 Middle_Name
241 963 973 87 Abbreviation_Name
预期输出是用于为每个字段输入单个字母的各个框的轮廓
【问题讨论】: