cap = VideoStream().start()
while True:
frame=cap.read()
Detect = detection_method(frame)
if detect:
Predict_label=function(recognize)
(Step 4)#Do somthing on this predict_label
cv2.destroyAllWindows()
vs.stop()
Label is for example:unknown,cat,dog,panda,...
在上面显示的代码中,我捕获了相机的帧并使用一种检测方法识别了这些对象,并对这些对象进行了预测,并在识别出这些对象时(例如,相应的图像)进行了预测我展示的那些对象。 我的问题是,如果标签第一次是“狗”,那么系统会再次尝试识别对象并预测标签,如果第二次检测到“狗”,让我们继续运行步骤4,否则,请继续执行步骤4无法执行。我该怎么办? 我的最终目标是降低模型的敏感性。 我想到的是计算模型两次预测标签的次数,但我无法实现。
答案 0 :(得分:1)
您想要的是一个有状态的系统,因此您必须存储以前的状态,以便能够在每次检测到某件事时决定要做什么。
例如,您可以使用collections.deque
来实现,请参见doc:
cap = VideoStream().start()
previous_detections = collections.dequeue(maxlen=5) # adapt for your own purposes
while True:
frame = cap.read()
detection = detection_method(frame)
previous_detections.append(detection) # store the detection
if detection:
# use all previous states for your own logic
# I am not familiar with opencv so this will likely not work, consider it pseudo-code
# this is supposed to check if all previous known detections are the same as the current one
if all(previous_detection == detection for previous_detection in previous_detections):
predict_label = function(recognize)
(Step 4)#Do somthing on this predict_label
cv2.destroyAllWindows()
vs.stop()