首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用cv2.dnn_detectionModel方法定制目标检测

如何使用cv2.dnn_detectionModel方法定制目标检测
EN

Stack Overflow用户
提问于 2021-07-21 17:09:09
回答 1查看 60关注 0票数 0

有没有办法从我的脚本中自定义对象检测。如果是,怎么做,我需要安装什么吗?请提供分步指南或视频指南。

不管怎样,我正在用树莓派来做这件事。所以它最好是免费的GPU,并且能够在raspberry pi中做到这一点。

下面这个脚本,它对我来说是可行的,只是我需要检测到不包括在"coco.name","ssd_mobilenet“中的特定东西。

示例:我想检测"SKII碳粉“而不是”瓶子“我希望它是"SKII碳粉”

代码语言:javascript
运行
复制
import cv2
import numpy as np

#Threshold setup
thres = 0.3 # Threshold to detect object
nms_threshold = 0.2

#camera setup
cap = cv2.VideoCapture(0)
#cap.set(3,1080)
#cap.set(4,1920)
#cap.set(10,300)

#standard configuration setting up
classFile = "coco.names"
classNames = []
with open(classFile,"rt") as f:
        classNames = f.read().rstrip("\n").split("\n")

#print(classNames)
configPath = "/home/pi/darknet/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"
weightPath = "/home/pi/darknet/frozen_inference_graph.pb"

net = cv2.dnn_DetectionModel(configPath,weightPath)
net.setInputSize(320,320)
net.setInputScale(1.0/ 120)
net.setInputMean((120, 120, 120))
net.setInputSwapRB(True)

while True:
    success,img = cap.read()

    img = cv2.flip(img, 0)

    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)
    #print(indices)

    for i in indices:
        i = i[0]
        box = bbox[i]
        x,y,w,h = box[0], box[1], box[2], box[3]
        cv2.rectangle(img, (x,y),(x+w,h+y), color=(0,255,0),thickness=1)
        cv2.putText(img,classNames[classIds[i][0]-1].upper(),(box[0]+10,box[1]+30),
            cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),1)

    cv2.imshow("Output",img)
    cv2.waitKey(1)
EN

回答 1

Stack Overflow用户

发布于 2021-07-22 15:54:20

由于基于您想要检测新类的注释,唯一的方法是采用已经检测到所需类(如果有)的预训练检测模型,并查看准确性是否符合您的需求,或者更好地采用在大数据集(例如COCO)上预训练的模型,并在标记为您感兴趣的类的数据集上对其进行微调。你需要这个数据集,根据你的类,你可能会在网上找到一些已经可用的东西,或者你必须收集一个。Tensorflow Object Detection API可能是一个很好的起点,它提供了关于COCO的预训练模型和相对易于使用的API来对新数据集进行微调。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68466862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档