内容简介:Build a simple Social Distancing Detector to monitor the practice of social distancing in a crowdSocial Distancing is one such terminology that has gained popularity over the past few months, thanks toSocial Distancing is possibly the only way to contain t
Build a simple Social Distancing Detector to monitor the practice of social distancing in a crowd
Jun 9 ·5min read
Social Distancing is one such terminology that has gained popularity over the past few months, thanks to COVID-19 . People are forced to maintain a sufficient amount of distance between each other to prevent the spread of this deadly virus. Amidst this crisis, I decided to build a simple Social Distancing Detector that could monitor the practice of social distancing in a huge crowd.
Overview-
Social Distancing is possibly the only way to contain the spread of COVID-19 . Recently, Andrew Ng’s Landing AI team created a Social Distancing Tool using the concepts of Computer Vision. This project is inspired by their work.
In this Social Distancing Detector, I’ll be using the weights of the YOLO v3 Object Detection Algorithm and the COCO dataset which are easily available online. Additionally, the main library being used will be the OpenCV along with the Deep Neural Network ( dnn ) module.
There are very few library requirements to build this Social Distancing Detector. So, if you are into Deep Learning, you would already have installed these libraries.
import numpy as np import cv2 import imutils import os import time
We need to define three user-defined functions for this detector program, which will be explained below.
Step 1: Check function
This function is used to calculate the distance between two different points ( a and b ) in an image (single frame of a video). The two points denote the center of the individuals detected in the video. The distance that is calculated is the Euclidean Distance between two points.
Then, calibration is used to find the midpoint between the two points and the ‘ if ’ statement returns a ‘ True ’ if the distance lies in between the range, else returns ‘ False ’.
Step 2: Setup function
This function has one input argument ‘ yolo ’, which is a string that contains an empty directory into which the ‘ weights ’, ‘ cfg ’ and ‘ labels ’. will be stored.
The os.path.sep.join()
function is used to join one or more path components intelligently. In simple terms, it stores the “ yolov3.weights ”, “ yolov3.cfg ” and “ coco.names ” files into the “ yolo ” directory.
The variable ‘ net ’ is used to read a network model stored in Darknet model files.
The ‘ ln ’ function is used to get all the layers of the network model such as ‘relu_89’, ‘conv_89’, ‘bn_89’, ‘relu_90’, ‘conv_90’ and then store only the indexes of layers with unconnected outputs. [‘yolo_82’, ‘yolo_94’, ‘yolo_106’] .
Step 3: Image Process function
This is the most complex function in our program. In this function, The argument is a single frame of the video. For every iteration, each frame of the input video gets processed along with the social distancing detection between each individual in a crowd and is returned to the main function.
The code for this function is available in my GitHub repository (link is available below) and can be used for reference.
Step 4: Input parameters
In this, we initialize the input video file as ‘ filename ’ and the output file to be stored in the .mp4 format as ‘ opname ’
create = None frameno = 0 filename = "videos/video_1.mp4" yolo = "yolo-coco/" opname = "output_videos/output_of_" + filename.split('/')[1][:-4] + '.mp4'
Step 5: Main function
We now come to the final part of the code. In this, the input video is separated into many single frames on which the functions defined above will be performed.
Once that is done, each output frame is received and combined together to create the output video.
The .time()
function is used to calculate the time taken to perform the whole operation. Longer the video, longer will be the time taken to process the video. In my example, a 3-second video took approximately 150-seconds to get processed to an output video along with the social distancing detection. So, do the Math!
Demo Video-
I am running this code on an example that was obtained from TRIDE (University of Oxford).
By comparing both the videos, we can see that the Social Distancing Detector is able to detect if the crowd is following social distancing and marks the people who are not in safe distances with a red box and the safe people with a green box .
This program can be altered with various threshold values to check for social distancing. For those who are interested, just by tweaking the code a little, you can set the source of the video as the webcam of your PC . I’ll leave that for you to try it out by yourself.
Conclusion-
This program is a very simple and basic implementation of the Social Distancing Detector. This code can serve as a basis for more complex programs which can be developed from this.
For those who are looking for the code, I am also attaching a link to my github repository where you can find the code of this program for your reference. Go ahead, fork this repository and bring in more developments to this simple piece of code!
In this article, we have successfully built a simple Social Distancing Detector using Deep Learning. This has several applications such as the CCTV cameras which can be equipped with this software tool to monitor the practice of social distancing in a huge crowd or in an office environment.
Also, do check out my other article where I have described the program to build a simple CNN model to detect if an individual is wearing a face mask or not.
Social Distancing in the Post-COVID era is one such practice that will be followed for many days to come by. Let us all be safe and stay healthy.Till then, Happy Machine Learning!
以上所述就是小编给大家介绍的《COVID-19: Social Distancing Detector》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
数据结构与算法分析
韦斯 (Mark Allen Weiss) / 陈越 / 机械工业出版社 / 2016-3-1 / 69.00元
本书是国外数据结构与算法分析方面的经典教材,使用卓越的Java编程语言作为实现工具讨论了数据结构(组织大量数据的方法)和算法分析(对算法运行时间的估计)。本书把算法分析与有效率的Java程序的开发有机地结合起来,深入分析每种算法,内容全面、缜密严格,并细致讲解精心构造程序的方法。一起来看看 《数据结构与算法分析》 这本书的介绍吧!