首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用Python提取和识别车牌号码?

如何用Python提取和识别车牌号码?
EN

Stack Overflow用户
提问于 2019-01-29 18:36:10
回答 3查看 9.2K关注 0票数 3

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

代码:

代码语言:javascript
运行
复制
 from PIL import Image
 from pytesseract import image_to_string

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

虽然这适用于正常扫描的文档,但它不适用于车辆号牌。

示例图像1

示例图像2

EN

回答 3

Stack Overflow用户

发布于 2019-01-30 17:17:47

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

代码语言:javascript
运行
复制
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()

输出是如下所示的车牌:

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

代码语言:javascript
运行
复制
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

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

如果您将车牌图像透视转换为边界框矩形,并删除周围的额外边框,则解析将更加准确。如果你也需要帮助,请告诉我。

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

票数 3
EN

Stack Overflow用户

发布于 2019-01-29 23:26:04

此命令仅适用于second image

代码语言:javascript
运行
复制
from PIL import Image, ImageFilter
import pytesseract

img = Image.open('TcjXJ.jpg')
img2 = img.filter(ImageFilter.BLUR)
pixels = img2.load()
width, height = img2.size
x_ = []
y_ = []
for x in range(width):
    for y in range(height):
        if pixels[x, y] == (255, 255, 255):
            x_.append(x)
            y_.append(y)

img = img.crop((min(x_), min(y_),  max(x_), max(y_)))
text = pytesseract.image_to_string(img, lang='eng', config='-c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
print(text)

你已经得到了输出:

代码语言:javascript
运行
复制
TN 99 F 2378
票数 2
EN

Stack Overflow用户

发布于 2019-01-29 20:14:26

你可以使用OpenVINO engine,它包含了用于车牌检测的pretrained model and sample

  • Python.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54419097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档