How to calculate distance between two person using python opencv?

Viewed 1776

I am working on a project where I have to calculate the distance between the detected persons in live video feed. To do this I am following the below pipeline:

1. Detect person using MobilenetSSD Caffe model
2. Extract bouding box of the persons detected
3. Calculate the centroid of each bouding box
4. Calculate the distance between the each centroid 

Below is the code snippet:

for (id1, p1), (id2, p2) in combinations(centroid_dict.items(), 2):
    ec_dist = dist.euclidean((p1[0], p1[1]), (p2[0], p2[1]))
    print("Euclidean distance {}".format(ec_dist))

In above code id1 and id2 are the id of the two persons. p1[0] and p1[1] are the x and y coordinate of person 1 and p2[0] p2[1] are the x y coordinate of person2. I am calculating the euclidean distance between the x and y coordinates of both the person.

Now lets say two persons are standing at a distance of 2meter to each other, for this I am getting euclidean distance in the code as 250. Now lets say if those two persons are now standing a bit far from the camera but the distance between them is still 2meter, in this case, I am getting euclidean distance as 343, which means the distance between person in the code is increasing if they are a bit far from the camera.

Initially I thought this logic will work fine regardless of where the persons are in the frame but looks like it is not working. Can anyone please help me suggest some good working solutions for this. Please help. Thanks

1 Answers

As an answer to the question in your comment.

enter image description here

In a camera view you have 3d shortening. And the only thing that you can be certain of with a persons position in the view is their feet on the ground.

If you take in account the 3d shortening and draw a grid on the view you could make a good estimate of the distance by recalculating the grid and positions to square.

Related