1

I have code from pyimagesearch "https://www.pyimagesearch.com/2017/09/18/real-time-object-detection-with-deep-learning-and-opencv/#comment-550946" for object detection using caffe model. What I want to do is whenever a person is detected it should count and finally I want to count the number of person passed .

I have tried something but it incrementing the count for every frame, even if the person is same and appeared in multiple frames , it is counting in all the frames.

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()

Actually what I want is whenever a person comes it should count one and when the next person comes it should count 2 like that the counting process should continue.

4

1 回答 1

0

即使人从框架中消失,值“人”也将保留在 CLASSED[idx] 中。

所以你初始化 CLASSED[idx] 或者需要采取其他步骤

于 2019-09-15T23:51:35.663 回答