-
Notifications
You must be signed in to change notification settings - Fork 2
/
FaceDepthMeasurement.py
38 lines (31 loc) · 1.03 KB
/
FaceDepthMeasurement.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
import cv2
import cvzone
from cvzone.FaceMeshModule import FaceMeshDetector
cap = cv2.VideoCapture(2)
detector = FaceMeshDetector(maxFaces=1)
while True:
success, img = cap.read()
img, faces = detector.findFaceMesh(img, draw=False)
if faces:
face = faces[0]
pointLeft = face[145]
pointRight = face[374]
# Drawing
# cv2.line(img, pointLeft, pointRight, (0, 200, 0), 3)
# cv2.circle(img, pointLeft, 5, (255, 0, 255), cv2.FILLED)
# cv2.circle(img, pointRight, 5, (255, 0, 255), cv2.FILLED)
w, _ = detector.findDistance(pointLeft, pointRight)
W = 6.3
# # Finding the Focal Length
# d = 50
# f = (w*d)/W
# print(f)
# Finding distance
f = 840
d = (W * f) / w
print(d)
cvzone.putTextRect(img, f'Depth: {int(d)}cm',
(face[10][0] - 100, face[10][1] - 50),
scale=2)
cv2.imshow("Image", img)
cv2.waitKey(1)