簡體   English   中英

使用 PyTesseract 對標簽進行文本檢測

[英]Text Detection of Labels using PyTesseract

一款 label 檢測工具,可根據設備編號 (19-V1083AI) 自動識別圖像並按字母順序對圖像進行排序。 在識別設備label的輪廓后,我使用pytesseract庫將圖像轉換為字符串。 盡管代碼運行正確,但它從不輸出設備編號。 這是我第一次使用 pytesseract 庫和 goodFeaturesToTrack function。 任何幫助將不勝感激!

原始圖像

import numpy as np
import cv2
import imutils #resizeimage
import pytesseract # convert img to string
from matplotlib import pyplot as plt
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

# Read the image file
image = cv2.imread('Car Images/s3.JPG')

# Resize the image - change width to 500
image = imutils.resize(image, width=500)


# Display the original image
cv2.imshow("Original Image", image)
cv2.waitKey(0)

# RGB to Gray scale conversion
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("1 - Grayscale Conversion", gray)
cv2.waitKey(0)

# Noise removal with iterative bilateral filter(removes noise while preserving edges)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
cv2.imshow("2 - Bilateral Filter", gray)
cv2.waitKey(0)


corners = cv2.goodFeaturesToTrack(gray,60,0.001,10)

corners = np.int0(corners)

for i in corners:
    x,y = i.ravel()
    cv2.circle(image,(x,y),0,255,-1)
    coord = np.where(np.all(image == (255, 0, 0),axis=-1))
plt.imshow(image)

# Use tesseract to covert image into string
text = pytesseract.image_to_string(image, lang='eng')
print("Equipment Number is:", text)


plt.show()

Output 圖2

注意:它適用於其中一個圖像,但不適用於其他圖像Output Image2

我發現使用 PyTesseract 的特定配置選項會找到您的文本 - 以及一些噪音。 以下是解釋的配置選項: https://stackoverflow.com/a/44632770/42346

對於這個任務,我選擇了:“稀疏文本。不按特定順序查找盡可能多的文本。”

由於 PyTesseract 返回了更多“文本”,您可以使用正則表達式來過濾掉噪音。

這個特定的正則表達式查找兩位數、一個連字符、五位數字或字符、一個空格,然后是兩位數或字符。 這可以根據需要調整為您的設備編號格式,但我有理由相信這是一個很好的解決方案,因為在返回的文本中沒有其他類似設備編號的東西。

import re
import cv2
import pytesseract

image = cv2.imread('Fv0oe.jpg') 
text = pytesseract.image_to_string(image, lang='eng', config='--psm 11') 

for line in text.split('\n'): 
     if re.match(r'^\d{2}-\w{5} \w{2}$',line): 
         print(line) 

結果(無需圖像處理):

19-V1083 AI

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM