Human Detection And Counting With Python
Real-time human detection and counting is a vast, challenging and important field of research.In this article, we are going to build the Human Detection and Counting System with python through images provided. This is an intermediate level Deep Learning project on Computer Vision, which will help you to master the concepts and make you an expert in the field of Data Science.
The project in Python requires you to have basic knowledge of python programming and the OpenCV library. We will be needing following libraries: OpenCV: A strong library used for Machine Learning , Imutils: To Image Processing , Numpy: Used for Scientific, Computing. Image is stored in a numpy array and Argparse: Used to give input in command line.
import cv2 import imutils import numpy as np import argparse
Here, the actual magic will happen. It will take a frame to detect a person in it. Make a box around a person and show the frame..and return the frame with person bounded by a green box.
It returns 2-tuple. List containing Coordinates of Bounding Box of person. Coordinates are in form X, Y, W, H. Where x,y are starting coordinates of box and w, h are width and height of box respectively and Confidence Value that it is a person.
def detect(frame): bounding_box_cordinates, weights = HOGCV.detectMultiScale(frame, winStride = (4, 4), padding = (8, 8), scale = 1.03) person = 1 for x,y,w,h in bounding_box_cordinates: cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 2) cv2.putText(frame, 'person: {}'.format(person), (x,y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 1) person += 1 cv2.putText(frame, 'Status : Detecting ', (40,40), cv2.FONT_HERSHEY_DUPLEX, 0.8, (255,0,0), 2) cv2.putText(frame, 'Total Persons : {}'.format(person-1), (40,70), cv2.FONT_HERSHEY_DUPLEX, 0.8, (255,0,0), 2) cv2.imshow('output', frame) return frame
def detectByPathImage(path, output_path): image = cv2.imread(path) image = imutils.resize(image, width = min(800, image.shape[1])) result_image = detect(image) if output_path is not None: cv2.imwrite(output_path, result_image) cv2.waitKey(0) cv2.destroyAllWindows()
This method is used to detec the human in the image.
def humanDetector(args): image_path = args["image"] video_path = args['video'] if str(args["camera"]) == 'true' : camera = True else : camera = False writer = None if args['output'] is not None and image_path is None: writer = cv2.VideoWriter(args['output'],cv2.VideoWriter_fourcc(*'MJPG'), 10, (600,600)) if camera: print('[INFO] Opening Web Cam.') detectByCamera(ouput_path,writer) elif video_path is not None: print('[INFO] Opening Video from path.') detectByPathVideo(video_path, writer) elif image_path is not None: print('[INFO] Opening Image from path.') detectByPathImage(image_path, args['output'])
The function argparse() simply parses and returns as a dictionary the arguments passed through your terminal to our script. Here it will take one argument for image.
def argsParser(): arg_parse = argparse.ArgumentParser() arg_parse.add_argument("-i", "--image", default=None, help="two_human.jpeg ") args = vars(arg_parse.parse_args()) return args
We have reached the end of our project. Here we will give the path of the image with in detectByPathImage() function as its argument and the second argument as the image which will detect and count number of humans in the image.
if __name__ == "__main__": HOGCV = cv2.HOGDescriptor() HOGCV.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) detectByPathImage("two_human.jpeg","count.jpeg")
In this deep learning project, we have learned how to create a people counter using HOG and OpenCV to generate an efficient people counter. We developed the project where you can supply the input as image. This is an intermediate level project, which will surely help you in mastering python and deep learning libraries.