People count in the retail industry

Contents

This article was published as part of the Data Science Blogathon

Point

In this blog, we will discuss popular people counting methods, along with some tasks that are performed in video processing for best results. There are some algorithms like Haar Cascade methods, HOG and OpenCV used in human detection. After understanding these methods along with their advantages, we can employ these methods in the people counting use case as described below.

Our goal is to find the number of people in the store at a particular time (stay time) and the number of people in various sections (groceries, drinks, etc.) inside the retail store with the help of CCTV footage. To perform this task, CCTV videos are required at the point of entry and in different sections within the store.

The video below shows a typical CCTV recording of a retail store, with various store sections in the field of view.

Algorithms

Let's take a look at some of the people detection algorithms along with the approach used in this blog.:

1. Haar Cascade people detection algorithm It is an ML-based approach in which a cascade function is trained from many positive and negative images. Pre-trained cascades are used in detection. Learn more about this method here: cascada.

Below is the code for it:

import numpy as np

import cv2 # Create our body classifier

body_classifier = cv2.CascadeClassifier (‘Haarcascade_fullbody.xml’) # Start video capture for video file

cap = cv2.VideoCapture (‘/ moskva.mov’) # Repeat once the video has loaded successfully

while cap.isOpened ():

# Read the first frame

right, frame = cap.read ()

gray = cv2.cvtColor (marco, cv2.COLOR_BGR2GRAY)

# Pass frame to our body classifier

bodies = body_classifier.detectMultiScale (Grey, 1.1, 3)

# Extract bounding boxes for any identified body

to (x, Y, w, h) in bodies:

cv2.rectangle (marco, (x, Y), (x + w, Y + h), (0, 255, 255), 2)

cv2.imshow ('Pedestrians', marco)

if cv2.waitKey (1) == 13: # 13 is the Enter key

break

cap.release ()

cv2.destroyAllWindows ()

2. Simple HOG detection HOG (Gradients histogram) it's a kind of “feature descriptor”. La técnica cuenta las apariciones de la orientación de gradient en partes localizadas de una imagen y, Thus, in a video. Learn more about this method here: pork.

Below is the code for it:

import cv2
import imutils
# HOG person detector initialization
hog = cv2.HOGDescriptor
hog.setSVMDetector (cv2.HOGDescriptor_getDefaultPeopleDetector)
# Reading the image
image = cv2.imread (‘img.png’)
# Resize the image
imagen = imutils.resize (picture,
width = min (400, image.shape[1]))
# Detecting all regions in the image that have pedestrians inside
(regions, _) = hog.detectMultiScale (picture, winStride = (4, 4), padding = (4, 4), scale = 1.05)
# Drawing the regions in the image
to (x, Y, w, h) in regions:
cv2.rectangle (picture, (x, Y), (x + w, Y + h), (0, 0, 255), 2)
# Showing the output image
cv2.imshow (“Image”, picture)
cv2.waitKey (0)
cv2.destroyAllWindows ()

3. OpenCV background subtraction Background subtraction is an important preprocessing step in many vision-based applications. For instance, consider cases like a visitor counter where a static camera takes the number of visitors entering or leaving the room, or a traffic camera that extracts information about the vehicles, etc. In all these cases, you must first remove the person or vehicles alone. Technically, it is necessary to extract the moving foreground from the static background. It is a relatively faster method of detecting people in real time. OpenCV has implemented three of these algorithms:

  1. BackgroundSustractorMOG
  2. BackgroundSustractorMOG2
  3. BackgroundStractorGMG

Find out more about these here: opencv

Below is the implementation of OpenCV background subtraction using BackgroundSubtractorMOG2:

import numpy as np

import cv2

cap = cv2.VideoCapture (‘vtest.avi’)

fgbg = cv2.createBackgroundSubtractorMOG2 ()

while (1):

right, frame = cap.read ()

fgmask = fgbg.apply (marco)

cv2.imshow (‘marco’, fgmask)

k = cv2.waitKey (30) and 0xff

si k == 27:

break

cap.release ()

cv2.destroyAllWindows ()

78865opencv2-5300586

Source: https://docs.opencv.org/3.4/d1/dc5/tutorial_background_subtraction.html

The second image shows the results of the OpenCV background subtraction in the first image.

Our approach uses this method for best results. Contour methods and morphological transformations have been used to count people with greater precision.

4. HOG with linear SVM algorithm The accuracy of the HOG detector (discussed in the Simple HOG detection method) can be further improved by using an SVM classifier to classify positive and negative characteristics of sample images.

The positive and negative characteristics extracted from the collected positive and negative imaging samples are used to train the SVM model with HOG detection.. This method counts the traffic with the highest precision and the algorithm can be customized. Negative images can be generated (retail store background images) for any new store to increase accuracy.

Getting closer

Comparison of the algorithms mentioned above:

18630comparison-7834250

Source: auto project work

Let's see the approach used in this blog, based on the previous observation, considering the different types of videos we get from the retail store:

Video Division

Video splitting of store layout is done for effective traffic counting across multiple categories from a single camera view. One footage can cover 2-3 categories like drinks, grocery sections. To get precise people: count in different sections of the store, divide is useful.

splitting-7857618
Source: auto project work

As you can see in the image above, CCTV videos are available at bay level, so to measure traffic at the category level, video coverage area is divided into categories by area.

Results

In the use case, our main task is to have an estimate of the count of people inside the store (and also in various sections of the store) to analyze the residence time. Having discussed the appropriate algorithms and approaches for the given case, let's see the results:

Input camera video / Exit

The algorithm used: – Opencv background receiver

Reason: – Quick detections are made because people tend to enter at a relatively fast speed (compared to slow movement inside the store). People are detected when they cross the camera's view.

Outcome:-

Camera videos on various sections within the store

The algorithm used: – HOG (SVM linear classifier)

Reason: – Accurate detection is needed as people often walk with carts / kids. This algorithm is the best for this case.

Outcome:-

People in the grocery section count in each square:

People in the beverage section count in each square:

Let us know in the comments in case of any approach that can further improve the results..

The media shown in this article is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.

Datapeaker