Skip to content

Instantly share code, notes, and snippets.

@jotathebest
Last active July 12, 2022 13:07
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save jotathebest/291827084275f799a2262b6a45888abd to your computer and use it in GitHub Desktop.
Save jotathebest/291827084275f799a2262b6a45888abd to your computer and use it in GitHub Desktop.
Pedestrian detector that sends people counter results to Ubidots. Libraries: OpenCV, requests, imutils
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
IMPORTANT: This example is given AS IT IS without any warranty
Made by: Jose Garcia
'''
URL_EDUCATIONAL = "http://things.ubidots.com"
URL_INDUSTRIAL = "http://industrial.api.ubidots.com"
INDUSTRIAL_USER = True # Set this to False if you are an educational user
TOKEN = "...." # Put here your Ubidots TOKEN
DEVICE = "detector" # Device where will be stored the result
VARIABLE = "people" # Variable where will be stored the result
# Opencv pre-trained SVM with HOG people features
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 sendToUbidots(token, device, variable, value, context={}, industrial=True):
# Builds the endpoint
url = URL_INDUSTRIAL if industrial else URL_EDUCATIONAL
url = "{}/api/v1.6/devices/{}".format(url, device)
payload = buildPayload(variable, value, context)
headers = {"X-Auth-Token": token, "Content-Type": "application/json"}
attempts = 0
status = 400
while status >= 400 and attempts <= 5:
req = requests.post(url=url, headers=headers, json=payload)
status = req.status_code
attempts += 1
time.sleep(1)
return req
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 your 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, 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}
sendToUbidots(token, device, variable,
len(result), context=context)
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}
# 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()
@jotathebest
Copy link
Author

that error means that you are not getting any frame from your camera, check the cv2.VideoCapture() method settings.

All the best

@gersmit
Copy link

gersmit commented Jul 5, 2019

Hi Jotathebest,

My name is Ger from Holland.
I try your script but get error :
File "peoplecounter.py", line 38, in detector
(rects, weights) = HOGCV.detectMultiScale(image, winStride=(4, 4),
AttributeError: 'str' object has no attribute 'detectMultiScale'

Can youy help me out to fix the problem so i can use your nice script?

Thanks in advance,
Ger

@venkateshyc353
Copy link

File "happy_i_love_you.py", line 182, in detectPeople
(result, image) = localDetect(image_path)
File "happy_i_love_you.py", line 115, in localDetect
result = detector(image)
File "happy_i_love_you.py", line 54, in detector
(rects, weights) = HOGCV.detectMultiScale(image, winStride=(4, 4),
NameError: name 'HOGCV' is not defined

please help me

@jotathebest
Copy link
Author

Hi there, I missed to add the HOGCV instance in this example script, I have just updated it so please gently try again.

All the best

@SyedImam1998
Copy link

You left this line of code out, but it was present in your tutorial. It wasnt working but it worked when i added it
...........

Opencv pre-trained SVM with HOG people features

HOGCV = cv2.HOGDescriptor() HOGCV.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

in which line do we need to add this

@prpun311
Copy link

Great job, got it working. I think you might be missing

import base64

@jotathebest
Copy link
Author

Great job, got it working. I think you might be missing

import base64

Thanks for the report, I have just added the missed package at the beginning.

Best

@iMuhammadessa
Copy link

iMuhammadessa commented Sep 20, 2020

How to generate graphical report from ubidots dashboard?
I have did the same but I can't understand it at the last because TOKEN not sending information from application towards dashboard?

@iMuhammadessa
Copy link

Great, It worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment