Using Python to Monitor Social Distancing in a Public Area | by Aqeel Anwar | Towards Data Science

Note: Idea credits Landing AI
Social-distancing is an important way to slow down the spread of infectious diseases. People are asked to limit their interactions with each other, reducing the chances of the disease being spread with physical or close contact.
In the past decade, AI/Deep Learning has shown promising results in several daily life problems. Various daily life tasks have been automated with the help of AI. In this article, we will go through in detail how can one use python combined with deep learning and computer vision to monitor social distancing. The complete python code with installing instructions is available on GitHub
Before we get into detail, let’s have a look at the final output. The video below is the result of using AI to monitor social distancing in a public area with already available street cameras.
The rest of the article will be divided into three steps
Step 1 — Installing the project
Step 2 —Running the project
Step 3 —Inputs and Outputs of the code
Step 4— How does it work?
Step 5— Suggested improvements
It’s advisable to make a new virtual environment for this project and install the dependencies. Following steps can be taken to download get started with these project
Clone the repository: The repository containing the code can be cloned using
Clone the repository: The repository containing the code can be cloned using
git clone https://github.com/aqeelanwar/SocialDistancingAI.git
2. Install required packages: The provided requirements.txt file can be used to install all the required packages. Use the following command
cd SocialDistancingAIpip install –r requirements.txt
This will install the required packages in the activated python environment.
The code takes the video file path as input. The following command can be used to run the code
cd SocialDistancingAIpython main.py --videopath "vid_short.mp4"
Running main.py will open a window of the first frame in the video. At this point the code expects the user to mark 6 points by clicking appropriate positions on the frame. These points are necessary to provide a user perspective of the video.
The first 4 among the 6 required points are used to mark the Region of Interest (ROI) where you want to monitor. Moreover, the lines marked by these points should be parallel in the real world as seen from above. For example, these lines could be the curbs of the road.These 4 points need to be provided in a pre-defined order which is the following.
Point1 (bl): Bottom left
Point2 (br): Bottom right
Point3 (tl): Top left
Point4 (tr): Top right
The last two points are used to mark two points 6 feet apart in the region of interest. For example, this could be a person’s height (easier to mark on the frame)
The gif below will help understand the points better
The Code outputs
The first output of the code is the original video with detected pedestrians in it. The pedestrians are localized with purple boxes.
The second output of the code is localized pedestrian video from a top-down perspective (bird's eye view) in the region of interest that was marked by the user.
Based on the bird's eye view and 6 feet distance points marked by the user, the algorithm calculates and reports the following parameters.
1. # 6ft violations: Number of times the pedestrians violated the 6 feet safe distance threshold
2. Stay-at-home Index: Quantifies how many people are staying at home as compared to the normal routine pedestrian traffic. 0% means that there is no change in the pedestrian traffic compared to normal days, 50% means half of the people are staying at home
3. Social-distancing Index: Quantifies the social distancing being maintained. 50% means half of the interactions violate the safe 6 feet distance criteria.
Detecting distances between pedestrians from monocular images without any extra information is not possible. One way (not very accurate though) is to ask the user for specific inputs leading to a distance estimation between the pedestrians. If the user could mark two points on the frame that are 6 feet apart, using extrapolation, one could find the distance between different points on the frame. This would have been true if the camera was equidistant to all the points on the plane where the pedestrians were walking. The closer the pedestrians are to the camera the bigger they are. The closer the two points (which are the same number of pixels apart ) on the frame to the camera, the smaller is the actual distance between them.
To cope with this issue, the code receives 4 points input from the user to mark two lines that are parallel seen from the above. The region marked by these 4 points are considered ROI (can be seen in yellow in the gif above). This polygon-shaped ROI is then warped into a rectangle which becomes the bird's eye view. This bird's eye view then has the property of points (which are the same number of pixels apart) being equidistant no matter where they are. All it needs is a multiplier that maps the distance between two points in pixels to distance in real life units (such as feet or meters). This is where the last two user input points come into play.
Deep Learning is used to detect and localize the pedestrians which are then mapped to a bird's eye view projection of the camera as explained above. Once we have the coordinates of the pedestrians in the bird's eye view the social distancing parameters become straightforward.
The complete block diagram of the algorithm can be seen below.
The code could use a lot of improvements in several areas. A few are suggested below
A more accurate approach of mapping the camera frame to a bird's eye viewThe code uses an existing multi-class classifier that was trained on 1000 classes from the COCO dataset. The code could use a re-trained classifier specifically trained for binary class (pedestrian, no-pedestrian).A more accurate way of calculating social distance parameters can be introduced.
A more accurate approach of mapping the camera frame to a bird's eye view
The code uses an existing multi-class classifier that was trained on 1000 classes from the COCO dataset. The code could use a re-trained classifier specifically trained for binary class (pedestrian, no-pedestrian).
A more accurate way of calculating social distance parameters can be introduced.
The algorithm can be used to analyze social distancing in a public area and carry out necessary actions to better deal with the pandemic. Automating the task will lead to effective actions taken in a short time hence equipping us better to deal with the situation. The complete code is available here at GitHub. Feel free to modify and improve it.
Input Pedestrian video is taken from https://www.youtube.com/watch?v=aUdKzb4LGJI
If this article was helpful to you, feel free to clap, share and respond to it. If you want to learn more about Machine Learning and Data Science, follow me @Aqeel Anwar or connect with me on LinkedIn.