-
Notifications
You must be signed in to change notification settings - Fork 8
/
recogniseFace.py
61 lines (48 loc) · 2.11 KB
/
recogniseFace.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
import face_recognition
import cv2
import numpy
debug = False
def getFaceCoordinates(input_image, upfactor=1, mode='hog', returnmode='2D'):
'''
Function can take in either a path string or an image object created by cv2.imread()
getFaceCoordinates returns a tuple of coordinates of the face detected as
returnmode: 1D
[(left, top, right, bottom)...]
returnmode: 2D
[((left, top), (right, top), (right, bottom), (left, bottom))...]
'''
# Allows for multiple argument types
if (type(input_image) is str):
input_image = cv2.imread(input_image)
elif (type(input_image) is numpy.ndarray):
pass
height, width, channels = input_image.shape
scale = 800 / height
# input_image = cv2.resize(input_image, (0, 0), fx=scale, fy=scale)
# [(left, top, right, bottom)] = face_recognition.face_locations(input_image)
array_of_face_locations = []
# model='hog' quick and dirty model='cnn' slower but accurate
for (top, right, bottom, left) in face_recognition.face_locations(input_image,
number_of_times_to_upsample=upfactor ,model=mode):
array_of_face_locations.append((left, top, right, bottom))
if (debug):
for (left, top, right, bottom) in array_of_face_locations:
cv2.rectangle(input_image, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.imshow("Test Image", input_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
if(returnmode == "1D"):
return array_of_face_locations
elif(returnmode == "2D"):
array_of_face_coordinates = []
for (left, top, right, bottom) in array_of_face_locations:
topleft = (left, top)
topright = (right, top)
bottomleft = (left, bottom)
bottomright = (right, bottom)
array_of_face_coordinates.append((topleft, topright, bottomright, bottomleft))
return array_of_face_coordinates
if debug:
test = cv2.imread("test.jpg")
print(getFaceCoordinates("test2.jpg", returnmode="2D"))
# Draw a box around the face