Distance measurement using opencv

In Covid-19 pandemic, we all know about the importance of Social Distancing. Right now, It is the only thing that can help to reduce the number of affected people.

So I have decided to make a social distancing checker using python. The main idea is to measure the distance of object in the image and if the distance between two objects are less than some predefined pixel value then 'green' dot at the center of the bounding-box will convert to 'red' dot. The output of the code will like this-
distance measurement
social distancing checker


Python code for Distance Measurement

I am providing the python code to measure the distance of the object in the image or each frame of a video. Later I will discuss some key points related to the code.

Key points to be Noted:

    • Everything will be the same like object detection (discussed in Part-I and Part-II) as I have implemented distance measurement based on object detection.
    • We will filter out only the 'person' object from the objects of other classes.
    • We will calculate the distance of each 'person' from others, represented by the center coordinate of the bounding rectangular box using distance.cdist() defined in scipy.spatial 
    • Here I have used 'MIN_DISTANCE' as 200px.
    • Update the 'color' if the MIN_DISTANCE value for a 'person' becomes less than 200px.

    Output of the code

    The output of the code is shown when the input is an image. If the input will be a video then the output will look like this. Here some of the screenshots
    Distance Measurement
    Distance Measurement
    Distance Measurement
    Distance Measurement

    Note : As you can see, this method is not very efficient to measure distance very accurately. Position of the camera, Image quality these are also very important factor for getting better accuracy.

    Conclusion

    In this post, we have learnt how to measure distance between two objects using object detection. We have also learnt about the respective python code for distance measurement.






    Comments

    Popular posts from this blog

    Object Detection (Part I)

    Invisible Cloak using OpenCV