python - 如何用Python提取识别车牌号?

标签 python machine-learning computer-vision ocr python-tesseract

我尝试与 PIL 合作使用 pytesseract 从车牌图像中识别车辆登记号。但是我无法从这些图像中获取文本。

代码:

 from PIL import Image
 from pytesseract import image_to_string

 img= Image.open('D://carimage1')
 text = image_to_string(img)
 print(text)

虽然这适用于普通扫描文档,但不适用于车牌。

示例图片 1

enter image description here

示例图片 2

enter image description here

最佳答案

这里是关于如何解决您的问题的粗略想法。您可以在此基础上构建。您需要从图像中提取车牌,然后将图像发送到您的 tesseract。阅读代码注释以了解我正在尝试做什么。

import numpy as np
import cv2
import pytesseract
import matplotlib.pyplot as plt

img = cv2.imread('/home/muthu/Documents/3r9OQ.jpg')
#convert my image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#perform adaptive threshold so that I can extract proper contours from the image
#need this to extract the name plate from the image. 
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
contours,h = cv2.findContours(thresh,1,2)

#once I have the contours list, i need to find the contours which form rectangles.
#the contours can be approximated to minimum polygons, polygons of size 4 are probably rectangles
largest_rectangle = [0,0]
for cnt in contours:
    approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
    if len(approx)==4: #polygons with 4 points is what I need.
        area = cv2.contourArea(cnt)
        if area > largest_rectangle[0]:
            #find the polygon which has the largest size.
            largest_rectangle = [cv2.contourArea(cnt), cnt, approx]

x,y,w,h = cv2.boundingRect(largest_rectangle[1])
#crop the rectangle to get the number plate.
roi=img[y:y+h,x:x+w]
#cv2.drawContours(img,[largest_rectangle[1]],0,(0,0,255),-1)
plt.imshow(roi, cmap = 'gray')
plt.show()

输出的是下面附上的车牌:

enter image description here

现在将这个裁剪后的图像传递到您的 tesseract 中。

gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
text = pytesseract.image_to_string(roi)
print text

我得到了您分享的示例图像的以下输出。

enter image description here

如果将车牌图像透视变换为边界框矩形并移除周围多余的边框,解析会更加准确。如果您也需要这方面的帮助,请告诉我。

如果按原样使用,上面的代码不适用于第二张图片,因为我将搜索过滤为具有 4 个边的多边形。希望你明白了。

关于python - 如何用Python提取识别车牌号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54419097/

相关文章:

python - 通过检查数据库并放置来解析python

r - R 中 svm 特征选择的示例

opencv - 使用单个静态相机从移动物体中获取深度图像

azure "Unauthorized. Access token is missing, invalid, audience is incorrect or have expired"

python - 从 QTableView 的每个单元格中获取数据

python - Python 中的网格网格函数 (meshgrid mgrid ogrid ndgrid)

machine-learning - 如何使用 tf.contrib.opt.ScipyOptimizerInterface 获取损失函数历史记录

python - 在 python 中尝试交叉验证时出错

python - 将图像拼接在一起 Opencv -Python

python - 如何在Python中读取ascii header 后的二进制数据