-
Notifications
You must be signed in to change notification settings - Fork 94
/
distance.py
101 lines (83 loc) · 3.67 KB
/
distance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
-------------------------------------------
- Author: Asadullah Dal -
- ============================= -
- Company Name: AiPhile -
- ============================= -
- Purpose : Youtube Channel -
- ============================ -
- Link: https://youtube.com/c/aiphile -
-------------------------------------------
"""
import cv2
# variables
# distance from camera to object(face) measured
KNOWN_DISTANCE = 76.2 # centimeter
# width of face in the real world or Object Plane
KNOWN_WIDTH = 14.3 # centimeter
# Colors
GREEN = (0, 255, 0)
RED = (0, 0, 255)
WHITE = (255, 255, 255)
fonts = cv2.FONT_HERSHEY_COMPLEX
cap = cv2.VideoCapture(1)
# face detector object
face_detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# focal length finder function
def focal_length(measured_distance, real_width, width_in_rf_image):
"""
This Function Calculate the Focal Length(distance between lens to CMOS sensor), it is simple constant we can find by using
MEASURED_DISTACE, REAL_WIDTH(Actual width of object) and WIDTH_OF_OBJECT_IN_IMAGE
:param1 Measure_Distance(int): It is distance measured from object to the Camera while Capturing Reference image
:param2 Real_Width(int): It is Actual width of object, in real world (like My face width is = 14.3 centimeters)
:param3 Width_In_Image(int): It is object width in the frame /image in our case in the reference image(found by Face detector)
:retrun focal_length(Float):"""
focal_length_value = (width_in_rf_image * measured_distance) / real_width
return focal_length_value
# distance estimation function
def distance_finder(focal_length, real_face_width, face_width_in_frame):
"""
This Function simply Estimates the distance between object and camera using arguments(focal_length, Actual_object_width, Object_width_in_the_image)
:param1 focal_length(float): return by the focal_length_Finder function
:param2 Real_Width(int): It is Actual width of object, in real world (like My face width is = 5.7 Inches)
:param3 object_Width_Frame(int): width of object in the image(frame in our case, using Video feed)
:return Distance(float) : distance Estimated
"""
distance = (real_face_width * focal_length) / face_width_in_frame
return distance
# face detector function
def face_data(image):
"""
This function Detect the face
:param Takes image as argument.
:returns face_width in the pixels
"""
face_width = 0
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray_image, 1.3, 5)
for (x, y, h, w) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), WHITE, 1)
face_width = w
return face_width
# reading reference image from directory
ref_image = cv2.imread("Ref_image.png")
ref_image_face_width = face_data(ref_image)
focal_length_found = focal_length(KNOWN_DISTANCE, KNOWN_WIDTH, ref_image_face_width)
print(focal_length_found)
cv2.imshow("ref_image", ref_image)
while True:
_, frame = cap.read()
# calling face_data function
face_width_in_frame = face_data(frame)
# finding the distance by calling function Distance
if face_width_in_frame != 0:
Distance = distance_finder(focal_length_found, KNOWN_WIDTH, face_width_in_frame)
# Drwaing Text on the screen
cv2.putText(
frame, f"Distance = {round(Distance,2)} CM", (50, 50), fonts, 1, (WHITE), 2
)
cv2.imshow("frame", frame)
if cv2.waitKey(1) == ord("q"):
break
cap.release()
cv2.destroyAllWindows()