python车牌识别并打印出识别信息

python用opencv识别车牌的完整信息并打印出来并导出至txt文本

这个例子还挺多的,
比如这个站内的博客,就可以:https://blog.csdn.net/weixin_45182459/article/details/125915894
或者这个:https://aitechtogether.com/python/51899.html

···

你也可以参考一下下面的:

# -*- coding: UTF-8 -*-
import cv2
import numpy as np

# 读取图片
img = cv2.imread('car_plate.jpg')
# 车牌定位
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)    # 灰度处理
# 自适应阈值二值化
binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 7, 5)
# 下面两行是用Sobel算子边缘检测
sobel = cv2.Sobel(binary, cv2.CV_8U, 1, 0, ksize = 3)
cv2.imwrite('sobel.jpg', sobel)
# 寻找车牌位置
contours = cv2.findContours(sobel, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = contours[0] if len(contours) == 2 else contours[1]
car_plate = []
for c in contours:
    # 用最小外接矩形框出车牌
    rect = cv2.minAreaRect(c)
    box = np.int0(cv2.boxPoints(rect))
    # 计算最小外接矩形的面积
    area = int(rect[1][0] * rect[1][1])
    # 筛选与车牌面积接近的外接矩形
    if area > 300 and area < 700:
        car_plate.append(box)
        cv2.drawContours(img, [box], 0, (0, 255, 0), 3)
# 识别车牌号
result = []
for m in car_plate:
    Xs = [i[0] for i in m]
    Ys = [i[1] for i in m]
    x1 = min(Xs)
    x2 = max(Xs)
    y1 = min(Ys)
    y2 = max(Ys)
    hight = y2 - y1
    width = x2 - x1
    crop_img = binary[y1:y1 + hight, x1:x1 + width]
    result_img = cv2.resize(crop_img, (136, 36))
    # 将识别的车牌号保存到txt文件中
    result.append(result_img)
    with open('result.txt', 'a') as f:
        f.write(result_img)
        f.write('\n')
# 显示结果
cv2.imshow('car_plate', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

import cv2
import pytesseract

# 读取图片
img = cv2.imread('car.jpg')

# 灰度化
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 高斯模糊
blur = cv2.GaussianBlur(gray, (3, 3), 0)

# 边缘检测
canny = cv2.Canny(blur, 150, 250)

# 查找轮廓
contours, hierarchy = cv2.findContours(canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# 寻找可能的车牌区域
for cnt in contours:
    area = cv2.contourArea(cnt)
    if area > 1500 and area < 10000:
        x, y, w, h = cv2.boundingRect(cnt)
        if w > h:
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
            plate_img = img[y:y+h, x:x+w]
            plate_gray = gray[y:y+h, x:x+w]
            break

# 提取车牌号
plate_text = pytesseract.image_to_string(plate_gray, lang='chi_sim')

# 输出结果
print("车牌号码: ", plate_text)

# 将结果写入txt文件
with open('plate_number.txt', 'w') as f:
    f.write(plate_text)

答案参考来自 https://www.wodianping.com/