重要Functions
3.1 使用dnn模組讀取模型(預設以CPU推論)
# 讀取模型架構與權重
def initNet():
# 可更換成yolov4-tiny.cfg與yolov4-tiny.weights
CONFIG = f'{pathlib.Path(__file__).parent.resolve()}\yolov4.cfg'
WEIGHT = f'{pathlib.Path(__file__).parent.resolve()}\yolov4.weights'
net = cv2.dnn.readNet(CONFIG, WEIGHT)
model = cv2.dnn_DetectionModel(net)
# 若以yolov4-tiny進行物件偵測,預設size=(416, 416)
model.setInputParams(size=(608, 608), scale=1/255.0)
model.setInputSwapRB(True)
return model
3.2 物件偵測:
# 物件偵測
def nnProcess(image, model):
classes, confs, boxes = model.detect(image, 0.3, 0.2)
return classes, confs, boxes
3.3 在原圖上畫出物件偵測框
# 在原圖上畫出被偵測的物件
def drawBox(image, classes, confs, boxes):
image1 = image.copy()
for (classid, conf, box) in zip(classes, confs, boxes):
x, y, w, h = box
cv2.rectangle(image1, (x, y), (x + w, y + h), (180, 0, 0), 3)
return image1
3.4 將推論結果格式化輸出
# Output輸出格式化
def predict(classes, confs, boxes):
# 讀取標籤類別
with open('./classes.txt') as f:
labels = f.read().split('\n')
predit_list = []
# 格式化推論結果
for classid, conf, box in zip(classes, confs, boxes):
predict = {'xmin': float(box[0]), 'ymin': float(box[1]),
'xmax': float(box[0])+float(box[2]),
'ymax': float(box[1])+float(box[3]),
'result': labels[classid], 'conf': float(conf)}
predit_list.append(predict)
return predit_list
完整程式碼
import numpy as np
import cv2
import pathlib
# 讀取中文路徑圖檔(圖片讀取為BGR)
def cv_imread(image_path):
image = cv2.imdecode(np.fromfile(image_path, dtype=np.uint8), -1)
image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
return image
# 顯示圖檔
def show_img(name, image):
cv2.imshow(name, image)
cv2.waitKey(0)
# 讀取模型架構與權重
def initNet():
# 可更換成yolov4-tiny.cfg與yolov4-tiny.weights
CONFIG = f'{pathlib.Path(__file__).parent.resolve()}\yolov4.cfg'
WEIGHT = f'{pathlib.Path(__file__).parent.resolve()}\yolov4.weights'
net = cv2.dnn.readNet(CONFIG, WEIGHT)
model = cv2.dnn_DetectionModel(net)
# 若以yolov4-tiny進行物件偵測,預設size=(416, 416)
model.setInputParams(size=(608, 608), scale=1/255.0)
model.setInputSwapRB(True)
return model
# 物件偵測
def nnProcess(image, model):
classes, confs, boxes = model.detect(image, 0.3, 0.2)
return classes, confs, boxes
# Output輸出格式化
def predict(classes, confs, boxes):
# 讀取標籤類別
with open('./classes.txt') as f:
labels = f.read().split('\n')
predit_list = []
# 格式化推論結果
for classid, conf, box in zip(classes, confs, boxes):
predict = {'xmin': float(box[0]), 'ymin': float(box[1]),
'xmax': float(box[0])+float(box[2]),
'ymax': float(box[1])+float(box[3]),
'result': labels[classid], 'conf': float(conf)}
predit_list.append(predict)
return predit_list
# 在原圖上畫出被偵測的物件
def drawBox(image, classes, confs, boxes):
image1 = image.copy()
for (classid, conf, box) in zip(classes, confs, boxes):
x, y, w, h = box
cv2.rectangle(image1, (x, y), (x + w, y + h), (180, 0, 0), 3)
return image1
if __name__ == '__main__':
start = time.time()
model_YOLOV4 = initNet()
image = cv_imread(f'{pathlib.Path(__file__).parent.resolve()}\\od_test.jpg')
classes, confs, boxes = nnProcess(image, model_YOLOV4)
result = predict(classes, confs, boxes)
end = time.time()
for i in result:
print(i)
print('YOLOv4物件偵測共花費 {} 秒'.format(end - start))
final_image = drawBox(image, classes, confs, boxes)
show_img('final_image', final_image)
執行結果
5.1 YOLOv4-tiny
物件偵測結果
推論速度與信心水準
5.2 YOLOv4
物件偵測結果
推論速度與信心水準
讓我們繼續看下去...
請問可以提供本文中的標籤類別檔案:classes.txt?
謝謝!
可前往以下網頁,將coco-classes.txt檔案下載,並更名為classes.txt,即可使用。
https://github.com/matlab-deep-learning/pretrained-yolo-v4/blob/main/src/+helper/coco-classes.txt