【发布时间】:2020-01-15 02:28:57
【问题描述】:
我有来自 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,这样计数过程应该继续下去。
【问题讨论】:
标签: python caffe object-detection mobilenet