社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

Sid • 5 年前 • 2361 次点击  

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

代码:

 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

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44191
 
2361 次点击  
文章 [ 3 ]  |  最新文章 5 年前
Muthukrishnan
Reply   •   1 楼
Muthukrishnan    6 年前

这里有一个关于如何解决问题的粗略想法。你可以在上面建造。您需要从图像中提取车牌号,然后将图像发送到您的镶嵌。阅读代码注释以了解我要做的事情。

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

现在把这张剪掉的照片传给你的镶嵌画。

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条边的多边形。希望你有这个想法。

Alderven
Reply   •   2 楼
Alderven    6 年前

这个只适用于 second image :

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)

你有输出:

TN 99 F 2378
Nuzhny
Reply   •   3 楼
Nuzhny    6 年前
  1. 你可以使用 OpenVINO engine ,它包含 pretrained model and sample 用于车牌检测和识别。
  2. OpenALPR 用于Python。