Python OpenCV PyteSeract未从裁剪图像中提取字符串

2024-06-06 14:57:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张图片(附件),想从表单中提取某些字段。例如,名字“Sarah”、她的电子邮件地址等。我有感兴趣的区域,它被突出显示,然后被裁剪。出于某种原因,我从图像到字符串的输出显示为空

所需的输出应提取数据。请有人给我指一下正确的方向好吗? 我正在关注这个关于上下文的伟大教程:https://www.youtube.com/watch?v=cUOcY9ZpKxw

['', '', '', '', '', '']

代码如下:


import cv2
import numpy as np
import pytesseract
import os
pytesseract.pytesseract.tesseract_cmd = r'Tesseract-OCR\tesseract.exe'

imgQ = cv2.imread('sarah.png')

#cv2.imshow('output',imgQ)
#cv2.waitKey(0)

roi = [[(98, 984), (680, 1074), 'text', 'Name'],
       [(740, 980), (1320, 1078), 'text', 'Phone'],
       [(100, 1418), (686, 1518), 'text', 'Email'],
       [(740, 1416), (1318, 1512), 'text', 'ID'],
       [(110, 1598), (676, 1680), 'text', 'City'],
       [(748, 1592), (1328, 1686), 'text', 'Country']]

myData=[]
for x,r in enumerate(roi):
        #highlighted the regions
        cv2.rectangle(imgQ, (r[0][0],r[0][1]),(r[1][0],r[1][1]),(0,255,0),cv2.FILLED)
        imgShow = cv2.addWeighted(imgQ,0.99,imgQ,0.1,0)
        #crop regions
        imgCrop = imgShow[r[0][1]:r[1][1], r[0][0]:r[1][0]]
        cv2.imshow(str(x),imgCrop)
        if r[2] == 'text':

            print('{} :{}'.format(r[3],pytesseract.image_to_string(imgCrop)))
            myData.append(pytesseract.image_to_string(imgCrop))
print(myData)    
            

image example here (also used in tutorial)


Tags: totextimageimportcv2printregionsimshow
1条回答
网友
1楼 · 发布于 2024-06-06 14:57:32

代码中的问题如下所示:

cv2.rectangle(img, (r[0][0], r[0][1]), (r[1][0], r[1][1]), (0, 255, 0), cv2.FILLED)
  • 这行执行什么

查找给定图像中的roi并用绿色填充。比如:

enter image description here

然后尝试从这个绿色矩形读取数据enumerate(roi)


  • 第二,为什么imgShow = cv2.addWeighted(img, 0.99, img, 0.1, 0)

  • 第三imgCrop = imgShow[r[0][1]:r[1][1], r[0][0]:r[1][0]]

我们从img开始收割怎么样

  • enter image description here

  • enter image description here

  • enter image description here

输出是

Name :Sarah

Phone :+ (00) 765-43-21

Email :sarah@abc.com

ID :1356856

City :London

Country :United Kingdom

代码:


import cv2
from pytesseract import image_to_string

img = cv2.imread("hzt5U.png")
# gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 21)
# txt = image_to_string(thr, config=" psm 6")
# print(txt)

roi = [[(98, 984), (680, 1074), 'text', 'Name'],
       [(740, 980), (1320, 1078), 'text', 'Phone'],
       [(100, 1418), (686, 1518), 'text', 'Email'],
       [(740, 1416), (1318, 1512), 'text', 'ID'],
       [(110, 1598), (676, 1680), 'text', 'City'],
       [(748, 1592), (1328, 1686), 'text', 'Country']]

my_data = []

for x, r in enumerate(roi):
    # highlighted the regions
    # cv2.rectangle(img, (r[0][0], r[0][1]), (r[1][0], r[1][1]), (0, 255, 0), cv2.FILLED)
    # imgShow = cv2.addWeighted(img, 0.99, img, 0.1, 0)

    # crop regions
    # imgCrop = imgShow[r[0][1]:r[1][1], r[0][0]:r[1][0]]
    imgCrop = img[r[0][1]:r[1][1], r[0][0]:r[1][0]]
    cv2.imwrite("/Users/ahx/Desktop/res{}.png".format(x), imgCrop)
    cv2.imshow(str(x), imgCrop)
    cv2.waitKey(0)

    if r[2] == 'text':
        print('{} :{}'.format(r[3], image_to_string(imgCrop)))
        my_data.append(image_to_string(imgCrop))

# print(my_data)

相关问题 更多 >