► 前言 現在許多停車場使用車牌識別系統,停車時需要先使用攝像頭拍攝影像,透過AI檢測車牌再進行識別,本篇博文介紹如何訓練車牌檢測模型。 ► 程式碼講解 在Kaggle網站內搜尋Car License Plate Detection並下載,解壓縮資料夾,更改名稱為“CarLicensePlateDetection”,將資料夾上傳至Google雲端硬碟,開啟Colab並設定為GPU執行,連結Google雲段硬碟。 先建立訓練資料夾,程式碼如下: import numpy as np import pandas as pd import os import numpy
os.mkdir("/content/plate_train_data") os.mkdir("/content/plate_train_data/images") os.mkdir("/content/plate_train_data/labels") os.mkdir("/content/plate_train_data/images/train") os.mkdir("/content/plate_train_data/images/val") os.mkdir("/content/plate_train_data/labels/train") os.mkdir("/content/plate_train_data/labels/val")
將 xml 文件轉換為 txt 文件,因為 Yolo需要使用歸一化邊界框的 txt 文件,格式為「class id, x, y, width, height」,資料呈現如下: 0 0.39375 0.34615384615384615 0.0975 0.05263157894736842 0 0.64375 0.35020242914979755 0.0625 0.06072874493927125 轉換程式碼如下: import xml.etree.ElementTree as ET from xml.dom.minidom import parse
path = "/content/drive/MyDrive/AI/dataset/CarLicensePlateDetection/annotations" classes = {"licence":0}
for annotations in os.listdir(path): dom = parse(os.path.join(path,annotations)) root = dom.documentElement filename = ".txt".join(root.getElementsByTagName("filename")[0].childNodes[0].data.split(".png")) image_width = root.getElementsByTagName("width")[0].childNodes[0].data image_height = root.getElementsByTagName("height")[0].childNodes[0].data with open("/content/plate_train_data/labels/train/"+filename,"w") as r: for items in root.getElementsByTagName("object") : name = items.getElementsByTagName("name")[0].childNodes[0].data xmin = items.getElementsByTagName("xmin")[0].childNodes[0].data ymin = items.getElementsByTagName("ymin")[0].childNodes[0].data xmax = items.getElementsByTagName("xmax")[0].childNodes[0].data ymax = items.getElementsByTagName("ymax")[0].childNodes[0].data x_center_norm = ((int(xmin)+int(xmax)) / 2 ) / int(image_width) y_center_norm = ((int(ymin)+int(ymax))/2) / int(image_height) width_norm = ((int(xmax)-int(xmin))/int(image_width)) height_norm = ((int(ymax)-int(ymin))/int(image_height)) r.write(str(classes[name])+" ") r.write(str(x_center_norm)+" ") r.write(str(y_center_norm)+" ") r.write(str(width_norm)+" ") r.write(str(height_norm)+"\n")
複製CarLicensePlateDetection內的image至plate_train_data/images/train import shutil path = "/content/drive/MyDrive/AI/dataset/CarLicensePlateDetection/images"
for images in os.listdir(path): image_path = os.path.join(path,images) shutil.copy(image_path,"/content/plate_train_data/images/train/"+images) 將訓練資料集亂數分割120至驗證資料集內 from random import shuffle from glob import glob import random import shutil
random.seed(42) files = glob("/content/plate_train_data/images/train/*.png") shuffle(files) txt_train_path = "/content/plate_train_data/labels/train/" img_train_path = "/content/plate_train_data/images/train/" txt_val_path = "/content/plate_train_data/labels/val/" img_val_path = "/content/plate_train_data/images/val/" for i in files[:120]: filename = i.split("/")[-1] shutil.move(txt_train_path+filename.replace(".png",".txt"),\ txt_val_path+filename.replace(".png",".txt")) shutil.move(img_train_path+filename,img_val_path+filename)
建立yaml檔案,train後面為train資料集位置 %cd /content !echo "train: /content/plate_train_data/images/train" > licence.yaml !echo "val: /content/plate_train_data/images/val" >> licence.yaml !echo "nc : 1" >> licence.yaml !echo "names: ['licence']" >> licence.yaml
下載Yolov5,並安裝相關套件 %cd /content/ %rm -rf yolov5 !git clone https://github.com/ultralytics/yolov5 %cd yolov5 !pip install -r requirements.txt
開始訓練 --img為圖像大小 --batch批次量 --epochs循環幾次 --data訓練資料的yaml檔案 訓練指令如下:
!python train.py --weights yolov5s.pt --cfg models/yolov5s.yaml --img 416 --batch 48 --epochs 40 --data /content/licence.yaml --cache
P(Precision)為正確數 / 預測總數 R(Recall)為預測正確數 / 真實總數 mAP(mean Average Precision):每個類的AP值的平均數,mAP越高,推論準確度越好。 mAP50-95表示在不同區段IoU闊值,從0.5~0.95,間隔0.05
訓練完成後顯示推論結果 from IPython.display import Image Image('/content/yolov5/runs/train/exp/val_batch0_labels.jpg')
►小結 以上流程是使用Yolov5訓練車牌檢測的流程,當檢測出車牌就可以裁切進行文字OCR,讓客戶繳費時輸入自己的車牌,點選自己的車子照片進行繳費,之後有空再來整理發文。 ► 參考資料 Car License Plate Detection |