Real -Time face distance measure from camera using OpenCV and cvzone-FaceMeshModule
Distance Measurement is the process of measuring how far an object is from another. There are many methods to calculate the distance but here, I just measure the distance from camera to object having real size of an object using cvzone and OpenCV.
$ pip3 install cvzone
import cv2
from cvzone.FaceMeshModule import FaceMeshDetector
we have a formula to calulate focal length:
focal_length = (size_object_pixel * distance)/size_object
In this formula we don't know the object size and distance, but we need the real size of an object. Here, I have took the distance between two eyes i.e apporximately 6.3 as real size of an object and assume initial distance from a camera is 50. The size of an object pixel can be obtained by cvzone library.
Now, we can easily calculate focal length. Then, after we can calculate distance from camera:
distance or depth = (size_object * focal_length_calculated)/size_object_pixel
Click here to watch demo
The MIT License (MIT). Please see License File for more information.