Compare commits
2 Commits
870c27d0cb
...
a26557345f
Author | SHA1 | Date | |
---|---|---|---|
a26557345f | |||
4426205b0f |
1
.gitignore
vendored
1
.gitignore
vendored
@ -7,3 +7,4 @@ camera/videos
|
|||||||
*.jpg
|
*.jpg
|
||||||
*.h264
|
*.h264
|
||||||
*.mp4
|
*.mp4
|
||||||
|
*.png
|
160
camera/counter_people.py
Normal file
160
camera/counter_people.py
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
from imutils.object_detection import non_max_suppression
|
||||||
|
import numpy as np
|
||||||
|
import imutils
|
||||||
|
import cv2
|
||||||
|
import requests
|
||||||
|
import time
|
||||||
|
import argparse
|
||||||
|
import time
|
||||||
|
import base64
|
||||||
|
|
||||||
|
'''
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
python peopleCounter.py -i PATH_TO_IMAGE # Reads and detect people in a single local stored image
|
||||||
|
python peopleCounter.py -c # Attempts to detect people using webcam
|
||||||
|
'''
|
||||||
|
|
||||||
|
HOGCV = cv2.HOGDescriptor()
|
||||||
|
HOGCV.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
|
||||||
|
|
||||||
|
def detector(image):
|
||||||
|
'''
|
||||||
|
@image is a numpy array
|
||||||
|
'''
|
||||||
|
|
||||||
|
clone = image.copy()
|
||||||
|
|
||||||
|
(rects, weights) = HOGCV.detectMultiScale(image, winStride=(4, 4),
|
||||||
|
padding=(8, 8), scale=1.05)
|
||||||
|
|
||||||
|
# draw the original bounding boxes
|
||||||
|
for (x, y, w, h) in rects:
|
||||||
|
cv2.rectangle(clone, (x, y), (x + w, y + h), (0, 0, 255), 2)
|
||||||
|
|
||||||
|
# Applies non-max supression from imutils package to kick-off overlapped
|
||||||
|
# boxes
|
||||||
|
rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
|
||||||
|
result = non_max_suppression(rects, probs=None, overlapThresh=0.65)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def buildPayload(variable, value, context):
|
||||||
|
return {variable: {"value": value, "context": context}}
|
||||||
|
|
||||||
|
|
||||||
|
def argsParser():
|
||||||
|
ap = argparse.ArgumentParser()
|
||||||
|
ap.add_argument("-i", "--image", default=None,
|
||||||
|
help="path to image test file directory")
|
||||||
|
ap.add_argument("-c", "--camera", default=False,
|
||||||
|
help="Set as true if you wish to use the camera")
|
||||||
|
args = vars(ap.parse_args())
|
||||||
|
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
def localDetect(image_path):
|
||||||
|
result = []
|
||||||
|
image = cv2.imread(image_path)
|
||||||
|
image = imutils.resize(image, width=min(400, image.shape[1]))
|
||||||
|
clone = image.copy()
|
||||||
|
if len(image) <= 0:
|
||||||
|
print("[ERROR] could not read local image")
|
||||||
|
return result
|
||||||
|
print("[INFO] Detecting people")
|
||||||
|
result = detector(image)
|
||||||
|
|
||||||
|
"""# shows the result
|
||||||
|
for (xA, yA, xB, yB) in result:
|
||||||
|
cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
|
||||||
|
|
||||||
|
cv2.imshow("result", image)
|
||||||
|
cv2.waitKey(0)
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
|
cv2.imwrite("result.png", np.hstack((clone, image)))"""
|
||||||
|
return result#(result, image)
|
||||||
|
|
||||||
|
|
||||||
|
def cameraDetect(token, device, variable, sample_time=5):
|
||||||
|
|
||||||
|
cap = cv2.VideoCapture(0)
|
||||||
|
init = time.time()
|
||||||
|
|
||||||
|
# Allowed sample time for Ubidots is 1 dot/second
|
||||||
|
if sample_time < 1:
|
||||||
|
sample_time = 1
|
||||||
|
|
||||||
|
while(True):
|
||||||
|
# Capture frame-by-frame
|
||||||
|
ret, frame = cap.read()
|
||||||
|
frame = imutils.resize(frame, width=min(400, frame.shape[1]))
|
||||||
|
result = detector(frame.copy())
|
||||||
|
|
||||||
|
# shows the result
|
||||||
|
#for (xA, yA, xB, yB) in result:
|
||||||
|
# cv2.rectangle(frame, (xA, yA), (xB, yB), (0, 255, 0), 2)
|
||||||
|
#cv2.imshow('frame', frame)
|
||||||
|
|
||||||
|
# Sends results
|
||||||
|
if time.time() - init >= sample_time:
|
||||||
|
#print("[INFO] Sending actual frame results")
|
||||||
|
# Converts the image to base 64 and adds it to the context
|
||||||
|
#b64 = convert_to_base64(frame)
|
||||||
|
#context = {"image": b64}
|
||||||
|
if len(result):
|
||||||
|
print("{} people detected.".format(len(result)))
|
||||||
|
init = time.time()
|
||||||
|
|
||||||
|
|
||||||
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||||
|
break
|
||||||
|
|
||||||
|
# When everything done, release the capture
|
||||||
|
cap.release()
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
|
|
||||||
|
def convert_to_base64(image):
|
||||||
|
image = imutils.resize(image, width=400)
|
||||||
|
img_str = cv2.imencode('.png', image)[1].tostring()
|
||||||
|
b64 = base64.b64encode(img_str)
|
||||||
|
|
||||||
|
return b64.decode('utf-8')
|
||||||
|
|
||||||
|
|
||||||
|
def detectPeople(args):
|
||||||
|
image_path = args["image"]
|
||||||
|
camera = True if str(args["camera"]) == 'true' else False
|
||||||
|
|
||||||
|
# Routine to read local image
|
||||||
|
if image_path != None and not camera:
|
||||||
|
print("[INFO] Image path provided, attempting to read image")
|
||||||
|
(result, image) = localDetect(image_path)
|
||||||
|
print("[INFO] sending results")
|
||||||
|
# Converts the image to base 64 and adds it to the context
|
||||||
|
b64 = convert_to_base64(image)
|
||||||
|
context = {"image": b64}
|
||||||
|
print(len(result))
|
||||||
|
# Sends the result
|
||||||
|
"""req = sendToUbidots(TOKEN, DEVICE, VARIABLE,
|
||||||
|
len(result), context=context)
|
||||||
|
if req.status_code >= 400:
|
||||||
|
print("[ERROR] Could not send data to Ubidots")
|
||||||
|
return req"""
|
||||||
|
|
||||||
|
# Routine to read images from webcam
|
||||||
|
if camera:
|
||||||
|
print("[INFO] reading camera images")
|
||||||
|
cameraDetect(TOKEN, DEVICE, VARIABLE)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = argsParser()
|
||||||
|
detectPeople(args)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
@ -20,6 +20,12 @@ args = vars(ap.parse_args())
|
|||||||
""" Determine opencv version and select tracker """
|
""" Determine opencv version and select tracker """
|
||||||
# extract the OpenCV version info
|
# extract the OpenCV version info
|
||||||
(major, minor) = cv2.__version__.split(".")[:2]
|
(major, minor) = cv2.__version__.split(".")[:2]
|
||||||
|
# different methods of opencv require differing ways to unpack find countours
|
||||||
|
if int(major) > 3:
|
||||||
|
OPENCV4=True
|
||||||
|
else:
|
||||||
|
OPENCV4=False
|
||||||
|
|
||||||
# if we are using OpenCV 3.2 or an earlier version, we can use a special factory
|
# if we are using OpenCV 3.2 or an earlier version, we can use a special factory
|
||||||
# function to create the entity that tracks objects
|
# function to create the entity that tracks objects
|
||||||
if int(major) == 3 and int(minor) < 3:
|
if int(major) == 3 and int(minor) < 3:
|
||||||
@ -62,8 +68,13 @@ now = ''
|
|||||||
framecounter = 0
|
framecounter = 0
|
||||||
trackeron = 0
|
trackeron = 0
|
||||||
people_count_total = 0
|
people_count_total = 0
|
||||||
|
frame_counter= 0
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
"""frame_counter+=1
|
||||||
|
if framecounter%5 != 0:
|
||||||
|
continue"""
|
||||||
|
|
||||||
people_count_per_frame = 0
|
people_count_per_frame = 0
|
||||||
frame = vs.read()
|
frame = vs.read()
|
||||||
frame = frame if args.get("video", None) is None else frame[1]
|
frame = frame if args.get("video", None) is None else frame[1]
|
||||||
@ -93,7 +104,10 @@ while True:
|
|||||||
# dilate the thresholded image to fill in holes, then find contours on thresholded image
|
# dilate the thresholded image to fill in holes, then find contours on thresholded image
|
||||||
thresh = cv2.dilate(thresh, None, iterations=2)
|
thresh = cv2.dilate(thresh, None, iterations=2)
|
||||||
thresh = np.uint8(thresh)
|
thresh = np.uint8(thresh)
|
||||||
_, cnts, im2 = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
|
if OPENCV4:
|
||||||
|
cnts, im2 = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
|
||||||
|
else:
|
||||||
|
_, cnts, im2 = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
|
||||||
#cnts = cnts if imutils.is_cv2() else im2
|
#cnts = cnts if imutils.is_cv2() else im2
|
||||||
#print(len(cnts))
|
#print(len(cnts))
|
||||||
#if len(cnts) > 1:
|
#if len(cnts) > 1:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user