python识别车牌号

2021/7/30 17:05:59

本文主要是介绍python识别车牌号,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

import cv2

"""定义一些参数"""
# imshow 窗口的尺寸
frameWidth = 640
frameHeight = 480
# 导入 xml 文件(根据自己的路径进行相对应的调整)
numberPlatesCascade = cv2.CascadeClassifier("C:/Users/geek/Desktop/haarcascade_russian_plate_number.xml")
# 设定一个可以被检测到的最小的物体的面积,可用于去除一些不必要的噪声
minArea = 500
# bounding box 的颜色
color = (255, 0, 255)

# 导入计算机自带的摄像头,并设置摄像头所拍摄画面的尺寸、亮度
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10, 150)
# 用于保存车牌照片时的计数
count = 0
global imgRoi

while True:
    success, img = cap.read()
    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 转为灰度图像
    numberPlates = numberPlatesCascade.detectMultiScale(imgGray, 1.1, 4)  # 调用xml文件抓到图像中的车牌

    for (x, y, w, h) in numberPlates:
        area = w * h
        if area > minArea:
            cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)  # 添加 bounding box
            cv2.putText(img, "NUMBER PLATE", (x, y-5),  
                        cv2.FONT_HERSHEY_PLAIN, 1, color, 2)  # 给 bounding box 添加注解
        # 单独将车牌抓出来,另外显示
        imgRoi = img[y:y + h + 30, x:x + w]
        cv2.imshow("Number Plate", imgRoi)

    cv2.imshow("Result", img)
    k=cv2.waitKey(1)
    if k & 0xFF == ord("s") and 'imgRoi' in dir():  # 用于保存抓到的车牌
        cv2.imwrite("C:/Users/geek/Desktop/platenum/NumberPlate_" + str(count) + ".jpg", imgRoi)
        cv2.rectangle(img, (0, 200), (640, 300), (0, 255, 0), cv2.FILLED)  # 做一个用于提示保存成功地提示条
        cv2.putText(img, "Scan Saved", (150, 265), cv2.FONT_HERSHEY_PLAIN,
                    2, (0, 255, 255), 2)  # 在提示条上写上内容
        cv2.imshow("Result", img)
        cv2.waitKey(500)
        count += 1
    if k == 27:
        #通过esc键退出摄像
        cv2.destroyAllWindows()
        break
#关闭摄像头
cap.release()


这篇关于python识别车牌号的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程