數(shù)據(jù)集地址
該圖像數(shù)據(jù)集包含8000張圖像,兩個類別分別是安全帽與人、以其中200多張圖像為驗證集,其余為訓練集。
模型訓練
準備好數(shù)據(jù)集以后,直接按下面的命令行運行即可:
yolotrainmodel=yolov8s.ptdata=hat_dataset.yamlepochs=50imgsz=640batch=4
導出與測試
模型導出與測試
yolo export model=hat_best.pt format=onnx yolo predict model=hat_best.pt source=./hats
部署推理
轉(zhuǎn)成ONNX格式文件以后,基于OpenVINO-Python部署推理,相關(guān)代碼如下
#ReadIR model=ie.read_model(model="hat_best.onnx") compiled_model=ie.compile_model(model=model,device_name="CPU") output_layer=compiled_model.output(0) capture=cv.VideoCapture("D:/images/video/hat_test.mp4") whileTrue: _,frame=capture.read() ifframeisNone: print("Endofstream") break bgr=format_yolov8(frame) img_h,img_w,img_c=bgr.shape start=time.time() image=cv.dnn.blobFromImage(bgr,1/255.0,(640,640),swapRB=True,crop=False) res=compiled_model([image])[output_layer]#1x84x8400 rows=np.squeeze(res,0).T class_ids=[] confidences=[] boxes=[] x_factor=img_w/640 y_factor=img_h/640 forrinrange(rows.shape[0]): row=rows[r] classes_scores=row[4:] _,_,_,max_indx=cv.minMaxLoc(classes_scores) class_id=max_indx[1] if(classes_scores[class_id]>.25): confidences.append(classes_scores[class_id]) class_ids.append(class_id) x,y,w,h=row[0].item(),row[1].item(),row[2].item(),row[3].item() left=int((x-0.5*w)*x_factor) top=int((y-0.5*h)*y_factor) width=int(w*x_factor) height=int(h*y_factor) box=np.array([left,top,width,height]) boxes.append(box) indexes=cv.dnn.NMSBoxes(boxes,confidences,0.25,0.45) forindexinindexes: box=boxes[index] color=colors[int(class_ids[index])%len(colors)] cv.rectangle(frame,box,color,2) cv.rectangle(frame,(box[0],box[1]-20),(box[0]+box[2],box[1]),color,-1) cv.putText(frame,class_list[class_ids[index]],(box[0],box[1]-10),cv.FONT_HERSHEY_SIMPLEX,.5,(0,0,0)) end=time.time() inf_end=end-start fps=1/inf_end fps_label="FPS:%.2f"%fps cv.putText(frame,fps_label,(20,45),cv.FONT_HERSHEY_SIMPLEX,1,(0,0,255),2) cv.imshow("YOLOv8hatDetection",frame) cc=cv.waitKey(1) ifcc==27: break cv.destroyAllWindows()
-
模型
+關(guān)注
關(guān)注
1文章
3462瀏覽量
49782 -
數(shù)據(jù)集
+關(guān)注
關(guān)注
4文章
1220瀏覽量
25183 -
命令行
+關(guān)注
關(guān)注
0文章
80瀏覽量
10505
原文標題:YOLOv8自定義數(shù)據(jù)集訓練實現(xiàn)安全帽檢測
文章出處:【微信號:CVSCHOOL,微信公眾號:OpenCV學堂】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
基于YOLOv8實現(xiàn)自定義姿態(tài)評估模型訓練

RK3399pro實現(xiàn)安全帽識別
ZLG安全帽佩戴檢測方案的解讀
使用YOLOv8做目標檢測和實例分割的演示
YOLOv8自定義數(shù)據(jù)集訓練到模型部署推理簡析
TensorRT 8.6 C++開發(fā)環(huán)境配置與YOLOv8實例分割推理演示

YOLOv8實現(xiàn)任意目錄下命令行訓練

基于YOLOv8的自定義醫(yī)學圖像分割

如何基于深度學習模型訓練實現(xiàn)圓檢測與圓心位置預測

YOLOv8實現(xiàn)旋轉(zhuǎn)對象檢測

RV1126 yolov8訓練部署教程

評論