-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathfacedetect_mtcnn.py
90 lines (66 loc) · 2.86 KB
/
facedetect_mtcnn.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
# MIT License
#
# Copyright (c) 2016 David Sandberg
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Borrowed from davidsandberg's facenet project: https://github.com/davidsandberg/facenet
# From this directory:
# facenet/src/align
#
# Just keep the MTCNN related stuff and removed other codes
# python package required:
# tensorflow, opencv,numpy
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import os
import argparse
import tensorflow as tf
import numpy as np
import detect_face
import cv2
def main(args):
sess = tf.Session()
pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
minsize = 40 # minimum size of face
threshold = [ 0.6, 0.7, 0.9 ] # three steps's threshold
factor = 0.709 # scale factor
filename =args.input
output_filename =args.output
draw = cv2.imread(filename)
img=cv2.cvtColor(draw,cv2.COLOR_BGR2RGB)
bounding_boxes, points = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
nrof_faces = bounding_boxes.shape[0]
for b in bounding_boxes:
cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 0))
print(b)
for p in points.T:
for i in range(5):
cv2.circle(draw, (p[i], p[i + 5]), 1, (0, 0, 255), 2)
cv2.imwrite(output_filename,draw)
print('Total %d face(s) detected, saved in %s' % (nrof_faces,output_filename))
def parse_arguments(argv):
parser = argparse.ArgumentParser()
parser.add_argument('--input', type=str, help='image to be detected for faces.',default='./test.jpg')
parser.add_argument('--output', type=str, help='new image with boxed faces',default='new.jpg')
return parser.parse_args(argv)
if __name__ == '__main__':
main(parse_arguments(sys.argv[1:]))