[英]Reading license plate from image using OpenCV Python and Tesseract
I have a problem because I have separate registrations for photos.我有一个问题,因为我有单独的照片注册。 Now I would like to get the registration number from the photo.
现在我想从照片中获取注册号。 Unfortunately, the effectiveness of the code I wrote is very low and I would like to ask for help in achieving greater efficiency.
不幸的是,我写的代码效率很低,我想寻求帮助以实现更高的效率。 any tips?
有小费吗?
In the first phase, the photo looks like this在第一阶段,照片看起来像这样
Then transform the photo to gray and only the black color contrasts然后将照片转换为灰色,只有黑色对比
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# define range of black color in HSV
lower_val = np.array([0,0,0])
upper_val = np.array([179,100,130])
# Threshold the HSV image to get only black colors
mask = cv2.inRange(hsv, lower_val, upper_val)
receives收到
What can I add or do to improve the effectiveness of the program.我可以添加什么或做什么来提高程序的有效性。 is there a way for the program to retrieve registrations a bit?
有没有办法让程序稍微检索注册? Will this help
这会帮助吗
configr = ('-l eng --oem 1 --psm 6-c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
text = pytesseract.image_to_string(mask,lang='eng', config=configr)
print(text)
Here's an approach:这是一种方法:
Color threshold to extract black text.提取黑色文本的颜色阈值。 We load the image, convert to HSV colorspace, define a lower and upper color range, and use
cv2.inRange()
to color threshold and obtain a binary mask我们加载图像,转换为 HSV 颜色空间,定义上下颜色范围,并使用
cv2.inRange()
颜色阈值并获得二值掩码
Perform morphological operations.进行形态学操作。 Create a kernel and perform morph close to fill holes in the contours.
创建一个内核并执行变形接近以填充轮廓中的孔。
Filter license plate contours.过滤车牌轮廓。 Find contours and filter using bounding rectangle area.
使用边界矩形区域查找轮廓和过滤器。 If a contour passes this filter, we extract the ROI and paste it onto a new blank mask.
如果轮廓通过此过滤器,我们将提取 ROI 并将其粘贴到新的空白蒙版上。
OCR using Pytesseract.使用 Pytesseract 进行 OCR。 We invert the image so desired text is black and throw it into Pytesseract.
我们反转图像,使所需的文本为黑色,然后将其放入 Pytesseract。
Here's a visualization of each step:这是每个步骤的可视化:
Obtained mask from color thresholding + morph closing从颜色阈值+变形闭合获得蒙版
Filter for license plate contours highlighted in green过滤以绿色突出显示的车牌轮廓
Pasted plate contours onto a blank mask将板轮廓粘贴到空白掩模上
Inverted image ready for Tesseract为 Tesseract 准备的倒置图像
Result from Tesseract OCR Tesseract OCR 的结果
PZ 689LR
PZ 689LR
Code代码
import numpy as np
import pytesseract
import cv2
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
# Load image, create blank mask, convert to HSV, define thresholds, color threshold
image = cv2.imread('1.png')
result = np.zeros(image.shape, dtype=np.uint8)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([0,0,0])
upper = np.array([179,100,130])
mask = cv2.inRange(hsv, lower, upper)
# Perform morph close and merge for 3-channel ROI extraction
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
close = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1)
extract = cv2.merge([close,close,close])
# Find contours, filter using contour area, and extract using Numpy slicing
cnts = cv2.findContours(close, cv2.RETR_TREE, 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)
area = w * h
if area < 5000 and area > 2500:
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 3)
result[y:y+h, x:x+w] = extract[y:y+h, x:x+w]
# Invert image and throw into Pytesseract
invert = 255 - result
data = pytesseract.image_to_string(invert, lang='eng',config='--psm 6')
print(data)
cv2.imshow('image', image)
cv2.imshow('close', close)
cv2.imshow('result', result)
cv2.imshow('invert', invert)
cv2.waitKey()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.