forked from spmallick/learnopencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathninjaEyeDetector.py
66 lines (46 loc) · 1.83 KB
/
ninjaEyeDetector.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
import numpy as np
import cv2
# Load the Cascade Classifier Xml file
face_cascade = cv2.CascadeClassifier("cascade/mallick_haarcascade_frontalface_default.xml")
# Specifying minimum and maximum size parameters
MIN_FACE_SIZE = 100
MAX_FACE_SIZE = 300
#Create a VideoCapture object
cap = cv2.VideoCapture(0)
# Check if camera opened successfully
if (cap.isOpened() == False):
print("Unable to read camera feed")
while(True):
# Reading each frame
ret, frameBig = cap.read()
# If frame opened successfully
if ret == True:
# Fixing the scaling factor
scale = 640.0/frameBig.shape[1]
# Resizing the image
frame = cv2.resize(frameBig,None,fx = scale,fy = scale, interpolation = cv2.INTER_LINEAR)
# Converting to grayscale
frameGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(frameGray, scaleFactor=1.1, minNeighbors=5,flags=0, minSize=(MIN_FACE_SIZE,MIN_FACE_SIZE),maxSize=(MAX_FACE_SIZE,MAX_FACE_SIZE))
# Loop over each detected face
for i in xrange (0,len(faces)):
# Dimension parameters for bounding rectangle for face
x,y,width,height = faces[i];
# Calculating the dimension parameters for eyes from the dimensions parameters of the face
ex,ey,ewidth,eheight = int(x + 0.125*width), int(y + 0.25 * height), int(0.75 * width), int(0.25 * height)
# Drawing the bounding rectangle around the face
cv2.rectangle(frame, (ex,ey),(ex+ewidth,ey+eheight),(128,255,0), 2)
# Display the resulting frame
cv2.imshow('Ninja Eye Detector',frame)
# Press ESC on keyboard to stop tracking
key = cv2.waitKey(1)
if (key==27):
break
# Break the loop
else:
break
# Release VideoCapture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()