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()

运行结果,还不错:


http://www.niftyadmin.cn/n/5238435.html

相关文章

谈谈 .NET8 平台中对 LiteDB 的 CRUD 操作

哪个啥!纯 C# 编写的 LiteDB 你还不会操作? LiteDB 简介LiteDB 安装同步版 LiteDB异步版 LiteDB.Async LiteDB StudioLiteDB CRUD 操作举例LiteDB vs SQLite 对比1、谈谈 sqlite 和 litedb 的 ACID 事务支持?2、谈谈 sqlite 和 litedb 的稳定…

HTML,CSS,JS前端三大件总结

前端三大件总结:HTML、CSS、JavaScript 在前端开发中,HTML、CSS和JavaScript是三大核心技术,它们共同构成了网页的基础结构和交互功能。下面将对这三大技术进行详细的介绍和总结。 一、HTML HTML(HyperText Markup Language&am…

梯度上升和随机梯度上升

目录 梯度上升算法: 代码: 随机梯度上升算法: 代码: 实验: 做图代码: 疑问: 1.梯度上升算法不适应大的数据集,改用随机梯度上升更合适。 2.改进过的随机梯度算法&#xff0…

MySQL笔记-第04章_运算符

视频链接:【MySQL数据库入门到大牛,mysql安装到优化,百科全书级,全网天花板】 文章目录 第04章_运算符1. 算术运算符2. 比较运算符3. 逻辑运算符4. 位运算符5. 运算符的优先级拓展:使用正则表达式查询 第04章_运算符 …

深度学习设计基于Tensorflow卷积神经网络猫的品种识别系统

欢迎大家点赞、收藏、关注、评论啦 ,由于篇幅有限,只展示了部分核心代码。 文章目录 一项目简介 二、功能三、系统四. 总结 一项目简介 基于Tensorflow卷积神经网络的猫的品种识别系统可以用于自动识别猫的品种类型。下面我将为您介绍一下这个系统的基本…

基于Amazon Bedrock介绍大语言模型

本文基于亚马逊云科技推出的大语言模型与生成式AI的全家桶:Bedrock对大语言模型进行介绍。大语言模型指的是具有数十亿参数(B)的预训练语言模型(例如:GPT-3, Bloom, LLaMA)。这种模型可以用于各种自然语言处理任务&…

基于Go语言实现简易Web应用

目录 前言Go语言特点写在使用Go语言实现Web应用前面创建Web服务器声明一个结构体操作加入中间件的使用使用静态文件服务器最后 前言 在编程语言中,近几年问世的几个新语言都是非常不错的,比如Go、Python、 Rust等等。其中,Go语言(Golang)作…

Attributes should be specified via @SpringBootApplication 和 idea运行时提示内存不足

警告信息 Attributes should be specified via SpringBootApplication 因为:SpringBootApplication注解中已经包含EnableAutoConfiguration注解,不需要重复定义EnableAutoConfiguration注解。 内存不足 参考文章:【java.lang.OutOfMemoryE…