-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyes.py
141 lines (101 loc) · 6.18 KB
/
pyes.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import sys
import shlex, subprocess #for running shell scripts
try:
import cv2 #OpenCV for face and video analysis
import numpy as np #numerical Python library required for OpenCV
import kivy #for gui application
kivy.require("1.10.0")
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
dependencies_exist=True #boolean to mark if dependencies are available
except ImportError:
print("\nSorry, you don't seem to have the required dependencies installed for the following version of Python: {}\n".format(sys.version))
if input("Would you like to install Kivy? Y/N:\n")=="y":
subprocess.Popen(shlex.split("pip3 install Kivy")) #command to install Kivy
if input("Would you like to install OpenCV? Y/N:\n")=="y":
subprocess.Popen(shlex.split("pip3 install opencv-python")) #command to install OpenCV
if input("Would you like to install Numpy? Y/N:\n")=="y":
subprocess.Popen(shlex.split("pip3 install numpy")) #command to install Numpy
print("Please restart the program")
dependencies_exist=False
if dependencies_exist: #only if everything is installed, program will continue to run
class PyesRecognitionLogic: #contains object detection functions
def __init__(self): #initializes Haar Cascades
self.face_cascade=cv2.CascadeClassifier("haar_cascades/face_cascade.xml")
self.eye_cascade=cv2.CascadeClassifier("haar_cascades/eyes_cascade.xml")
self.nose_cascade=cv2.CascadeClassifier("haar_cascades/nose_cascade.xml")
self.hand_cascade=cv2.CascadeClassifier("haar_cascades/hand_cascade.xml")
self.clock_cascade=cv2.CascadeClassifier("haar_cascades/clock_cascade.xml")
self.cap=cv2.VideoCapture(0) #0 is id of webcam
def face_detector(self):
while True:
ret, color_frame=self.cap.read() #returns each frame of the video
gray_frame=cv2.cvtColor(color_frame,cv2.COLOR_BGR2GRAY) #converts each frame to grayscale
self.face=self.face_cascade.detectMultiScale(gray_frame,1.3,5)
for (fx,fy,fw,fh) in self.face:
cv2.rectangle(color_frame,(fx,fy),(fx+fw,fy+fh),(255,255,0),3) #BGR values
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(color_frame,"Face",(fx,fy-7),font,1,(255,255,0),2,cv2.LINE_AA)
cv2.imshow("Detecting faces",color_frame)
if cv2.waitKey(1) & 0xFF==ord("q"): #quits when pressing Q
break
pyes.Exit() #exits Kivy app
def eye_detector(self):
while True:
ret, color_frame=self.cap.read() #returns each frame of the video
gray_frame=cv2.cvtColor(color_frame,cv2.COLOR_BGR2GRAY) #converts each frame to grayscale
self.eye=self.eye_cascade.detectMultiScale(gray_frame,1.3,5)
for (ex,ey,ew,eh) in self.eye:
cv2.rectangle(color_frame,(ex,ey),(ex+ew,ey+eh),(0,255,255),3) #BGR values
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(color_frame,"Eye",(ex,ey-7),font,1,(0,255,255),2,cv2.LINE_AA)
cv2.imshow("Detecting eyes",color_frame)
if cv2.waitKey(1) & 0xFF==ord("q"): #quits when pressing Q
break
pyes.Exit() #exits Kivy app
def nose_detector(self):
while True:
ret, color_frame=self.cap.read() #returns each frame of the video
gray_frame=cv2.cvtColor(color_frame,cv2.COLOR_BGR2GRAY) #converts each frame to grayscale
self.nose=self.nose_cascade.detectMultiScale(gray_frame,1.3,5)
for (nx,ny,nw,nh) in self.nose:
cv2.rectangle(color_frame,(nx,ny),(nx+nw,ny+nh),(0,255,0),3) #BGR values
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(color_frame,"Nose",(nx,ny-7),font,1,(0,255,0),2,cv2.LINE_AA)
cv2.imshow("Detecting noses",color_frame)
if cv2.waitKey(1) & 0xFF==ord("q"): #quits when pressing Q
break
pyes.Exit() #exits Kivy app
def hand_detector(self):
while True:
ret, color_frame=self.cap.read() #returns each frame of the video
gray_frame=cv2.cvtColor(color_frame,cv2.COLOR_BGR2GRAY) #converts each frame to grayscale
self.hand=self.hand_cascade.detectMultiScale(gray_frame,1.3,5)
for (hx,hy,hw,hh) in self.hand:
cv2.rectangle(color_frame,(hx,hy),(hx+hw,hy+hh),(0,0,200),3) #BGR values
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(color_frame,"Fist",(hx,hy-7),font,1,(0,0,200),2,cv2.LINE_AA)
cv2.imshow("Detecting fists",color_frame)
if cv2.waitKey(1) & 0xFF==ord("q"): #quits when pressing Q
break
pyes.Exit() #exits Kivy app
def clock_detector(self):
while True:
ret, color_frame=self.cap.read() #returns each frame of the video
gray_frame=cv2.cvtColor(color_frame,cv2.COLOR_BGR2GRAY) #converts each frame to grayscale
self.clock=self.clock_cascade.detectMultiScale(gray_frame,1.3,5)
for (hx,hy,hw,hh) in self.clock:
cv2.rectangle(color_frame,(hx,hy),(hx+hw,hy+hh),(100,50,200),3) #BGR values
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(color_frame,"Clock",(hx,hy-7),font,1,(100,50,200),2,cv2.LINE_AA)
cv2.imshow("Detecting clocks",color_frame)
if cv2.waitKey(1) & 0xFF==ord("q"): #quits when pressing Q
break
pyes.Exit() #exits Kivy app
class PyesGridLayout(GridLayout,PyesRecognitionLogic): #initializes the application
pass
class PyesApp(App):
def build(self): #builder method
return PyesGridLayout()
pyes=PyesApp()
pyes.run()