Python 计算视频中的人数

Python 计算视频中的人数,python,caffe,object-detection,mobilenet,Python,Caffe,Object Detection,Mobilenet,我有pyimagesearch“”中的代码,用于使用caffe模型进行对象检测。我想做的是,无论何时检测到一个人,它都应该计数,最后我想计算通过的人数 我尝试过一些东西,但它增加了每一帧的计数,即使这个人是相同的并且出现在多个帧中,它也在所有帧中计数 import numpy as np import argparse import cv2 import imutils ap = argparse.ArgumentParser() ap.add_argument("-v", "--vide

我有pyimagesearch“”中的代码,用于使用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()

实际上,我想要的是,每当一个人来的时候,它应该计数1,当下一个人来的时候,它应该计数2,就像计数过程应该继续一样。

即使这个人从帧中消失,值“person”仍将保留在CLASSED[idx]中


因此,您初始化CLASSED[idx]或需要执行其他步骤

即使此人从帧中消失,值“person”仍将保留在CLASSED[idx]中

因此,您需要初始化CLASSED[idx]或采取其他步骤