雖然使用visual studio 建立了darknet, 也使用darknet.exe 訓練自己的程式集, 如果因某些原因想使用python 執行 yolov4 的偵測結果. 應如何做?! 本篇只使用現有的source code 達到目的. 並非自行建構所有內容
source code: https://github.com/Asadullah-Dal17/yolov4-opencv-python
1. code--> "download zip", download 後解壓放入一目錄.
2. python 環境只須 opencv
pip install opencv-contrib-python==4.5.3.56
3. python yolov4.py 就可以有結果.
4. 改成自己的weight.
with open('coco.names', 'r') as f:
class_name = [cname.strip() for cname in f.readlines()]
# print(class_name)
net = cv.dnn.readNet('yolov4-tiny.weights', 'yolov4-tiny.cfg')
5. 如果沒有GPU, 或GPU 環境無法動作, 將以下二行, 改為新的二行
#net.setPreferableBackend(cv.dnn.DNN_BACKEND_CUDA)
#net.setPreferableTarget(cv.dnn.DNN_TARGET_CUDA_FP16)
net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv.dnn.DNN_TARGET_OPENCL_FP16)
6. Inference output BOX 的格式為 [left, top, width, height]
7. video file or camera
cap = cv.VideoCapture('input_video.avi') <-- input from video file
cap = cv.VideoCapture(0) #by camera <-- input from 1st camera
#使用此source code 沒有遇到太多問題
#這個sample 只能用video/or camer 當 input , 也可以稍改一下read image 的部分, 就可以從影像檔,讀入