-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfacialDetection.py
More file actions
60 lines (45 loc) · 1.45 KB
/
facialDetection.py
File metadata and controls
60 lines (45 loc) · 1.45 KB
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
#Python 3.x
import cv2
import face_recognition
from settings import *
def convertToFrLocationFormat(faceLocations):
"""
converts face locations into format for face_recognition
"""
toReturn = []
for (x, y, w, h) in faceLocations:
top = y
right = x + w
bottom = y + h
left = x
toReturn.append((top, right, bottom, left))
return toReturn
def haarDetectFaceLocations(image):
"""
Take a raw image and run the haar cascade face detection on it
"""
#Create the haar cascade
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
#Our operations on the frame come here
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#Detect faces in the image with openCV Haar Cascade
faceLocations = faceCascade.detectMultiScale(
gray,
scaleFactor = HAAR_SCALE_FACTOR,
minNeighbors = 5,
minSize = (30, 30)
#flags = cv2.CV_HAAR_SCALE_IMAGE
)
#Convert face locations into face_recognition format
faceLocations = convertToFrLocationFormat(faceLocations)
return faceLocations
def hogDetectFaceLocations(image, isBGR=False):
"""
Take a raw image and run the hog face detection on it
"""
#Convert from BGR to RGB if needed
if (isBGR):
image = image[:, :, ::-1]
#Run the face detection model to find face locations
faceLocations = face_recognition.face_locations(image)
return faceLocations