forked from xipeng13/recurrent-face-alignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetectImageDemo.py
90 lines (75 loc) · 2.59 KB
/
DetectImageDemo.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
# Demo code to detect landmarks from static image
# "A Recurrent Encoder-Decoder Network for Sequential Face Alignment", ECCV, 2016
# Xi Peng, [email protected]
# V0.1
import os,sys
import numpy as np
from PIL import Image, ImageDraw
WORKPATH = os.path.dirname(os.path.abspath(__file__))
print WORKPATH
sys.path.append(WORKPATH+'/pylib/')
import PXIO, PXPts
# set caffe/python path
sys.path.append('path/to/caffe/python/')
import caffe
SL = 128
NLMK = 7
def DetectImage(img_path, bbox, save_path):
ch, hei, wid = 11, SL, SL
data = np.zeros((1,ch,hei,wid))
pts2 = np.zeros((NLMK,2))
zoom_ratio = float(bbox[2]-bbox[0]) / SL
img = Image.open(img_path)
img_crop = img.crop(bbox)
img2 = img_crop.resize((SL, SL), Image.ANTIALIAS)
img2 = np.asarray(img2)
mean_shape = np.zeros((1,8,SL,SL))
data[0,0:3,:,:] = np.transpose(img2, (2,0,1))
for s in range(2):
data[0,3:11,:,:] = mean_shape
net.blobs['img_lmk'].data[...] = data
net.forward()
prob_map = net.blobs['prob'].data[...]
prob_map = np.squeeze(prob_map[0,:,:,:])
mean_shape = prob_map
label_map = np.argmax(prob_map, axis=0)
for l in range(NLMK):
try:
y,x = np.where(label_map == l+1)
yc,xc = np.mean(y), np.mean(x)
pts2[l,:] = [xc,yc]
except:
print l
pts = pts2 * zoom_ratio
pts = pts + np.tile( np.array([bbox[0],bbox[1]]), (NLMK,1) )
draw = ImageDraw.Draw(img)
for l in range(NLMK):
draw.ellipse((pts[l,0]-3,pts[l,1]-3,pts[l,0]+3,pts[l,1]+3), fill='white')
img.save(save_path)
del draw
if __name__ == '__main__':
# detect landmarks from static image
folder = 'AFW/'
# working path
model_path = WORKPATH + '/model/'
data_path = WORKPATH + '/data/'
result_path = WORKPATH + '/result/'
PXIO.DeleteThenCreateFolder(result_path + folder)
# load caffe model
caffe.set_mode_gpu()
caffe.set_device(1)
net = caffe.Net(model_path+'deploy.prototxt', model_path+'/deploy_weights.caffemodel', caffe.TEST)
# load image and bbox list, bbox = [left,top,right,bottom]
img_bbox = PXIO.ReadLineFromFile(data_path + folder + 'img_bbox.txt')
for line in img_bbox:
token = line.split(' ')
img_path = token[0]
img_name = img_path.split('/')[-1]
print img_name
bbox = map(int, token[1:])
save_path = result_path + folder + img_name[:-4] + '.png'
# detect landmarks
try:
DetectImage(img_path,bbox,save_path)
except:
print img_name + ': detect abnormal!'