opencv实战——几十行代码搞定物体识别(0基础!!小白入门学习)

我在前面的内容 发布了opencv的几个模块(人脸识别,颜色,形状)
现在我们来小练手一下

Part 1:准备数据
1.准备coco.names
是 coco 数据集的标签信息,可以根据自己类别进行修改

2.准备训练好的模型(后续我们可以训练自己的模型)
这里是ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt

3.准备权重
frozen_inference_graph.pb

我关于文件我上传到了我的github,我之前写的最新的2021CS231n我也上传到了github
https://github.com/Esaka-13/Easyopencv-ObjectDetection

Part 2: 主函数

import numpy as np
import cv2

thres = 0.5 # Threshold to detect object
nms_threshold = 0.2 #(0.1 to 1) 1 means no suppress , 0.1 means high suppress 
'''
读取摄像头
'''
cap = cv2.VideoCapture(0) #这里也可以换成视频路径
cap.set(cv2.CAP_PROP_FRAME_WIDTH,1000) #width
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,1000) #height
cap.set(cv2.CAP_PROP_BRIGHTNESS,150) #brightness 

'''
读取分类
'''
classNames = []
with open('coco.names','r') as f:
    classNames = f.read().splitlines() #按行拆分
print(classNames)


'''
识别字体颜色
'''
font = cv2.FONT_HERSHEY_PLAIN
Colors = np.random.uniform(0, 255, size=(len(classNames), 3))

'''
构建网络
'''
weightsPath = "frozen_inference_graph.pb"
configPath = "ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"
net = cv2.dnn_DetectionModel(weightsPath,configPath)
net.setInputSize(320,320)
net.setInputScale(1.0/ 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)

'''
识别模块
'''
while True:
    success,img = cap.read()
    classIds, confs, bbox = net.detect(img,confThreshold=thres)
    bbox = list(bbox)
    confs = list(np.array(confs).reshape(1,-1)[0])
    confs = list(map(float,confs))
    #print(type(confs[0]))
    #print(confs)

    indices = cv2.dnn.NMSBoxes(bbox,confs,thres,nms_threshold)
    if len(classIds) != 0:
        for i in indices:
            i = i[0]
            box = bbox[i]
            confidence = str(round(confs[i],2))
            color = Colors[classIds[i][0]-1]
            x,y,w,h = box[0],box[1],box[2],box[3]
            cv2.rectangle(img, (x,y), (x+w,y+h), color, thickness=2)
            cv2.putText(img, classNames[classIds[i][0]-1]+" "+confidence,(x+10,y+20),
                        font,1,color,2)

    cv2.imshow("Output",img)
    if cv2.waitKey(1) & 0xFF == ord('q'):break

opencv实战——几十行代码搞定物体识别(0基础!!小白入门学习)_第1张图片

你可能感兴趣的:(Opencv学习,项目实战,opencv,神经网络,SSD,物体识别,python)