-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_img.py
151 lines (124 loc) · 6.17 KB
/
test_img.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import sys
import argparse
import time
import tensorflow as tf
import cv2
import numpy as np
from src.mtcnn import PNet, RNet, ONet
from tools import detect_face, get_model_filenames
def main(args):
img = cv2.imread(args.image_path)
file_paths = get_model_filenames(args.model_dir)
count = 0
with tf.device('/gpu:0'):
with tf.Graph().as_default():
config = tf.ConfigProto(allow_soft_placement=True)
with tf.Session(config=config) as sess:
if len(file_paths) == 3:
image_pnet = tf.placeholder(
tf.float32, [None, None, None, 3])
pnet = PNet({'data': image_pnet}, mode='test')
out_tensor_pnet = pnet.get_all_output()
image_rnet = tf.placeholder(tf.float32, [None, 24, 24, 3])
rnet = RNet({'data': image_rnet}, mode='test')
out_tensor_rnet = rnet.get_all_output()
image_onet = tf.placeholder(tf.float32, [None, 48, 48, 3])
onet = ONet({'data': image_onet}, mode='test')
out_tensor_onet = onet.get_all_output()
saver_pnet = tf.train.Saver(
[v for v in tf.global_variables()
if v.name[0:5] == "pnet/"])
saver_rnet = tf.train.Saver(
[v for v in tf.global_variables()
if v.name[0:5] == "rnet/"])
saver_onet = tf.train.Saver(
[v for v in tf.global_variables()
if v.name[0:5] == "onet/"])
saver_pnet.restore(sess, file_paths[0])
def pnet_fun(img): return sess.run(
out_tensor_pnet, feed_dict={image_pnet: img})
saver_rnet.restore(sess, file_paths[1])
def rnet_fun(img): return sess.run(
out_tensor_rnet, feed_dict={image_rnet: img})
saver_onet.restore(sess, file_paths[2])
def onet_fun(img): return sess.run(
out_tensor_onet, feed_dict={image_onet: img})
else:
saver = tf.train.import_meta_graph(file_paths[0])
saver.restore(sess, file_paths[1])
def pnet_fun(img): return sess.run(
('softmax/Reshape_1:0',
'pnet/conv4-2/BiasAdd:0'),
feed_dict={
'Placeholder:0': img})
def rnet_fun(img): return sess.run(
('softmax_1/softmax:0',
'rnet/conv5-2/rnet/conv5-2:0'),
feed_dict={
'Placeholder_1:0': img})
def onet_fun(img): return sess.run(
('softmax_2/softmax:0',
'onet/conv6-2/onet/conv6-2:0',
'onet/conv6-3/onet/conv6-3:0'),
feed_dict={
'Placeholder_2:0': img})
start_time = time.time()
rectangles, points = detect_face(img, args.minsize,
pnet_fun, rnet_fun, onet_fun,
args.threshold, args.factor)
duration = time.time() - start_time
points = np.transpose(points)
for rectangle in rectangles:
cv2.putText(img, str(rectangle[4]),
(int(rectangle[0]), int(rectangle[1])),
cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0))
cv2.rectangle(img, (int(rectangle[0]), int(rectangle[1])),
(int(rectangle[2]), int(rectangle[3])),
(255, 0, 0), 1)
count+=1
for point in points:
for i in range(0, 10, 2):
cv2.circle(img, (int(point[i]), int(
point[i + 1])), 2, (0, 255, 0))
print(duration)
print(type(rectangles))
print(args.image_path)
print(count)
print(np.int_(rectangles))
data = [args.image_path, "\n", str(count), "\n", str(np.int_(rectangles)), "\n"]
file = open(args.save_file,"a+")
file.writelines(data)
cv2.imshow("test", img)
if args.save_image:
cv2.imwrite(args.save_name, img)
if cv2.waitKey(0) & 0xFF == ord('q'):
cv2.destroyAllWindows()
def parse_arguments(argv):
parser = argparse.ArgumentParser()
parser.add_argument('image_path', type=str,
help='The image path of the testing image')
parser.add_argument('--model_dir', type=str,
help='The directory of trained model',
default='./save_model/all_in_one/')
parser.add_argument(
'--threshold',
type=float,
nargs=3,
help='Three thresholds for pnet, rnet, onet, respectively.',
default=[0.8, 0.8, 0.8])
parser.add_argument('--minsize', type=int,
help='The minimum size of face to detect.', default=20)
parser.add_argument('--factor', type=float,
help='The scale stride of orginal image', default=0.7)
parser.add_argument('--save_image', type=bool,
help='Whether to save the result image', default=False)
parser.add_argument('--save_name', type=str,
help='If save_image is true, specify the output path.',
default='result.jpg')
parser.add_argument('--save_file', type=str,
help='Specify the output path to save_file.',
default='wider_face_test_bbx_gt.txt')
return parser.parse_args(argv)
if __name__ == '__main__':
main(parse_arguments(sys.argv[1:]))