-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.py
124 lines (105 loc) · 3.95 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import cv2 as cv
import numpy as np
import module as m
import time
# Variables
COUNTER = 0
TOTAL_BLINKS = 0
CLOSED_EYES_FRAME = 3
cameraID = 0
videoPath = "Video/Your Eyes Independently_Trim5.mp4"
# variables for frame rate.
FRAME_COUNTER = 0
START_TIME = time.time()
FPS = 0
# creating camera object
camera = cv.VideoCapture(0)
# camera.set(3, 640)
# camera.set(4, 480)
# Define the codec and create VideoWriter object
fourcc = cv.VideoWriter_fourcc(*'XVID')
f = camera.get(cv.CAP_PROP_FPS)
width = camera.get(cv.CAP_PROP_FRAME_WIDTH)
height = camera.get(cv.CAP_PROP_FRAME_HEIGHT)
print(width, height, f)
fileName = videoPath.split('/')[1]
name = fileName.split('.')[0]
print(name)
# Recoder = cv.VideoWriter(f'{name}.mp4', fourcc, 15, (int(width), int(height)))
while True:
FRAME_COUNTER += 1
# getting frame from camera
ret, frame = camera.read()
if ret == False:
break
# converting frame into Gry image.
grayFrame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
height, width = grayFrame.shape
circleCenter = (int(width/2), 50)
# calling the face detector funciton
image, face = m.faceDetector(frame, grayFrame)
if face is not None:
# calling landmarks detector funciton.
image, PointList = m.faceLandmakDetector(frame, grayFrame, face, False)
# print(PointList)
cv.putText(frame, f'FPS: {round(FPS,1)}',
(460, 20), m.fonts, 0.7, m.YELLOW, 2)
RightEyePoint = PointList[36:42]
LeftEyePoint = PointList[42:48]
leftRatio, topMid, bottomMid = m.blinkDetector(LeftEyePoint)
rightRatio, rTop, rBottom = m.blinkDetector(RightEyePoint)
# cv.circle(image, topMid, 2, m.YELLOW, -1)
# cv.circle(image, bottomMid, 2, m.YELLOW, -1)
blinkRatio = (leftRatio + rightRatio)/2
cv.circle(image, circleCenter, (int(blinkRatio*4.3)), m.CHOCOLATE, -1)
cv.circle(image, circleCenter, (int(blinkRatio*3.2)), m.CYAN, 2)
cv.circle(image, circleCenter, (int(blinkRatio*2)), m.GREEN, 3)
if blinkRatio > 4:
COUNTER += 1
cv.putText(image, f'Blink', (70, 50),
m.fonts, 0.8, m.LIGHT_BLUE, 2)
# print("blink")
else:
if COUNTER > CLOSED_EYES_FRAME:
TOTAL_BLINKS += 1
COUNTER = 0
cv.putText(image, f'Total Blinks: {TOTAL_BLINKS}', (230, 17),
m.fonts, 0.5, m.ORANGE, 2)
# for p in LeftEyePoint:
# cv.circle(image, p, 3, m.MAGENTA, 1)
mask, pos, color = m.EyeTracking(frame, grayFrame, RightEyePoint)
maskleft, leftPos, leftColor = m.EyeTracking(
frame, grayFrame, LeftEyePoint)
# draw background as line where we put text.
cv.line(image, (30, 90), (100, 90), color[0], 30)
cv.line(image, (25, 50), (135, 50), m.WHITE, 30)
cv.line(image, (int(width-150), 50), (int(width-45), 50), m.WHITE, 30)
cv.line(image, (int(width-140), 90),
(int(width-60), 90), leftColor[0], 30)
# writing text on above line
cv.putText(image, f'{pos}', (35, 95), m.fonts, 0.6, color[1], 2)
cv.putText(image, f'{leftPos}', (int(width-140), 95),
m.fonts, 0.6, leftColor[1], 2)
cv.putText(image, f'Right Eye', (35, 55), m.fonts, 0.6, color[1], 2)
cv.putText(image, f'Left Eye', (int(width-145), 55),
m.fonts, 0.6, leftColor[1], 2)
# showing the frame on the screen
cv.imshow('Frame', image)
else:
cv.imshow('Frame', frame)
# Recoder.write(frame)
# calculating the seconds
SECONDS = time.time() - START_TIME
# calculating the frame rate
FPS = FRAME_COUNTER/SECONDS
# print(FPS)
# defining the key to Quite the Loop
key = cv.waitKey(1)
# if q is pressed on keyboard: quit
if key == ord('q'):
break
# closing the camera
camera.release()
# Recoder.release()
# closing all the windows
cv.destroyAllWindows()