以下是使用OpenCV在给定图像中使用牌照周围的矩形框检测到的牌照号码示例。
使用OpenCV和Pytesseract从车牌中提取文本我们可以使用OpenCV提取车牌号。我们可以使用边缘检测技术提取文本。在获得灰度格式的图像后,我们将图像转换为双向滤镜模式。接下来,我们在感兴趣的区域周围绘制一个包含车牌ID的框,使用Pytesseract库中具有图像到字符串功能的函数,我们可以获得车牌编号。import cv2
import imutils
import numpy as np
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:Program FilesTesseract-OCR esseract.exe'
for i in lst_add[1562:1572]:
print(i)
img = cv2.imread(i,cv2.IMREAD_COLOR)
img = cv2.resize(img, (600,400) )
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 13, 15, 15)
edged = cv2.Canny(gray, 30, 200)
contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
contours = sorted(contours, key = cv2.contourArea, reverse = True)[:10]
screenCnt = None
for c in contours:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * peri, True)
if len(approx) == 4:
screenCnt = approx
break
if screenCnt is None:
detected = 0
print ("No contour detected")
else:
detected = 1
if detected == 1:
cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)
mask = np.zeros(gray.shape,np.uint8)
new_image = cv2.drawContours(mask,[screenCnt],0,255,-1,)
new_image = cv2.bitwise_and(img,img,mask=mask)
(x, y) = np.where(mask == 255)
(topx, topy) = (np.min(x), np.min(y))
(bottomx, bottomy) = (np.max(x), np.max(y))
Cropped = gray[topx:bottomx+1, topy:bottomy+1]
text = pytesseract.image_to_string(Cropped, config='--psm 11')
print("Detected license plate Number is:",text)
img = cv2.resize(img,(500,300))
Cropped = cv2.resize(Cropped,(400,200))
cv2.imshow('car',img)
cv2.imshow('Cropped',Cropped)
cv2.waitKey(1)
cv2.destroyAllWindows()
为车牌持有人构建虚拟的MongoDB数据库我们使用pymongo库在MongoDB中创建一个名为Charan的数据库。在MongoDB内部创建一个名为License Details的表,该表包含多个字段,例如License ID,候选人名称,地址和车牌号。因此,我们设计了一个虚拟数据库表,其中包含所有相关详细信息,以使用车牌识别人员详细信息。from flask_pymongo import PyMongo
DEFAULT_CONNECTION_URL = "mongodb://localhost:27017/"
DB_NAME = "Charan"
# Establish a connection with mongoDB
client = pymongo.MongoClient(DEFAULT_CONNECTION_URL)
client.list_database_names()
dataBase = client[DB_NAME]
COLLECTION_NAME = "License_Details"
collection = dataBase[COLLECTION_NAME]
创建由键值格式的数据组成的词典列表。我们可以通过将列表作为MongoDB的insert_many函数中的参数传递来直接将详细信息推入表中。