我有pyimagesearch“ https://www.pyimagesearch.com/2017/09/18/real-time-object-detection-with-deep-learning-and-opencv/#comment-550946”中的代码,用于使用caffe模型进行对象检测。我想做的是,每当检测到一个人时,它都应该计数,最后我要计算通过的人数。
我尝试了一些方法,但是它增加了每一帧的计数,即使人相同并且出现在多个帧中,它也在所有帧中计数。
import numpy as np
import argparse
import cv2
import imutils
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", default="1.avi",
help="path to input video")
ap.add_argument("-p", "--prototxt",
default="MobileNetSSD_deploy.prototxt.txt",
help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "--model", default="MobileNetSSD_deploy.caffemodel",
help="path to Caffe pre-trained model")
ap.add_argument("-c", "--confidence", type=float, default=0.6,
help="minimum probability to filter weak detections")
args = vars(ap.parse_args())
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "tvmonitor"]
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
cap = cv2.VideoCapture("1.mp4")
count= 0
while True:
ret, image = cap.read()
image = imutils.resize(image, width=500)
image = np.rot90(image)
image = np.rot90(image)
image = np.rot90(image)
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)),
0.007843, (300, 300), 127.5)
net.setInput(blob)
detections = net.forward()
for i in np.arange(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > args["confidence"]:
idx = int(detections[0, 0, i, 1])
box = detections[0, 0, i, 3:7] * np.array([w, h,
w, h])
(startX, startY, endX, endY) = box.astype("int")
label = "{}: {:.2f}%".format(CLASSES[idx],
confidence * 100)
print("[INFO] {}".format(label))
if CLASSES[idx]=="person":
count += 1
print("person",count)
cap.release()
cv2.destroyAllWindows()
实际上,我想要的是一个人到来时应该计数一次,而下一个人到来时它应该计数2,这样计数过程应该继续进行。
答案 0 :(得分:0)
即使人物从框架中消失了,值“人物”也将保留在CLASSED [idx]中。
因此您初始化CLASSED [idx]或需要采取其他步骤