【问题标题】:How to customize object detection using cv2.dnn_detectionModel method如何使用 cv2.dnn_detectionModel 方法自定义对象检测
【发布时间】:2021-09-28 16:10:04
【问题描述】:

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

无论如何,我正在使用 Raspberry Pi 来做这件事。所以它最好是免费的 GPU,并且能够在树莓派中做到这一点。

在这个脚本下面,它对我有用,只是我需要检测“coco.name”、“ssd_mobilenet”中未包含的特定内容。

示例:我想检测“SKII Toner”而不是“bottle”我希望它是“SKII Toner”

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)

【问题讨论】:

  • 我不完全理解你的问题..你想检测一个新的类“SKII Toner”(我什至不知道它是什么)?
  • @rok 早安,是的,我想检测新的类,而不是使用 coco.names。

标签: raspberry-pi python-3.7 object-detection cv2


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-05-09
    • 1970-01-01
    • 2019-12-11
    • 2018-07-03
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    相关资源
    最近更新 更多