iT邦幫忙

2022 iThome 鐵人賽

DAY 9
0

練習用圖

  1. edge.jpg

內容

  1. 下載YOLOv4-tiny權重YOLOv4-tiny之cfg檔

  2. 下載YOLOv4權重YOLOv4之cfg檔

  3. 重要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 物件偵測:

    • 信心水準(conf)閾值為0.3
    • NMS為0.2,用來篩除重複框選同一物件的Bounding Box。
    • 程式碼
    # 物件偵測
    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
    
  4. 完整程式碼

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)
  1. 執行結果

    5.1 YOLOv4-tiny

    • 物件偵測結果

    • 推論速度與信心水準

    5.2 YOLOv4

    • 物件偵測結果

    • 推論速度與信心水準


小結

  1. 下一站,我們前往「YOLOv4解析(一)」。

讓我們繼續看下去...


參考資料

  1. AlexeyAB/darknet
  2. 【第31天】番外篇-Windows + YOLOV4 本地端訓練

上一篇
《第8天》OpenCV邊緣偵測
下一篇
《第10天》YOLOv4解析(一)
系列文
Object Detection and Image Processing with Python30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
DeanYu
iT邦新手 4 級 ‧ 2023-05-18 15:07:38

請問可以提供本文中的標籤類別檔案:classes.txt?
謝謝!

可前往以下網頁,將coco-classes.txt檔案下載,並更名為classes.txt,即可使用。
https://github.com/matlab-deep-learning/pretrained-yolo-v4/blob/main/src/+helper/coco-classes.txt

我要留言

立即登入留言