opencv4.4尝鲜(玩转yolov4检测)

新出的opencv4.4是:

1、支持yolov4检测的,opencv4.3及其之前版本是不支持yolov4的。

2、对比于之前在darknet上使用yolov4检测,直接使用opencv即可,更加方便,并且速度更快。

前置:https://blog.csdn.net/qq_34717531/article/details/107763872

 

一、批量图片yolov4检测,计数并保存:

#引用包
import cv2 as cv
import time
import os

#置信度和nms设置
confThreshold = 0.25
nmsThreshold = 0.4

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/ycc/opencv/yolov4.weights"
cfg="/home/ycc/opencv/yolov4.cfg"
m="/home/ycc/opencv/coco.names"

# 网络设置
net = cv.dnn_DetectionModel(cfg, weights)
#GPU运行
net.setPreferableBackend(cv.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv.dnn.DNN_TARGET_CUDA)
net.setInputSize(608, 608)
net.setInputScale(1.0 / 255)
net.setInputSwapRB(True)
with open(m, "r") as f:
    class_names = [cname.strip() for cname in f.readlines()]
#批量图片检测和保存位置
test_dir = '/home/ycc/darknet-master/ming/'
save_dir = '/home/ycc/opencv/out1/'

#批量加载图像
pics = os.listdir(test_dir)
for im in pics:
    s = time.time()
    img = os.path.join(test_dir,im)
    image = cv.imread(img)

    #模型检测
    classes, confidences, boxes = net.detect(image, confThreshold, nmsThreshold)
    count=0
    #循环画框
    for (classid, score, box) in zip(classes, confidences, boxes):
        #label = '%s: %.2f' % (class_names[classid], score)
        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)
    #num_detections = len(boxes)
    #for (classid, score, box) in zip(classes, scores, boxes):
        #print(classid, score, box)
        #color = COLORS[int(classid) % len(COLORS)]
        #label = "%s : %f" % (class_names[classid[0]], score)
        #cv2.rectangle(image, box, color, 2)
        #cv2.putText(image, label, (box[0], box[1] ), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
        count+=1
    print("一张图检测耗时:%.3f秒" % (time.time() - s))
    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保存转换后的图片

    


opencv4.4尝鲜(玩转yolov4检测)_第1张图片

opencv4.4在进行检测之前是进行了一次初始化,后面的检测时间包括读图,检测,画框。

 

opencv4.4尝鲜(玩转yolov4检测)_第2张图片

 

二、视频检测,计数并保存

import cv2 as cv
import time
import os


#置信度和nms设置
confThreshold = 0.25
nmsThreshold = 0.4

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/ycc/opencv/yolov4-tiny.weights"
cfg="/home/ycc/opencv/yolov4-tiny.cfg"
m="/home/ycc/opencv/coco.names"

# 网络设置
net = cv.dnn_DetectionModel(cfg, weights)
#GPU运行
net.setPreferableBackend(cv.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv.dnn.DNN_TARGET_CUDA)

net.setInputSize(608, 608)
net.setInputScale(1.0 / 255)
net.setInputSwapRB(True)
with open(m, "r") as f:
    class_names = [cname.strip() for cname in f.readlines()]

#保存处理后视频的一套
cap = cv.VideoCapture("1.ts")
#cap = cv.VideoCapture(rtsp)
fps = int(cap.get(cv.CAP_PROP_FPS))
width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
fourcc = cv.VideoWriter_fourcc(*"MJPG")
out = cv.VideoWriter('11.avi', fourcc, int(fps), (int(width),int(height)))


while cv.waitKey(1) < 1:
    (grabbed, frame) = cap.read()#获取每一帧
    if not grabbed:
        exit()
     
    start = time.time()
    classes, confidences, boxes = net.detect(frame, confThreshold, nmsThreshold)
    end = time.time()

    start_drawing = time.time()
    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(frame, box, color1, 1)
        cv.rectangle(frame, (left-1, top - labelSize[1]-5), (left + labelSize[0], top), color, cv.FILLED)
        cv.putText(frame, label, (left, top-5), cv.FONT_HERSHEY_SIMPLEX, 0.5, c,2)
        count+=1
    end_drawing = time.time() 
    fps_label = "FPS: %.2f (excluding drawing time of %.2fms)" % (1 / (end - start),(end_drawing - start_drawing) * 1000)
    cv.putText(frame,"Num: %s" % str(count),(100,150),cv.FONT_HERSHEY_SIMPLEX,2,[255,255,0],3)
    cv.putText(frame, fps_label, (50, 50), cv.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 2)
    out.write(frame)
    cv.imshow("detections", frame)
cap.release()
out.release()
cv.destoryAllWindows()
    

计数:

 

 

你可能感兴趣的:(yolov4-tiny,opencv,yolov4,opencv,python,深度学习)