前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python识别车牌号

python识别车牌号

作者头像
JaneYork
发布2023-10-11 15:18:35
2040
发布2023-10-11 15:18:35
举报
代码语言:javascript
复制
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()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档