Distance Estimation Using Single Camera OpenCV Python
Distance Estimation in computer vision is quite complicated, because you have to buy some extra hardware, like stereo camera, or depth sensor, to accomplish the task, here we are going to implement simple and effective solution, by using webcam, or smartphone camera.
Codebase
Here I am going to explain important code snippets, the complete codebase is available on my github repository, complete Tutorial available on Youtube as well.
Requirement
- Python
- OpenCV
- Measurement tap
Requirements
Setup Environment for face detection:
You need Python 3.x Version, Opencv-python, and you need a haar cascade file for face detection,
Distance Estimation
- Capture Reference Image:
- Measure the distance from the object(face) to the camera, capture Reference image, and the measured distance, note it down
- Measure the object (face) width, make sure that measurement units are kept for reference image and object(face) width. mine Reference Image
In case you don't want to set up anything here then, just go with my repository,
git clone https://github.com/Asadullah-Dal17/Distance_measurement_using_single_camera
Let's talk about three important functions or modules.
Face Detector
This function will detect the face and return the face width in the pixels values. This face data function simply takes one argument, which image, returns the face width in the pixels, which is a requirement for the focal length finder and distance finder function.
def face_data(image): face_width = 0 # making face width to zero # coverting color image ot gray scale image gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # detectiong face in the image faces = face_detector.detectMultiScale(gray_image, 1.3, 5) # looping through the faces detect in the image getting cooordinates x, y , width and height for (x, y, h, w) in faces: # draw the rectangle on the face cv2.rectangle(image, (x, y), (x+w, y+h), GREEN, 2) # getting face width in the pixels face_width = w # return the face width in pixel return face_width
Focal Length Finder
The Focal Length finder Function Tacks Three Arguments:
- Measured_distance is the distance from the camera to object while capturing the Reference image, Known_distance = 72.2 #centimeter
- Real_width Its measure the width of an object in real-world, here I measure the width of the face which is around Known_width =14.3 #centimeter
- Width_in_rf_image is the width of the object in the image/frame it will be in pixels
This function will return the focal length, which is used to find the distance.
# focal length finder function def FocalLength(measured_distance, real_width, width_in_rf_image): focal_length = (width_in_rf_image* measured_distance)/ real_width return focal_length
Distance Finder
This Function Tasks Three Argument,
- Focal length in pixel, which is a return from the Focal length finder function
- Real_width Its measure the width of an object in real-world, here I measure the width of the face which is around Known_width =14.3 #centimeter
- Width_in_rf_image is the width of the object in the image/frame it will be in pixels
The distance finder function will return the distance in the centimeters
# distance estimation function def Distance_finder(Focal_Length, real_face_width, face_width_in_frame): distance = (real_face_width * Focal_Length)/face_width_in_frame return distance
In case you have any question, please leave that down blow, another option would be, Github discussion tab, it's open for anyone,
I have no writing experience at all, I would love to get your suggestions, here as well,
thank you so much, Stay Safe....
Digital Products Expert Full Time (Kacha DFS), Self-Employed at (ensitesolution.com).
2 年is their any way that you can get cameras focal length without calculating usung known distances.
Professional with experience in Data Science, ML -DL, AI +BigData, SQL ETL Engineerig Software - IT, FP&A, SCRUM, Manufacturing, Administration, SOP, SupplyChain and Digital Strategy .
2 年Excellent!