-
Notifications
You must be signed in to change notification settings - Fork 1
/
detectFace.py
executable file
·57 lines (54 loc) · 1.5 KB
/
detectFace.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
'''
File name: detectFace.py
Author:
Date created:
'''
'''
File clarification:
Detect or hand-label bounding box for all face regions
- Input img: the first frame of video
- Output bbox: the four corners of bounding boxes for all detected faces
'''
import cv2
import pdb
import numpy as np
import scipy
import matplotlib
import matplotlib.pyplot as plt
from skimage.feature import corner_shi_tomasi, corner_peaks
def detectFace(img):
#TODO: Your code here
# read image to array
casc = './Haarcascade_Classifier/haarcascade_frontalface_alt.xml'
faceCascade = cv2.CascadeClassifier(casc)
#convert to gray scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(
gray,
#scale factor decides the accuracy of detection
scaleFactor=1.1,
minNeighbors=5,
)
bbox = []
# Draw a rectangle around the face
for (x, y, w, h) in faces:
#cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
#print ([(y,x),(y,x+w),(y+h,x),(y+h,x+w)])
bbox.append([(y,x),(y,x+w),(y+h,x),(y+h,x+w)])
bbox = np.array(bbox)
# cv2.imshow("Faces found" ,img)
# cv2.imwrite("result.jpg", img)
#click on any key to terminate display
#cv2.waitKey(0)
return bbox
if __name__ == '__main__':
# setup video capture
cap = cv2.VideoCapture('./Datasets/Difficult/StrangerThings.mp4')
ret,img = cap.read()
cap.release()
if ret:
print ("Frame read %s", ret)
bbox = detectFace(img)
else:
print ("Frame read %s", ret)