-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenRecordOnBlink.py
87 lines (76 loc) · 3.27 KB
/
ScreenRecordOnBlink.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
from PIL import ImageGrab
import numpy as np
import cv2
from win32api import GetSystemMetrics
import datetime
#Initializing the face and eye cascade classifiers from xml files
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
#Variable store execution state
first_read = True
#Starting the video capture
cap = cv2.VideoCapture(0)
ret,img = cap.read()
while(ret):
ret,img = cap.read()
#Converting the recorded image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Applying filter to remove impurities
gray = cv2.bilateralFilter(gray,5,1,1)
#Detecting the face for region of image to be fed to eye classifier
faces = face_cascade.detectMultiScale(gray, 1.3, 5,minSize=(200,200))
if(len(faces)>0):
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
#roi_face is face which is input to eye classifier
roi_face = gray[y:y+h,x:x+w]
roi_face_clr = img[y:y+h,x:x+w]
eyes = eye_cascade.detectMultiScale(roi_face,1.3,5,minSize=(50,50))
#Examining the length of eyes object for eyes
if(len(eyes)>=2):
#Check if program is running for detection
if(first_read):
cv2.putText(img,
"Eye detected press s to begin",
(70,70),
cv2.FONT_HERSHEY_PLAIN, 3,
(0,255,0),2)
else:
cv2.putText(img,
"Eyes open!", (70,70),
cv2.FONT_HERSHEY_PLAIN, 2,
(255,255,255),2)
else:
if(first_read):
#To ensure if the eyes are present before starting
cv2.putText(img,
"No eyes detected", (70,70),
cv2.FONT_HERSHEY_PLAIN, 3,
(0,0,255),2)
else:
width = GetSystemMetrics(0)
height = GetSystemMetrics(1)
timestamp = datetime.datetime.now().strftime('%d-%m-%Y %H-%M-%S')
file_name = 'ScreenRecording'+ timestamp +'.mp4'
fource = cv2.VideoWriter_fourcc('m','p','4','v')
captured_video = cv2.VideoWriter(file_name,fource,20.0,(width,height))
print(width,height)
while True:
image = ImageGrab.grab(bbox=(0,0,width,height))
img_np = np.array(image)
image_final = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
cv2.imshow('Secret Capture', image_final)
captured_video.write(image_final)
else:
cv2.putText(img,
"No face detected",(100,100),
cv2.FONT_HERSHEY_PLAIN, 3,
(0,255,0),2)
#Controlling the algorithm with keys
cv2.imshow('img',img)
a = cv2.waitKey(1)
if(a==ord('q')):
break
elif(a==ord('s') and first_read):
#This will start the detection
first_read = False