image-recognition - 如何从图像中读取所有文本?
问题描述
我面临以下挑战:
- 它没有提取所有文本。
- 读取的所有文本都在输出 u'\x0c'
- 它没有形成正确的文本
image_to_data 能够转换一些,但大部分都不正确。我附上下面的代码
fn = 'testimgnum.png'
image = cv2.imread(fn)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
'''
gray = cv2.Canny(gray, 100, 200)
gray = cv2.dilate(gray, None, iterations=1)
gray = cv2.erode(gray, None, iterations=1)
'''
cv2.imwrite('nt1.png', gray)
cnts = cv2.findContours(gray.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
(cnts, _) = contours.sort_contours(cnts)
orig = gray.copy()
arr_values = []
ctr = 0
#filter the boxes
custom_config = r'--oem 3 --psm 10000'
k = 0
for c in cnts:
k=k+1
#print("c:",c)
try:
text_val=''
box = cv2.minAreaRect(c)
box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
box = np.array(box, dtype="int")
tl, tr, br, bl = box
#print(box)
width = abs(br[0]-tl[0])
height = abs(tl[1]-br[1])
x1= min(tl[0],br[0])
x2= max(tl[0],br[0])
y1=max(tl[1],br[1])
y2 = min(tl[1],br[1])
print(x1,x2,y1,y2, height, width)
crop_img = gray[y2-6:y1+6, x1-4:x2+4]
cv2.imwrite('{0}.png'.format(k), crop_img)
cv2.imshow("Image", crop_img)
cv2.waitKey(0)
#crop_img1 = cv2.resize(crop_img, None, fx=6, fy=6, interpolation=cv2.INTER_CUBIC)
text_val = pytesseract.image_to_string(crop_img, lang='eng',config='--oem 3 -c tessedit_char_whitelist=0123456789')#, config=custom_config,
print("text_val:", text_val)
arr_values.append([x1,x2,y1,y2,width,height,text_val])
except Exception as e:
print("e:", e)
ctr+=1
pass
我正在使用 tesseract 4 和 python 2.7。我有一个类似问题的工作模型。我使用的是旧版本的 tesseract。
解决方案
这就是我的方法:
那将是完整的代码:
import cv2
import pytesseract
# Read image
img = cv2.imread('KbZMp.png')
# Mask white-ish parts
mask = (img > 248).all(axis=2).astype(img.dtype) * 255
mask = cv2.dilate(mask, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)))
# Find contours w.r.t. the OpenCV version; retrieve bounding rectangles
cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
rects = [cv2.boundingRect(cnt) for cnt in cnts]
# Just for visualization purposes
out = img.copy()
# Iterate bounding rectangles, and OCR
for x, y, w, h in rects:
# Use saturation channel of ROI for actual OCR
sat = cv2.cvtColor(img[y:y+h, x:x+w], cv2.COLOR_BGR2HSV)[..., 1]
# Pytesseract with -psm 6
text = pytesseract.image_to_string(sat, config='--psm 6')\
.replace('\n', '').replace('\f', '')
# Just for visualization purposes
out = cv2.rectangle(out, (x, y), (x+w, y+h), (0, 255, 0), 2)
out = cv2.putText(out, text, (x, y-5),
cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255))
# Just for visualization purposes
cv2.imshow('out', out)
cv2.waitKey(0)
而且,这是结果的可视化:
警告:我使用曼海姆大学图书馆的特殊版本的 Tesseract 。
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.19041-SP0
Python: 3.9.1
PyCharm: 2021.1.1
OpenCV: 4.5.2
pytesseract: 5.0.0-alpha.20201127
----------------------------------------
推荐阅读
- ssl - 配置嵌入式 Tomcat 以使用 BouncyCastle 提供符合 FIPS 的 HTTP2
- xslt - XSLT - 过滤带有日期和当前日期的 XML 节点 ()
- powershell - 按 OU 筛选的 PowerShell
- excel - 你能解释一下这行代码是如何工作的(工作表函数)
- flutter - 我想加密我的颤振代码项目并且仍然能够运行
- python - Flask 400 错误请求 KeyError
- google-cloud-intellij - 在 Intellij IDEA 中,我可以在哪里看到更改 kubernetes 命名空间的“设置上下文”?
- arrays - 在 Reactjs 中使用 json 数组渲染表格
- python - Pandas - 需要在后续行中添加 2 个具有相同 CSV 的数据框
- aws-cdk - 类中未找到 AWS CDK CloudMapOptions 容器和 container_port