-
Notifications
You must be signed in to change notification settings - Fork 1
/
facepose_detection.py
187 lines (159 loc) · 6.67 KB
/
facepose_detection.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/home/hackpython/anaconda3/bin/python
# Author: Abhishek Sharma
# Program: Face Pose Estimation using Haar Cascasde, HOG and Dlib Library.
import os
import cv2
import numpy as np
import sys
import imutils
import dlib
from imutils import face_utils
class PoseDetection:
def __init__(self,option_type,path):
self.face_cascade = cv2.CascadeClassifier("cascade/haarcascade_frontalface_default.xml")
self.eye_cascade = cv2.CascadeClassifier("cascade/haarcascade_eye.xml")
self.smile_cascade = cv2.CascadeClassifier("cascade/haarcascade_smile.xml")
self.shape_predictor = "cascade/shape_predictor_68_face_landmarks.dat"
self.facedetect = False
self.functioncall = option_type
self.sourcepath = path
self.image_path = None
self.video_path = None
self.webcam_path = None
self.main_function()
def haar_facedetection(self,img):
faces = self.face_cascade.detectMultiScale(img,1.3,5)
print(faces)
return faces
def haar_eyedetection(self,img):
eyes = self.eye_cascade.detectMultiScale(img,1.3,5)
return eyes
def haar_smilecascade(self,img):
smile = self.smile_cascade.detectMultiScale(img,1.3,5)
return smile
def dlib_function(self,image):
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(self.shape_predictor)
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rects = detector(image, 1)
for (i, rect) in enumerate(rects):
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
for (x, y) in shape:
cv2.circle(image, (x, y), 1, (0, 0, 255), -1)
return image
def webcam(self):
cap = cv2.VideoCapture(int(self.webcam_path))
tracker = cv2.Tracker_create("MIL")
count = 0
while (cap.isOpened()):
ret, img = cap.read()
if not ret:
print("Cannot Read Video File")
break
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(21,21),0)
fullbody = self.HogDescriptor(gray)
for (x,y,w,h) in fullbody:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
faces = self.haar_facedetection(gray)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = self.haar_eyedetection(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color, (ex,ey), (ex+ew,ey+eh), (0,255,0),2)
smile = self.haar_smilecascade(roi_gray)
for (sx,sy,sw,sh) in smile:
cv2.rectangle(roi_color, (sx,sy), (sx+sw,sy+sh),(0,255,0),2)
img = self.dlib_function(img)
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
def video(self):
cap = cv2.VideoCapture(str(self.webcam_path))
tracker = cv2.Tracker_create("MIL")
count = 0
while (cap.isOpened()):
ret, img = cap.read()
if not ret:
print("Cannot Read Video File")
break
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(21,21),0)
faces = self.haar_facedetection(gray)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = self.haar_eyedetection(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color, (ex,ey), (ex+ew,ey+eh), (0,255,0),2)
smile = self.haar_smilecascade(roi_gray)
for (sx,sy,sw,sh) in smile:
cv2.rectangle(roi_color, (sx,sy), (sx+sw,sy+sh),(0,255,0),2)
img = self.dlib_function(img)
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
def image(self):
img = cv2.imread(self.image_path)
img = imutils.resize(img,width=min(800,img.shape[1]))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(21,21),0)
fullbody = self.HogDescriptor(gray)
for (x,y,w,h) in fullbody:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
faces = self.haar_facedetection(gray)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = self.haar_eyedetection(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color, (ex,ey), (ex+ew,ey+eh), (0,255,0),2)
smile = self.haar_smilecascade(roi_gray)
for (sx,sy,sw,sh) in smile:
cv2.rectangle(roi_color, (sx,sy), (sx+sw,sy+sh),(0,255,0),2)
img = self.dlib_function(img)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def HogDescriptor(self,image):
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
(rects, weights) = hog.detectMultiScale(image, winStride=(5,5),padding=(16,16), scale=1.05, useMeanshiftGrouping=False)
return rects
def main_function(self):
if self.functioncall == "image":
self.image_path = self.sourcepath
self.image()
elif self.functioncall == "webcam":
self.webcam_path = self.sourcepath
self.webcam()
elif self.functioncall == "video":
self.video_path = self.sourcepath
self.video()
if __name__ == '__main__':
print("\nPose Estimation\n")
print("\nSelect:\n 1:Image \n 2:Video \n 3:Webcam \n ")
input_type = int(input("Choice(Number): "))
if input_type == 1:
image_path = input("Enter Absolute Image Path: ")
PoseDetection(option_type="image",path=str(image_path))
elif input_type == 2:
video_path = input("Enter Video Path: ")
PoseDetection(option_type="video",path=str(video_path))
elif input_type == 3:
webcam_path = input("Enter Cam Number(0:Default): ")
PoseDetection(option_type="webcam",path=str(webcam_path))
else:
print("Please Select Correct Option")