其他分享
首页 > 其他分享> > Opencv Q&A_3

Opencv Q&A_3

作者:互联网

2022/03/07

基于opencv实现的物体识别

 

准备工作

使用的是训练好的模型,需提前准备好:

配置文件:ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt

配重文件:frozen_inference_graph.pb

coco数据集的标注文件:coco.names

https://github.com/Fafa-DL/Opencv-project/tree/main/CVZone/05%20Object%20Detection%20OpenCV%20(MobileNet%20SSD)

 

代码

import cv2 as cv


coco_path = 'D:\work\\automation\Skill\Python\pythonWORK\cv\object_detect\Object_Detection_Files\coco.names'
# img_path = 'D:\work\\automation\Skill\Python\pythonWORK\cv\material\lena.png'
config_path = 'D:\work\\automation\Skill\Python\pythonWORK\cv\object_detect\Object_Detection_Files\ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weights_path = 'D:\work\\automation\Skill\Python\pythonWORK\cv\object_detect\Object_Detection_Files\\frozen_inference_graph.pb'
detect_thres = 0.6  #检测阈值 越高越严格
nms_thres = 0.3     #nms阈值,越低越严格


with open(coco_path,'r') as f:      #读取coco数据集
    className = f.read()
    className = list(className.split('\n'))

###以下配置后续深入学习,当前仅做使用即可
net = cv.dnn_DetectionModel(weights_path,config_path)   #创建dnn_detect对象(权重路径,配置路径)
net.setInputSize(320,320)
net.setInputScale(1.0/127.5)
net.setInputMean((127.5,127.5,127.5))
net.setInputSwapRB(True)



cap = cv.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
cap.set(10,150) #亮度



while True:
    flag, img = cap.read()
    classIds, confs, bbox = net.detect(img, confThreshold=0.5)  # 返回依次为:识别结果序号,准确率,左上角坐标和右下角坐标
    if len(classIds) != 0:      #当检测数据不为空时才进行数据处理
        bbox = bbox.tolist()    #numpy.array转化为list
        confs = list(map(float, confs.flatten()))  #多维降一维,numpy.float全部映射为float,map转为list
        classIds_t = cv.dnn.NMSBoxes(bbox,confs,detect_thres,nms_thres)     #非极大抑制留下识别最好的,输出序号(坐标,识别准确率)

        for i in classIds_t:
            i = i[0]        #classIds_t是每个元素都带有[]的列表,去除列表
            cv.rectangle(img, bbox[i], color=(0, 255, 0), thickness=2)
            cv.putText(img, className[classIds[i][0] - 1] + '  ' + str(round(confs[i] * 100, 2)), (bbox[i][0] + 2, bbox[i][1] + 25),
                       cv.FONT_HERSHEY_SIMPLEX, 1,color=(0, 255, 0), thickness=2)   #classIds第一号元素序号为1,需减1
    cv.imshow('img', img)
    cv.waitKey(1)

 

运行效果

 

 

遇到的问题

Q1:dnn_DetectionModel()函数的配置

A1:以后的学习补上,本次仅做应用

Q2:net.detect()的输出成分结构

A2:

  第一个输出classIds:识别结果对应coco.names中的序号(实际应该-1),为二维numpy.array结构(每个元素都单独一个子数组如[[1][2][3]])

  第二个输出confs:识别准确率,为二维numpy.array结构

  第三个输出bbox:识别框的坐标数据,依次为左上角坐标,右下角坐标四个数据,为二维numpy.array结构

 

 

标签:detect,img,classIds,Opencv,bbox,coco,cv
来源: https://www.cnblogs.com/toriyung/p/15977935.html