Python Opencv实践 - Yolov3目标检测

本文使用CPU来做运算,未使用GPU。练习项目,参考了网上部分资料。

如果要用TensorFlow做检测,可以参考这里

使用GPU运行基于pytorch的yolov3代码的准备工作_little han的博客-CSDN博客文章浏览阅读943次。记录一下自己刚拿到带独显的电脑,如何成功使用上GPU跑程序的过程。List item环境:win10平台:pycharm代码是基于pytorch的yolo目标检测程序,是B站的一个up分享的,链接如下:https://www.bilibili.com/video/BV14f4y1q7ms1 下载安装cuda以及CUDNN教程参考:深度学习环境搭建(GPU)CUDA安装(完全版)注意:教程中cuda的安装地址选择系统默认值,否则容易出错,即系统找不到路径。在配置路径时,可以先检查自己的cudahttps://blog.csdn.net/weixin_42217041/article/details/118107802

以下是代码,仅供参考:

复制代码
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

#读取支持的类名文件coco.names
classNamesFile = './coco.names'
classNames = []
with open(classNamesFile,'rt') as f:
    classNames = f.read().rstrip('\n').split('\n')
classNum = len(classNames)
print('Total ' + str(classNum) + ' classes: ')
print(classNames)

#置信度阈值
confidenceThreshold = 0.5
#非极大值抑制参数
NMSThreshold = 0.3
#Blob参数
targetWidth = 608
#yolov3的模型配置和权重
modelConfigFile = './yolov3.cfg'
modelWeightsFile = './yolov3.weights'
#读取dnn网络
net = cv.dnn.readNetFromDarknet(modelConfigFile, modelWeightsFile)
#设置网络偏好使用的后端和目标,这里使用CPU
net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)
#获取网络的层名
layerNames = net.getLayerNames()
#print("All layer names:")
#print(layerNames)
#获得未连接的输出层
#https://blog.csdn.net/weixin_43745234/article/details/124628811
outputLayerNames = [layerNames[i - 1] for i in net.getUnconnectedOutLayers()]
print(outputLayerNames)

#读取视频
video = cv.VideoCapture('../../SampleVideos/Party.mp4')
width = video.get(cv.CAP_PROP_FRAME_WIDTH)
height = video.get(cv.CAP_PROP_FRAME_HEIGHT)
fps = video.get(cv.CAP_PROP_FPS)
fourcc = int(video.get(cv.CAP_PROP_FOURCC))
totalFrames = video.get(cv.CAP_PROP_FRAME_COUNT)
print("Video Properties: resolution - (", width, height, ") FPS - "
      , fps, " FOURCC - "
      , chr(fourcc&0xFF), chr((fourcc>>8)&0xFF), chr((fourcc>>16)&0xFF),chr((fourcc>>24)&0xFF)
      , " Frame Count - ", totalFrames)

def DebugOutputs(outputs):
    print("Length of Outputs:")
    print(len(outputs))
    for i in range(len(outputs)):
        print(outputs[i].shape)
        print(outputs[i][0])

def FindObjects(outputs, img, confidenceThreshold):
    h,w,c = img.shape
    print(h,w,c)
    boxes = []
    classIds = []
    confidences = []

    for output in outputs:
        for detection in output:
            scores = detection[5:]
            classId = np.argmax(scores)
            confidence = scores[classId]
            #设置置信度阈值
            if confidence > confidenceThreshold:
                #yolov3的输出层的shape一般是一个二维数组(nBoxes, 85)
                #nBoxes 表示该层输出的边界框的数量
                #85列信息 表示每个边框相关的信息,比如边框位置(中心X,Y,长度和宽度W,H(百分比)),
                #        第五个值为该边框最有可能的物体分类ID号(confidence,置信度)
                #        剩下的80个位置的值是每一类物体的ID和为该物体的概率信息等
                #print(detection)
                #print(detection[0],detection[1],detection[2],detection[3],detection[4])
                boxWidth = int(detection[2] * w)
                boxHeight = int(detection[3] * h)
                boxX,boxY = int(detection[0] * w - boxWidth / 2),int(detection[1] * h - boxHeight / 2)
                boxes.append([boxX,boxY,boxWidth,boxHeight])
                classIds.append(classId)
                confidences.append(float(confidence))

    #print("Detected classes:")
    #for id in classIds:
        #print(classNames[id])
    #非极大值抑制
    indices = cv.dnn.NMSBoxes(boxes, confidences, confidenceThreshold, NMSThreshold)
    #print(indices)
    for index in indices:
        box = boxes[index]
        x,y,w,h = box[0],box[1],box[2],box[3]
        #print(box)
        #绘制边框和文字信息
        cv.rectangle(img, (x,y), (x+w,y+h), (0,255,0),2)
        text = '{}: {:.3f}'.format(classNames[classIds[index]], confidences[index])
        (text_w, text_h), baseline = cv.getTextSize(text, cv.FONT_HERSHEY_SIMPLEX, 0.5, 2)
        cv.rectangle(img, (x, y - text_h - baseline), (x + text_w, y), (255,0,255), -1)
        cv.putText(img, text, (x, y - 5), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)
        
while (True):
    ret,frame = video.read()
    if ret == False:
        break;
    #从Image创建blob
    #参考资料:https://blog.csdn.net/weixin_42216109/article/details/103010206
    blob = cv.dnn.blobFromImage(frame, 1/255, (targetWidth, targetWidth), [0,0,0])
    net.setInput(blob)
    #拿到输出层的结果
    outputs = net.forward(outputLayerNames)
    #DebugOutputs(outputs)
    FindObjects(outputs, frame, confidenceThreshold)
    cv.imshow('VideoPlayer', frame)
    if cv.waitKey(1) & 0xFF == ord('q'):
        break;

video.release()
cv.destroyAllWindows()

运行结果,还不错:

相关推荐
天天爱吃肉82183 分钟前
【跨界封神|周杰伦×王传福(陶晶莹主持):音乐创作与新能源NVH测试,底层逻辑竟完全同源!(新人必看入行指南)】
python·嵌入式硬件·算法·汽车
薛定谔的猫喵喵10 分钟前
基于PyQt5的视频答题竞赛系统设计与实现
开发语言·qt·音视频
岱宗夫up15 分钟前
Python 数据分析入门
开发语言·python·数据分析
码界筑梦坊17 分钟前
325-基于Python的校园卡消费行为数据可视化分析系统
开发语言·python·信息可视化·django·毕业设计
ccLianLian19 分钟前
计算机基础·cs336·损失函数,优化器,调度器,数据处理和模型加载保存
人工智能·深度学习·计算机视觉·transformer
asheuojj20 分钟前
2026年GEO优化获客效果评估指南:如何精准衡量TOP5关
大数据·人工智能·python
多恩Stone21 分钟前
【RoPE】Flux 中的 Image Tokenization
开发语言·人工智能·python
李日灐23 分钟前
C++进阶必备:红黑树从 0 到 1: 手撕底层,带你搞懂平衡二叉树的平衡逻辑与黑高检验
开发语言·数据结构·c++·后端·面试·红黑树·自平衡二叉搜索树
Risehuxyc32 分钟前
备份三个PHP程序
android·开发语言·php
lly20240637 分钟前
PHP Error: 常见错误及其解决方法
开发语言