Click here to Skip to main content
15,889,116 members
Articles / Artificial Intelligence / Computer vision
Article

People Detection using OpenCV in Arduino Create

26 Oct 2018CPOL 5.4K   5  
Here we utilize the OpenCV libraries and apply the Histograms of Oriented Gradients (HOG) algorithm to create a computer vision application for people detection/counting.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Counts (or measures) the number of people who enter a designated area. You may be familiar with people counting systems, found in small shops, libraries and convenience stores, that use infrared sensors to detect people. When an infrared beam is cut (a person intercepts it by entering or exiting a door for example) the system increments a count. This technology has limitations when it comes to instances of occlusion (when one person A blocks person B and person B doesn't get counted). An appropriately designed computer vision-based people counting system can be more robust in handling cases of occlusion. Here we utilize the OpenCV libraries and apply the Histograms of Oriented Gradients (HOG) algorithm to create a computer vision application for people detection/counting.

What you’ll learn

  • How to run a basic people counter computer vision application

Gather your materials

Click here for instructions on how to set up the OpenCV libraries on your hardware.

Get the Code

To open the example in the Arduino Create* IDE, navigate to Examples > FROM LIBRARIES > UP SQUARED GROVE IOT DEV KIT > OpenCV-PeopleCounter.

Note: if you haven't already done so, connect to your Intel®-based platform through Arduino Create. For instruction on how to perform this task, see the Arduino Create Get Started Guide.

Verify

Image 1

Upload

Image 2

Run the Application

Note: make sure you've plugged in a UVC webcam before attempting to run the application

After successfully uploading the sketch to the target hardware, a sketches folder will be created in the Home directory on the Ubuntu desktop (where the Arduino Create IDE places the compiled code). Because the sketches folder is protected, You'll need to run the application as root user.

sudo su
cd sketches
./OpenCV-PeopleCounter

Two windows should pop up on the desktop (one for the webcam video and a terminal window for displaying numerical values). When a person is detected, you'll see a green bounding box around that person. And the terminal displays the "People count" as an integer value (person count per frame).

Image 3

Image 4

How it works

The people detector portion of the code uses Histograms of Oriented Gradients (HOG), a type of "feature descriptor", to achieve object detection. A feature descriptor (an algorithm) encodes information from an image or video into numerical values. Those values are then used to distinguish one feature from another (to make the task of classification easier).

In the code sample (.ino file), we initialize the HOG object with HOGDescriptor hog; and then instruct the HOG object to use a default people counter hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());. For each frame in the video capture, we perform the detection of people (our object) with hog.detectMultiScale (below int detectAndDraw(const HOGDescriptor &hog, Mat &img)) and then a rectangle (green) is drawn around that detected person starting from for(size_t i = 0; i < found.size(); i++ ). To count the number of rectangles (which corresponds to a detected a person), we use int num_people = detectAndDraw(hog, image).

Because we take advantage of an already existing (default) detector (here, we don't actually build an object detector from scratch), we don't provide an in-depth explanation of the HOG object. Most of the work is taken care of by the OpenCV libraries and that means the sample code is high-level (the details are abstracted from your view as the user).

About HOG

Here, "histogram" refers to a distribution (for example, a cluster of similar things, such as edges), "oriented" means directions and "gradients" refer to x, y derivatives (as in calculus, we’re looking for slopes here). Two types of gradients are used as features: edges (which include curves) and intensity. Curves and edges are the main idea for this type of problem (detection of objects such as people), and for HOG filter approaches in general. And a HOG filter is a linear classifier, which means that it’s very good at sorting things into "buckets" based on multi-dimensional features (such as curves and edges).

Read more about the HOG descriptor class at opencv.org.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
You may know us for our processors. But we do so much more. Intel invents at the boundaries of technology to make amazing experiences possible for business and society, and for every person on Earth.

Harnessing the capability of the cloud, the ubiquity of the Internet of Things, the latest advances in memory and programmable solutions, and the promise of always-on 5G connectivity, Intel is disrupting industries and solving global challenges. Leading on policy, diversity, inclusion, education and sustainability, we create value for our stockholders, customers and society.
This is a Organisation

42 members

Comments and Discussions

 
-- There are no messages in this forum --