Browse Source

add timing to person detection

master
Lennart Heimbs 5 years ago
parent
commit
13549bb594
2 changed files with 96 additions and 31 deletions
  1. 5
    4
      camera/counter_people.py
  2. 91
    27
      camera/person_detection.py

+ 5
- 4
camera/counter_people.py View File

@@ -1,11 +1,12 @@
#!/usr/bin/env python

import argparse
import numpy as np
import imutils
import cv2
import argparse
from video_stream import imagezmq
import imutils
from imutils.object_detection import non_max_suppression
from video_stream import imagezmq


'''
Usage:
@@ -25,7 +26,7 @@ def detector(image):

clone = image.copy()

(rects, weights) = HOGCV.detectMultiScale(image, winStride=(4, 4), padding=(8, 8), scale=1.05)
(rects, _) = HOGCV.detectMultiScale(image, winStride=(4, 4), padding=(8, 8), scale=1.05)

# draw the original bounding boxes
for (x, y, w, h) in rects:

+ 91
- 27
camera/person_detection.py View File

@@ -1,15 +1,26 @@
#!/usr/bin/env python

from imutils.video import VideoStream
from imutils.video import FPS
import argparse
#from datetime import datetime, time
import time
from statistics import median

import imutils
from imutils.video import VideoStream
#from imutils.video import FPS

import cv2
from datetime import datetime, time
import numpy as np
import time as time2

VISUAL_DEBUG=True
frame_timer = None
contour_timer = None
detection_timer = None

frame_time = []
contour_time = []
detection_time = []

VISUAL_DEBUG = True

def getArgs():
""" Arguments """
@@ -22,18 +33,20 @@ def getArgs():

def main():
args = getArgs()
timer = Timer()

# if the video argument is None, then the code will read from webcam (work in progress)
if args.get("video", None) is None:
vs = VideoStream(src=0).start()
time2.sleep(2.0)
time.sleep(2.0)
# otherwise, we are reading from a video file
else:
vs = cv2.VideoCapture(args["video"])

cv2.namedWindow('Video stream', cv2.WINDOW_NORMAL)
detector = DetectionFromFrame(args["min_area"], 0.8)
detector = DetectionFromFrame(args["min_area"], 0.5)
while True:
timer.start_frame_timer()
detector.currentFrame = vs.read()
detector.currentFrame = detector.currentFrame if args.get("video", None) is None else detector.currentFrame[1]
# if the frame can not be grabbed, then we have reached the end of the video
@@ -42,12 +55,13 @@ def main():

# resize the frame to 500
detector.currentFrame = imutils.resize(detector.currentFrame, width=500)
detector.framecounter+=1
detector.framecounter += 1
if detector.framecounter > 1:
cnts = detector.prepareFrame()
for c in cnts:
boundRect = cv2.boundingRect(c)
timer.start_contour_timer()
bound_rect = cv2.boundingRect(c)
#(x, y, w, h) = cv2.boundingRect(c)
#initBB2 =(x,y,w,h)

@@ -56,19 +70,24 @@ def main():
net = cv2.dnn.readNetFromCaffe(prott1, prott2)

#trackbox = detector.currentFrame[y:y+h, x:x+w]boundRect[1]
trackbox = detector.currentFrame[boundRect[1]:boundRect[1]+boundRect[3],
boundRect[0]:boundRect[0]+boundRect[2]]
trackbox = detector.currentFrame[bound_rect[1]:bound_rect[1]+bound_rect[3],
bound_rect[0]:bound_rect[0]+bound_rect[2]]
trackbox = cv2.resize(trackbox, (224, 224))
#cv2.imshow('image',trackbox)
timer.start_detection_timer()
blob = cv2.dnn.blobFromImage(cv2.resize(trackbox, (300, 300)),0.007843, (300, 300), 127.5)
net.setInput(blob)
detections = net.forward()
for i in np.arange(0, detections.shape[2]):
detector.detectConfidentiallyPeople(i, detections, boundRect)
cv2.rectangle(detector.currentFrame, (boundRect[0], boundRect[1]),
(boundRect[0] + boundRect[2], boundRect[1] + boundRect[3]), (255, 255, 0), 1)
detector.detectConfidentiallyPeople(i, detections, bound_rect)
timer.stop_detection_timer()
cv2.rectangle(detector.currentFrame, (bound_rect[0], bound_rect[1]),
(bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3]), (255, 255, 0), 1)

timer.stop_contour_timer()

# show the frame and record if the user presses a key
@@ -82,10 +101,55 @@ def main():
detector.firstFrame = None
#detector.lastFrame = detector.currentFrame

timer.print_time()

# finally, stop the camera/stream and close any open windows
vs.stop() if args.get("video", None) is None else vs.release()
cv2.destroyAllWindows()

class Timer:
def __init__(self):
self.frame_timer = None
self.contour_timer = None
self.detection_timer = None

self.contour_time = []
self.detection_time = []

def start_frame_timer(self):
self.frame_timer = time.time()

def get_frame_time(self):
return time.time() - self.frame_timer

def start_contour_timer(self):
self.contour_timer = time.time()

def stop_contour_timer(self):
self.contour_time.append(time.time() - self.contour_timer)

def start_detection_timer(self):
self.detection_timer = time.time()

def stop_detection_timer(self):
self.detection_time.append(time.time() - self.detection_timer)

def print_time(self):
average_contour = 0 if not self.contour_time else sum(self.contour_time)/float(len(self.contour_time))
average_detection = 0 if not self.detection_time else sum(self.detection_time)/float(len(self.detection_time))

median_contour = 0 if not self.contour_time else median(self.contour_time)
median_detection = 0 if not self.detection_time else median(self.detection_time)

total_contour = sum(self.contour_time)
total_detection = sum(self.detection_time)

print("Time for Frame: {:.2f}. Contour Total: {:.2f}. Contour Median: {:.2f}. Contour Average: {:.2f}. Detection Total: {:.2f}. Detection Median: {:.2f}. Detection Average: {:.2f}. ".format(
self.get_frame_time(), total_contour, median_contour, average_contour, total_detection, median_detection, average_detection))
#print("Contour Times:" + str(timer.contour_time))
#print("Detection Times:" + str(timer.detection_time))
self.contour_time = []
self.detection_time = []

class DetectionFromFrame:
def __init__(self, min_size, confidence):
@@ -132,10 +196,10 @@ class DetectionFromFrame:

return cnts

def detectConfidentiallyPeople(self, i, detections, boundRect):
CLASSES = ["person"]
def detectConfidentiallyPeople(self, i, detections, bound_rect):
#CLASSES = ["person"]

COLORS = [0,255,0]
detected_color = (0, 255, 0)
#COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))

confidence = detections[0, 0, i, 2]
@@ -144,21 +208,21 @@ class DetectionFromFrame:
# extract the index of the class label from the `detections`, then compute the (x, y)-coordinates of
# the bounding box for the object
#idx = int(detections[0, 0, i, 1])
box = detections[0, 0, i, 3:7] * np.array([boundRect[2], boundRect[3], boundRect[2], boundRect[3]])
(startX, startY, endX, endY) = box.astype("int")
#box = detections[0, 0, i, 3:7] * np.array([bound_rect[2], bound_rect[3], bound_rect[2], bound_rect[3]])
#(startX, startY, endX, endY) = box.astype("int")
# draw the prediction on the frame
#label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100)
label = "{}: {:.2f}%".format(CLASSES[0], confidence * 100)
label = "{:.2f}%".format(confidence * 100)
#cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2)
cv2.rectangle(self.currentFrame, (boundRect[0], boundRect[1]),
(boundRect[0] + boundRect[2], boundRect[1] + boundRect[3]), (0,255, 0), 3)
cv2.rectangle(self.currentFrame, (bound_rect[0], bound_rect[1]),
(bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3]), detected_color, 3)
y = boundRect[1] - 15 if boundRect[1] - 15 > 15 else boundRect[1] + 15
y = bound_rect[1] - 15 if bound_rect[1] - 15 > 15 else bound_rect[1] + 15

#cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
cv2.putText(self.currentFrame, label, (0, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1)
cv2.putText(self.currentFrame, label, (bound_rect[0], bound_rect[1]-5), cv2.FONT_HERSHEY_SIMPLEX, 0.3, detected_color, 1)
#cv2.imshow("Video stream", self.currentFrame)
#print("Person found")


Loading…
Cancel
Save