-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_face.py
52 lines (42 loc) · 1.31 KB
/
detect_face.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
import cv2
import os
import re
import scipy.misc
import warnings
import face_recognition.api as face_recognition
import sys
def get_coordinates(image_path):
img = face_recognition.load_image_file(image_path)
locations = face_recognition.face_locations(img)
location_json=[]
for coordinates in locations:
json_dict={
'ymin':coordinates[0],
'xmin':coordinates[1],
'ymax':coordinates[2],
'xmax':coordinates[3]}
location_json.append(json_dict)
return locations,location_json
if __name__ == "__main__":
image_path=sys.argv[1]
result,_=get_coordinates(image_path)
print("Coordinates: ",result)
img = cv2.imread(sys.argv[1], 0)
for coordinates in result:
ymin, xmin, ymax, xmax = coordinates
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (255,255,0), 2)
# xmin,ymin ------
# | |
# | |
# | |
# --------xmax,ymax
# will show the image in a window
cv2.imshow('image', img)
k = cv2.waitKey(0) & 0xFF
# wait for ESC key to exit
if k == 27:
cv2.destroyAllWindows()
# wait for 's' key to save and exit
elif k == ord('s'):
cv2.imwrite('messigray.png',img)
cv2.destroyAllWindows()