Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

blink_detection.py 3.1 KB

You have to be logged in to leave a comment. Sign In
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
102
103
  1. #!/usr/bin/env python3
  2. # This is a demo of detecting eye status from the users camera. If the users eyes are closed for EYES_CLOSED seconds, the system will start printing out "EYES CLOSED"
  3. # to the terminal until the user presses and holds the spacebar to acknowledge
  4. # this demo must be run with sudo privileges for the keyboard module to work
  5. # PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
  6. # OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
  7. # specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
  8. # imports
  9. import face_recognition
  10. import cv2
  11. import time
  12. from scipy.spatial import distance as dist
  13. import keyboard as kb
  14. EYES_CLOSED_SECONDS = 5
  15. def main():
  16. closed_count = 0
  17. video_capture = cv2.VideoCapture(0)
  18. ret, frame = video_capture.read(0)
  19. # cv2.VideoCapture.release()
  20. small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
  21. rgb_small_frame = small_frame[:, :, ::-1]
  22. face_landmarks_list = face_recognition.face_landmarks(rgb_small_frame)
  23. process = True
  24. while True:
  25. ret, frame = video_capture.read(0)
  26. # get it into the correct format
  27. small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
  28. rgb_small_frame = small_frame[:, :, ::-1]
  29. # get the correct face landmarks
  30. if process:
  31. face_landmarks_list = face_recognition.face_landmarks(rgb_small_frame)
  32. # get eyes
  33. for face_landmark in face_landmarks_list:
  34. left_eye = face_landmark['left_eye']
  35. right_eye = face_landmark['right_eye']
  36. color = (255,0,0)
  37. thickness = 2
  38. cv2.rectangle(small_frame, left_eye[0], right_eye[-1], color, thickness)
  39. cv2.imshow('Video', small_frame)
  40. cv2.waitKey(1)
  41. ear_left = get_ear(left_eye)
  42. ear_right = get_ear(right_eye)
  43. closed = ear_left < 0.2 and ear_right < 0.2
  44. if (closed):
  45. closed_count += 1
  46. else:
  47. closed_count = 0
  48. if (closed_count >= EYES_CLOSED_SECONDS):
  49. asleep = True
  50. while (asleep): #continue this loop until they wake up and acknowledge music
  51. print("EYES CLOSED")
  52. if (kb.is_pressed('space')):
  53. asleep = False
  54. closed_count = 0
  55. process = not process
  56. def get_ear(eye):
  57. # compute the euclidean distances between the two sets of
  58. # vertical eye landmarks (x, y)-coordinates
  59. A = dist.euclidean(eye[1], eye[5])
  60. B = dist.euclidean(eye[2], eye[4])
  61. # compute the euclidean distance between the horizontal
  62. # eye landmark (x, y)-coordinates
  63. C = dist.euclidean(eye[0], eye[3])
  64. # compute the eye aspect ratio
  65. ear = (A + B) / (2.0 * C)
  66. # return the eye aspect ratio
  67. return ear
  68. if __name__ == "__main__":
  69. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...