所以我试着做一个yt的对象检测教程。我找到了一个叫murtaza的人我想试试他的密码。这是视频;https://www.youtube.com/watch?v=diWDgKcH3E0;它没有什么问题;它能正常工作,但我想知道我是否只能检测到帧中的某个对象,而不是所有类型的对象;所以,我不希望它检测到"coco.names“文件中的所有东西,但只有一个;我知道怎么做吗?因为他使用的编码策略非常先进,所以我不知道如何使代码做到这一点;因此:在视频流(网络摄像头) -"ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt“文件中,.detect只使用一个对象,而不是所有类型的对象:https://github.com/sidpro-hash/Object-Detection -only下载:-"coco.names”- -"frozen_inference_graph.pb“,这里是代码:
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)发布于 2022-03-15 01:40:13
简单回答:是的,您可以使用一个简单的逻辑操作来检查类ID,然后只显示属于该类的边界框(如果您不想用单个输出层重新训练模型)
然而,要正确回答这个问题并使用最好的实际场景;让我们检查一下单类对象检测和多类对象检测之间的区别,以及是否值得对其进行重新培训。
在模型架构方面,只有不同的层才是完全连接的层(当然,只有当您使用相同的主干进行特征提取时才是如此)。单类对象检测器只有一个回归层集,而多类对象检测模型则采用回归层+ softmax分类器来检测和分类多类标签。
那你应该用哪一种?
在某些情况下(即使您试图检测单个类),在不同的对象看起来非常相似的情况下,将它们分离成多个类,并在多类检测器中对它们进行培训,将减少为您想要的类提供假阳性结果的机会。你可能会像现在一样认为这种情况不是猫,因为它们和狗很相似,你很容易把猫误认为是狗。但是,如果你现在把猫和狗分开,你就很难把它们误认为是彼此的。
几乎每时每刻,都没有解决深度学习问题的通用方法。但现在,他们背后的理论将引导你走向成功。
如果要将该模型(ssd_mobilenet_v3)转换为单类对象检测器,所需做的就是删除完全连接的图层,添加你自己的,然后像往常一样训练;
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........https://stackoverflow.com/questions/71475517
复制相似问题