opencv4.4使用yolov3和yolov4模型檢測目標


yolov3:

# -*- coding: utf-8 -*
import numpy as np
import cv2 as cv
import os
import time

yolo_dir = '/home/ubuntu/model/yolo4-tiny'  # YOLO文件路徑
weightsPath = os.path.join(yolo_dir, 'yolov3-test.weights')  # 權重文件
configPath = os.path.join(yolo_dir, 'yolov3-test.cfg')  # 配置文件
labelsPath = os.path.join(yolo_dir, 'test.names')  # label名稱
imgPath = os.path.join(yolo_dir, 'test.jpg')  # 測試圖像
CONFIDENCE = 0.25  # 過濾弱檢測的最小概率
THRESHOLD = 0.2  # 非最大值抑制閾值

#批量圖片檢測和保存位置
test_dir = '/home/ubuntu/model/yolo4-tiny/imgs'
save_dir = '/home/ubuntu/model/yolo4-tiny/imgsResultV3'

# 加載網絡、配置權重
net = cv.dnn.readNetFromDarknet(configPath, weightsPath)  # #  利用下載的文件


#批量加載圖像
pics = os.listdir(test_dir)
start = time.time()
minTime = 10;
maxTime = 0;
for im in pics:
# 加載圖片、轉為blob格式、送入網絡輸入層
    s = time.time()
    imgPath = os.path.join(test_dir,im)
    img = cv.imread(imgPath)
    blobImg = cv.dnn.blobFromImage(img, 1.0/255.0, (416, 416), None, True, False)   # # net需要的輸入是blob格式的,用blobFromImage這個函數來轉格式
    net.setInput(blobImg)  # # 調用setInput函數將圖片送入輸入層

    # 獲取網絡輸出層信息(所有輸出層的名字),設定並前向傳播
    outInfo = net.getUnconnectedOutLayersNames()  # # 前面的yolov3架構也講了,yolo在每個scale都有輸出,outInfo是每個scale的名字信息,供net.forward使用
    layerOutputs = net.forward(outInfo)  # 得到各個輸出層的、各個檢測框等信息,是二維結構。
    # 拿到圖片尺寸
    (H, W) = img.shape[:2]
    # 過濾layerOutputs
    # layerOutputs的第1維的元素內容: [center_x, center_y, width, height, objectness, N-class score data]
    # 過濾后的結果放入:
    boxes = [] # 所有邊界框(各層結果放一起)
    confidences = [] # 所有置信度
    classIDs = [] # 所有分類ID

    # # 1)過濾掉置信度低的框框
    for out in layerOutputs:  # 各個輸出層
        for detection in out:  # 各個框框
            # 拿到置信度
            scores = detection[5:]  # 各個類別的置信度
            classID = np.argmax(scores)  # 最高置信度的id即為分類id
            confidence = scores[classID]  # 拿到置信度

            # 根據置信度篩查
            if confidence > CONFIDENCE:
                box = detection[0:4] * np.array([W, H, W, H])  # 將邊界框放會圖片尺寸
                (centerX, centerY, width, height) = box.astype("int")
                x = int(centerX - (width / 2))
                y = int(centerY - (height / 2))
                boxes.append([x, y, int(width), int(height)])
                confidences.append(float(confidence))
                classIDs.append(classID)

    # # 2)應用非最大值抑制(non-maxima suppression,nms)進一步篩掉
    idxs = cv.dnn.NMSBoxes(boxes, confidences, CONFIDENCE, THRESHOLD) # boxes中,保留的box的索引index存入idxs
    # 得到labels列表
    with open(labelsPath, 'rt') as f:
        labels = f.read().rstrip('\n').split('\n')
    # 應用檢測結果
    np.random.seed(42)
    COLORS = np.random.randint(0, 255, size=(len(labels), 3), dtype="uint8")  # 框框顯示顏色,每一類有不同的顏色,每種顏色都是由RGB三個值組成的,所以size為(len(labels), 3)
    if len(idxs) > 0:
        for i in idxs.flatten():  # indxs是二維的,第0維是輸出層,所以這里把它展平成1維
            (x, y) = (boxes[i][0], boxes[i][1])
            (w, h) = (boxes[i][2], boxes[i][3])

            color = [int(c) for c in COLORS[classIDs[i]]]
            cv.rectangle(img, (x, y), (x+w, y+h), color, 2)  # 線條粗細為2px
            text = "{}: {:.4f}".format(labels[classIDs[i]], confidences[i])
            cv.putText(img, text, (x, y-5), cv.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)  # cv.FONT_HERSHEY_SIMPLEX字體風格、0.5字體大小、粗細2px
    print(imgPath+"檢測耗時: {:.6f} seconds".format(time.time() - s))  # # 可以打印下信息
    times = time.time() - s
    if(minTime>times):
        minTime = times
    if(maxTime<times):
        maxTime = times
    cv.imwrite(os.path.join(save_dir,im),img)

print("yolov3檢測100張圖總共耗時:%.6f秒" % (time.time() - start))
print("耗時最長為:%.6f秒" % (maxTime))
print("耗時最短為:%.6f秒" % (minTime))
 
 
yolov4:
 
# -*- coding: utf-8 -*
import cv2 as cv
import time
import os
 
#置信度和nms設置
confThreshold = 0.25
nmsThreshold = 0.2
 
class_names = []#初始化一個列表以存儲類名
COLORS = [(0, 255, 255),(0,0,255), (255, 255, 0),(0, 255, 0), (255, 0, 0)]#顏色
color=(0,255,0)
color1=(0,0,255)
c=(0, 0, 0)

#模型參數
weights="/home/ubuntu/model/yolo4-tiny/yolov4-tiny.weights"
cfg="/home/ubuntu/model/yolo4-tiny/yolov4-tiny.cfg"
m="/home/ubuntu/model/yolo4-tiny/coco.names"
#批量圖片檢測和保存位置
test_dir = '/home/ubuntu/model/yolo4-tiny/imgs'
save_dir = '/home/ubuntu/model/yolo4-tiny/imgsResultV4'


# 網絡設置
net = cv.dnn_DetectionModel(cfg, weights)
#GPU運行
net.setPreferableBackend(cv.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv.dnn.DNN_TARGET_CUDA)
net.setInputSize(416, 416)
net.setInputScale(1.0 / 255)
net.setInputSwapRB(True)
with open(m, "r") as f:
    class_names = [cname.strip() for cname in f.readlines()]
 
#批量加載圖像路徑
pics = os.listdir(test_dir)
start = time.time()
minTime = 10;
maxTime = 0;
inImgTime=0;
testImgTime=0;
for im in pics:
    s = time.time()
    #加載圖片
    img = os.path.join(test_dir,im)
    image = cv.imread(img)
    
    sTest = time.time()
    inImg = sTest-s
    inImgTime +=inImg
    #模型檢測
    classes, confidences, boxes = net.detect(image, confThreshold, nmsThreshold)
    sTestEnd = time.time()
    testImg = sTestEnd-sTest
    testImgTime+= testImg
    count=0
    #循環畫框
    for (classid, score, box) in zip(classes, confidences, boxes):
        label = "%s(%2.0f%%)" % (class_names[classid[0]], score*100)#標簽置信度
        labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
        left, top, width, height = box
        cv.rectangle(image, box, color1, 1)
        cv.rectangle(image, (left-1, top - labelSize[1]-5), (left + labelSize[0], top), color, cv.FILLED)
        cv.putText(image, label, (left, top-5), cv.FONT_HERSHEY_SIMPLEX, 0.5, c,2)
        count+=1
    times = time.time() - s
    if(minTime>times):
        minTime = times
    if(maxTime<times):
        maxTime = times
    print(img+"加載圖片耗時:%.6f秒" % inImg)
    print(img+"檢測圖片耗時:%.6f秒" % testImg)
    print(img+"檢測耗時:%.3f秒" % times)
    cv.putText(image,"Num: %s" % str(count),(25,50),cv.FONT_HERSHEY_SIMPLEX,1,[255,255,0],2)
    cv.imwrite(os.path.join(save_dir,im),image)#用原始圖像名im保存轉換后的圖片

print("yolov4檢測6張圖總共耗時:%.3f秒" % (time.time() - start))
print("耗時最長為:%.3f秒" % (maxTime))
print("耗時最短為:%.3f秒" % (minTime))
print("加載圖片總共耗時:%.6f秒" % (inImgTime))
print("檢測圖片總共耗時:%.6f秒" % (testImgTime))



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM