无法从这些图像中提取文本

时间:2020-01-13 10:10:53

标签: python-3.x python-imaging-library opencv3.0 python-tesseract

我试图从下面的图像中检测并提取文本,但是我无法正确获取标题文本。

图片1:

enter image description here

图片2:

enter image description here

对于那些类型的图像,我无法检测到图像并从中提取文本。 请帮助我处理这些图像。

我尝试了以下代码:

import cv2
import pytesseract    
pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract.exe"    
# Load image and threshold
image = cv2.imread(r"C:\Users\Admin\Downloads\Table_result\Table_result\semi train\Caffia 
coffee_before.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# Connect text with a horizontal shaped kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 3))
dilate = cv2.dilate(thresh, kernel, iterations=1)

# Remove non-text contours using aspect ratio filtering
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    x, y, w, h = cv2.boundingRect(c)
    aspect = w / h
    if aspect < 3:
        cv2.drawContours(thresh, [c], -1, (0, 0, 0), -1)

# Invert image and OCR
result = 255 - thresh
data = pytesseract.image_to_string(result, config='-l eng --oem 2 --psm 6')
print(data)

我的代码的结果: 对于图片1:

FD Product Cust. Prod. Product Description Pack Size Qty Weight Unit Line 
Value V
Code Code Price
bl [SnR] o1 Each 1.00 £0.00 FX0]
ISR XA oY) Pack 10 | 1.00 [N £2.05
2350500 Chillies Green 1x500 gm| 1.00 £3.13 £3.13

对于图片2:

POR245 Caffia Alliance RFA FD Coffee 3 Pint Sachets (x 120) 30 61.40 
1,842.00 0.00

对于图像1,我得到的是标头结果,但内容提取得不好。 对于图像2,将不会提取标头部分,但会正确提取内容数据。

0 个答案:

没有答案