-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
60 lines (53 loc) · 2.05 KB
/
example.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
import caffe
import numpy as np
import skimage.io
def face_net(image_dim, input_dim, model_file, pretrained, batch=10, gpu=True):
mean = np.zeros([3] + input_dim)
scale = 0.00390625
net = caffe.Classifier(model_file, pretrained,
mean=mean, input_scale=scale,
#image_dims=image_dim, batch=batch)
image_dims=image_dim, batch=batch, gpu=gpu)
#caffe.set_phase_test()
#net.set_mode_gpu()
return net
def face_input_prepare(net, inputs, oversample=False):
# Scale to standardize input dimensions.
input_ = np.zeros((len(inputs),
net.image_dims[0], net.image_dims[1], inputs[0].shape[2]),
dtype=np.float32)
for ix, in_ in enumerate(inputs):
#input_[ix] = caffe.io.resize_image(in_, net.image_dims)
input_[ix] = caffe.io.resize_image_tlc(in_, net.image_dims)
if oversample:
# Generate center, corner, and mirrored crops.
input_ = caffe.io.oversample(input_, net.crop_dims)
else:
# Take center crop.
center = np.array(net.image_dims) / 2.0
crop = np.tile(center, (1, 2))[0] + np.concatenate([
-net.crop_dims / 2.0,
net.crop_dims / 2.0
])
input_ = input_[:, crop[0]:crop[2], crop[1]:crop[3], :]
# Classify
caffe_in = np.zeros(np.array(input_.shape)[[0,3,1,2]],
dtype=np.float32)
for ix, in_ in enumerate(input_):
caffe_in[ix] = net.preprocess(net.inputs[0], in_)
return caffe_in
# img_file: image filename
# image_dim: dimension of image, [152,152] in this case
# input_dim: dimension of input, [152,152]
def face_recognition(img_file, image_dim, input_dim, model_file, pretrained):
mean = np.zeros([3] + input_dim)
scale = 0.00390625
net = caffe.Classifier(model_file, pretrained,
mean=mean, input_scale=scale,
image_dims=image_dim)
#net.set_phase_test()
#net.set_mode_gpu()
input_image = skimage.io.imread(img_file)
prediction = net.predict([input_image], oversample=False)
label = prediction.argmax()
return label