【问题标题】:how to detect a certain object in python [closed]如何在python中检测某个对象[关闭]
【发布时间】:2022-07-08 23:05:42
【问题描述】:

所以我试图从 yt 做一个对象检测教程。我找到了一个叫 murtaza 的人,我想试试他的代码。这是视频; https://www.youtube.com/watch?v=diWDgKcH3E0 ; 没有错;它有效,但我想知道我是否只能检测框架中的某个对象而不是各种对象;所以就像我不希望它检测“coco.names”文件中的各种东西,但只有一个;知道我该怎么做吗?我正在使用 pycharm;而且因为他使用的编码策略非常先进;我不知道如何让代码做到这一点;所以: . 只检测一个对象,而不是视频流中的所有对象(网络摄像头) 。文件: https://github.com/sidpro-hash/Object-Detection - 仅下载: -“可可名称” -“frozen_inference_graph.pb” -“ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt” 这是代码:

    import cv2
import cvzone
thres = 0.5 #to detect objects

#img = cv2.imread('cat3.jpg')
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)

classNames = []
classFile = 'coco.names'
with open(classFile, 'rt') as f:
    classNames = f.read().rstrip('\n').split('\n')

configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weigthsPath = 'frozen_inference_graph.pb'

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

while True:
    _, img = cap.read()
    classIds, confs, bbox = net.detect(img, confThreshold=0.5)
    print(classIds, bbox)

    if len(classIds) != 0:
        for classId, confidence, box in zip(classIds.flatten(), confs.flatten(), bbox):
            #cv2.rectangle(img, box, color=(0,0,255), thickness=3)
            cvzone.cornerRect(img, box)
            cv2.putText(img, classNames[classId-1].upper(), (box[0]+10,box[1]+30),
                        cv2.FONT_HERSHEY_COMPLEX,1,(0,0,0), 2)
            cv2.putText(img, str(round(confidence*100, 2)), (box[0]+200,box[1]+30),
                        cv2.FONT_HERSHEY_COMPLEX,1,(0,0,0), 2)
    cv2.imshow("output", img)
    cv2.waitKey(1)

【问题讨论】:

  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: python opencv


【解决方案1】:

简短回答:是的,您可以使用简单的逻辑运算来检查类 ID,然后仅显示属于该类的边界框(如果您不想重新训练具有单个输出层的模型)

但是,要正确回答问题并使用最佳实际场景;让我们来看看单类和多类对象检测的区别是什么,是否值得重新训练。

就模型架构而言,只有不同的层是全连接层(当然,前提是您使用相同的主干进行特征提取。)单类目标检测器只有一个回归层集,而多类目标检测器只有一个回归层集目标检测模型有回归层+softmax分类器来检测和分类多类标签。

那么你应该使用哪一个?

在某些情况下(即使您尝试检测单个类),其中不同的对象看起来非常相似,将它们分成多个类并在多类检测器中训练它们将减少给出误报结果的机会你想要的课程。您可能会认为这种情况就像您现在不知道猫是什么一样,并且由于它们与狗相似,因此您很容易将猫误认为是狗。但如果你现在把猫和狗分开了,把它们误认为是彼此的机会就很低了。

几乎一直以来,深度学习问题都没有通用的解决方案。但是现在了解它们背​​后的理论将引导您走向成功。

如果您想将该模型 (ssd_mobilenet_v3) 转换为单类目标检测器,您只需remove fully connected layer, add your own then train as usual;

from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import Input, Flatten, Dense, Dropout
from tensorflow.keras.models import Model

# 1 for single-class detection
NUMBER_OF_CLASSES = 1

# Include_top False means you exclude FC (Fully Connected) Layers of the model
model = MobileNetV2(include_top=False, input_tensor=Input(150, 150, 3))

# This freezes all layers of the MobileNet to not update it during training process
model.trainable = False

# We flatten the output layer so we can add our own (With 1 class)
flatten = model.output
flatten = Flatten()(flatten)

# construct a fully-connected layer header to output the predicted
# bounding box coordinates
bboxHead = Dense(128, activation="relu")(flatten)
bboxHead = Dense(64, activation="relu")(bboxHead)
bboxHead = Dense(32, activation="relu")(bboxHead)
bboxHead = Dense(4, activation="sigmoid",
                 name="bounding_box")(bboxHead)

# construct a second fully-connected layer head, this one to predict
# the class label
softmaxHead = Dense(512, activation="relu")(flatten)
softmaxHead = Dropout(0.5)(softmaxHead)
softmaxHead = Dense(512, activation="relu")(softmaxHead)
softmaxHead = Dropout(0.5)(softmaxHead)
softmaxHead = Dense(NUMBER_OF_CLASSES, activation="softmax",
                    name="class_label")(softmaxHead)

model = Model(
    inputs=model.input,
    outputs=(bboxHead, softmaxHead)
)

## Training........

【讨论】:

  • 嘿@Ege Yıldırım,什么类型的张量流?我正在使用pycharm,所以我尝试安装库;但它仍然给我一个错误,说它没有在 tensorflow 文件中找到 keras ..
  • @Keshav.h 试试 tensorflow.python.keras :) 我很欣赏你在那个年纪的工作,这是对你未来工作的一个重要提示 = 学习如何有效地使用谷歌!祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
相关资源
最近更新 更多