(本項目所有代碼打包至我的資源中,大家可在我的文章底部選擇下載)
目錄
需求
實現效果
學習視頻
大致思路
代碼實現
資源下載
需求
????????通過車輛識別技術,識別視頻中每個車輛及其車牌號,車輛應進行追蹤,避免重復計數量。
實現效果
車牌識別
學習視頻
使用 Python、Yolov8 和 EasyOCR 自動識別車牌 計算機視覺教程_嗶哩嗶哩_bilibili
大致思路
????????通過 opencv 將視頻轉換為幀,對幀應用車輛識別模型,并使用 model.track 或者 sort 追蹤器進行追蹤,給每個車輛一個唯一的 id ,然后進行車牌識別,對每一幀識別到的車牌,通過幾何判斷是否位于某個車輛中,是則將該車牌分配給該車輛,否則說明車牌識別錯誤或車輛識別錯誤,不作考慮。將車牌分配好車輛后,對車牌進行裁剪,將裁剪好的車牌使用 opencv 技術轉換為灰度值圖片,再設置閾值轉換為閾值灰白圖像,然后使用 easyocr 或者 Paddleocr 等文字識別技術,對閾值黑白圖像進行字符識別,最終得到車牌信息,然后將所有信息,包括車輛、車牌 box 信息、置信度、車輛 id ,車牌號等信息放入 csv 表格中,方面查看測試結果。
代碼實現
引入模塊:
import numpy as np
from ultralytics import YOLO
import cv2from util import *
# from sort.sort import *
# 跟蹤器對象
# mot_tracker = Sort()
這里由于我的環境問題,下載不了使用 sort 的庫,即這幾個:
filterpy==1.4.5
scikit-image==0.17.2
lap==0.4.0
所以只能使用 yolo 自帶的追蹤器進行追蹤
定義存儲字典并解析視頻:
# 存儲所有信息
results = {}# 加載模型
coco_model = YOLO('yolov8n.pt', task='detect')
license_plate_detector = YOLO('best.pt')# 加載視頻
cap = cv2.VideoCapture('./3.mp4')
通過 opencv 的 VideoCapture 函數加載視頻,并加載模型,這里的車牌模型我使用的官方模型,車牌模型是自己訓練的,大家下載后自取。
識別幀,為了方便測試,只取前十幀:
# 讀取視頻幀
ret = True
count = -1
vehicles = [2, 3, 5, 7]
while ret:count += 1ret, frame = cap.read()# print(frame)if ret and count < 10:# 創建幀編號空字典,方便后面存儲數據results[count] = {}# detections = coco_model(frame)[0]detections = coco_model.track(frame, persist=True) # persist=True 會記住上一幀的信息# detection = detections.boxes.data.tolist()# print(detection)detections_ = []
如果檢測到車輛,則放入列表中
檢測車牌,并將車牌分配給對應的車輛:
# 檢測車牌licence_plates = license_plate_detector(frame)[0]for licence_plate in licence_plates.boxes.data.tolist():x1, y1, x2, y2, score ,class_id = licence_plate# get_car 分配每一個車牌給車輛xcar_1, ycar_1, x_car2, y_car2, car_id = get_car(licence_plate, track_ids)
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
# 函數集合
import csv
import string
import easyocr
# 指定模型存儲目錄
model_storage_directory = "E:\python_works\yolo\my_detect\my_detect\models"
# 定義字符拾取器
reader = easyocr.Reader(['en', 'ch_sim'], model_storage_directory=model_storage_directory)def get_car(licence_plate, vehicle_track_ids):"""將識別車牌分配給已經追蹤的車輛"""global car_indexx1, y1, x2, y2, score, class_id = licence_platefoundIt = False# 遍歷所有車輛for j in range(len(vehicle_track_ids)):xcar1, ycar_1, xcar_2, ycar_2, car_id = vehicle_track_ids[j][:5]# 坐標原點為左上角,橫軸為x軸,縱軸為y軸if x1 > xcar1 and y1 > ycar_1 and x2 < xcar_2 and y2 < ycar_2:car_index = jfoundIt = Truebreakif foundIt:return vehicle_track_ids[car_index][:5]return -1, -1, -1, -1, -1
分配算法是通過判斷車牌 box 是否位于某個車輛 box 中,左上角為坐標原點,水平為 X 軸,豎直為 Y 軸,若分分配失敗,則認為車輛識別或車牌識別出現誤差。
如果分配成功,則裁剪并處理車牌圖像:
# 如果車牌匹配到了車輛if car_id != -1:# 裁剪車牌licence_plate_crop = frame[int(y1):int(y2), int(x1):int(x2), :]# 轉換成灰度圖片licence_plate_crop_gray = cv2.cvtColor(licence_plate_crop, cv2.COLOR_BGR2GRAY)# 調整閾值,若低于64,則設置值為 255,否則設置成 0 ,輸出閾值圖像_,licence_plate_crop_thresh = cv2.threshold(licence_plate_crop_gray, 64, 255, cv2.THRESH_BINARY_INV)# # 顯示灰度圖片和閾值黑白圖像# cv2.imshow('original_crop', licence_plate_crop)# cv2.imshow('threshold', licence_plate_crop_thresh)## cv2.waitKey(0)
然后識別車牌:
# read_licence_plate 識別車牌字符licence_plate_text, licence_plate_core = read_licence_plate(licence_plate_crop_thresh)
def read_licence_plate(licence_plate):"""識別車牌字符串返回格式化字符串和置信度得分"""detections = reader.readtext(licence_plate)for detection in detections:bbox, text, score = detectiontext = text.upper().replace(' ','')print(text)return 0, 0
識別車牌是通過 easyocr 的文字識別進行的,先創建識別器,將閾值黑白圖像輸入函數,去掉空格后打印車牌字符串。
車牌識別后,將所有信息都存儲到 csv 表格中進行查看:
# 存儲車牌信息if licence_plate_text is not None:results[count][car_id] = {'car':{'bbox':[xcar_1, ycar_1, x_car2, y_car2]},'licence_plates':{'bbox':[x1, y1, x2, y2],'text':licence_plate_text,'bbox_score':score,'text_score':licence_plate_core}}# 寫入csv
write_csv(results, './test.csv')
def write_csv(results, output_path):"""Write the results to a CSV file.Args:results (dict): Dictionary containing the results.output_path (str): Path to the output CSV file."""with open(output_path, 'w', newline='') as f: # 使用newline=''避免在Windows上出現多余的空行writer = csv.writer(f)# 寫入表頭writer.writerow(['frame_nmr', 'car_id', 'car_bbox', 'license_plate_bbox','license_plate_bbox_score', 'license_number', 'license_number_score'])for frame_nmr in results.keys():for car_id in results[frame_nmr].keys():print(results[frame_nmr][car_id])if 'car' in results[frame_nmr][car_id] and \'licence_plates' in results[frame_nmr][car_id] and \'text' in results[frame_nmr][car_id]['licence_plates']:car_bbox = results[frame_nmr][car_id]['car']['bbox']license_plate_bbox = results[frame_nmr][car_id]['licence_plates']['bbox']bbox_score = results[frame_nmr][car_id]['licence_plates']['bbox_score']text = results[frame_nmr][car_id]['licence_plates']['text']text_score = results[frame_nmr][car_id]['licence_plates']['text_score']# 寫入數據行writer.writerow([frame_nmr,car_id,'[{} {} {} {}]'.format(*car_bbox),'[{} {} {} {}]'.format(*license_plate_bbox),bbox_score,text,text_score])
最后運行,識別到車牌信息及其對應車輛信息:
????????可以看到已能基本勢必到車牌,并且都是 id 為2的車輛,這說明 id 為 1 的車輛并沒有分配到對應的車牌,或者說根本沒識別到車牌,這是可接受的,因為我的車牌識別模型是自己訓練的,只用了大概 300 張訓練集,識別不準確是正常的。然后 id 為 2 的車輛在這幾個幀中識別到的車牌字符是不一樣的,但是仔細觀察發現 “肅R18”、“京凡168”、“就凡768”,識別到的車牌可以認為是很相像的,可以認為是字符識別誤差或者圖片質量誤差,后期可設計算法匹配到正確的車牌號或者繼續優化模型,以識別到更精確的車牌號,這里不做過多介紹,后期會再發博客探討。
這里再提供下源碼:
main.py:
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
import numpy as np
from ultralytics import YOLO
import cv2
from util import *# 存儲所有信息
results = {}# 加載模型
coco_model = YOLO('yolov8n.pt', task='detect')
license_plate_detector = YOLO('best.pt')# 加載視頻
cap = cv2.VideoCapture('./3.mp4')# 讀取視頻幀
ret = True
count = -1
vehicles = [2, 3, 5, 7]
while ret:count += 1ret, frame = cap.read()# print(frame)if ret and count < 8:# 創建幀編號空字典,方便后面存儲數據results[count] = {}# detections = coco_model(frame)[0]detections = coco_model.track(frame, persist=True) # persist=True 會記住上一幀的信息# detection = detections.boxes.data.tolist()# print(detection)detections_ = []if len(detections) > 0:track_ids = detections[0].boxes.data.cpu().numpy().tolist()for detection in track_ids:x1, y1, x2, y2, score, class_id = detection[:6]if int(class_id) in vehicles:detections_.append([x1, y1, x2, y2, score])else:track_ids = []print("Track IDs:", track_ids)# for detection in detections.boxes.data.tolist():# x1, y1, x2, y2, score ,class_id = detection# # print# if int(class_id) in vehicles:# detections_.append([x1, y1, x2, y2, score])# 汽車跟蹤,返回的一個 追蹤id 列表,每一個車輛都有 id 及其坐標,即使幀變了,同一個目標的 id 不會變# track_ids = mot_tracker.update(np.asarray(detections_))# 檢測車牌licence_plates = license_plate_detector(frame)[0]for licence_plate in licence_plates.boxes.data.tolist():x1, y1, x2, y2, score ,class_id = licence_plate# get_car 分配每一個車牌給車輛xcar_1, ycar_1, x_car2, y_car2, car_id = get_car(licence_plate, track_ids)# 如果車牌匹配到了車輛if car_id != -1:# 裁剪車牌licence_plate_crop = frame[int(y1):int(y2), int(x1):int(x2), :]# 轉換成灰度圖片licence_plate_crop_gray = cv2.cvtColor(licence_plate_crop, cv2.COLOR_BGR2GRAY)# 調整閾值,若低于64,則設置值為 255,否則設置成 0 ,輸出閾值圖像_,licence_plate_crop_thresh = cv2.threshold(licence_plate_crop_gray, 64, 255, cv2.THRESH_BINARY_INV)# # 顯示灰度圖片和閾值黑白圖像# cv2.imshow('original_crop', licence_plate_crop)# cv2.imshow('threshold', licence_plate_crop_thresh)## cv2.waitKey(0)# read_licence_plate 識別車牌字符licence_plate_text, licence_plate_core = read_licence_plate(licence_plate_crop_thresh)# 存儲車牌信息if licence_plate_text is not None:results[count][car_id] = {'car':{'bbox':[xcar_1, ycar_1, x_car2, y_car2]},'licence_plates':{'bbox':[x1, y1, x2, y2],'text':licence_plate_text,'bbox_score':score,'text_score':licence_plate_core}}# 寫入csv
write_csv(results, './test.csv')
util.py:
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
# 函數集合
import csv
import string
import easyocr
# 指定模型存儲目錄
model_storage_directory = "E:\python_works\yolo\my_detect\my_detect\models"
# 定義字符拾取器
reader = easyocr.Reader(['en', 'ch_sim'], model_storage_directory=model_storage_directory)def get_car(licence_plate, vehicle_track_ids):"""將識別車牌分配給已經追蹤的車輛"""global car_indexx1, y1, x2, y2, score, class_id = licence_platefoundIt = False# 遍歷所有車輛for j in range(len(vehicle_track_ids)):xcar1, ycar_1, xcar_2, ycar_2, car_id = vehicle_track_ids[j][:5]# 坐標原點為左上角,橫軸為x軸,縱軸為y軸if x1 > xcar1 and y1 > ycar_1 and x2 < xcar_2 and y2 < ycar_2:car_index = jfoundIt = Truebreakif foundIt:return vehicle_track_ids[car_index][:5]return -1, -1, -1, -1, -1def read_licence_plate(licence_plate):"""識別車牌字符串返回格式化字符串和置信度得分"""detections = reader.readtext(licence_plate)if detections:# 按照置信度排序,取最高分的結果detections.sort(key=lambda x: x[2], reverse=True)bbox, text, score = detections[0]text = text.upper().replace(' ','')print(text)return text, scoreelse:return None, Nonedef write_csv(results, output_path):"""Write the results to a CSV file.Args:results (dict): Dictionary containing the results.output_path (str): Path to the output CSV file."""with open(output_path, 'w', newline='') as f: # 使用newline=''避免在Windows上出現多余的空行writer = csv.writer(f)# 寫入表頭writer.writerow(['frame_nmr', 'car_id', 'car_bbox', 'license_plate_bbox','license_plate_bbox_score', 'license_number', 'license_number_score'])for frame_nmr in results.keys():for car_id in results[frame_nmr].keys():print(results[frame_nmr][car_id])if 'car' in results[frame_nmr][car_id] and \'licence_plates' in results[frame_nmr][car_id] and \'text' in results[frame_nmr][car_id]['licence_plates']:car_bbox = results[frame_nmr][car_id]['car']['bbox']license_plate_bbox = results[frame_nmr][car_id]['licence_plates']['bbox']bbox_score = results[frame_nmr][car_id]['licence_plates']['bbox_score']text = results[frame_nmr][car_id]['licence_plates']['text']text_score = results[frame_nmr][car_id]['licence_plates']['text_score']# 寫入數據行writer.writerow([frame_nmr,car_id,'[{} {} {} {}]'.format(*car_bbox),'[{} {} {} {}]'.format(*license_plate_bbox),bbox_score,text,text_score])# 示例調用
資源下載
車牌識別項目代碼1.0資源-CSDN下載
感謝您的觀看!!!