diff --git a/SSH/test.py b/SSH/test.py index 4c3c755..26581db 100644 --- a/SSH/test.py +++ b/SSH/test.py @@ -7,9 +7,10 @@ from ssh_detector import SSHDetector long_max = 1200 t = 2 +detector = SSHDetector('./model/e2ef', 0) -f = 't2.jpg' +f = '../sample-images/t2.jpg' if len(sys.argv)>1: f = sys.argv[1] img = cv2.imread(f) @@ -18,7 +19,6 @@ if img.shape[0]>long_max or img.shape[1]>long_max: scale = float(long_max) / max(img.shape[0], img.shape[1]) img = cv2.resize(img, (0,0), fx=scale, fy=scale) print('resize to', img.shape) -detector = SSHDetector('./model/e2ef', 0) for i in xrange(t-1): #warmup faces = detector.detect(img) timea = datetime.datetime.now() diff --git a/alignment/alignment.py b/alignment/alignment.py new file mode 100644 index 0000000..a2c264c --- /dev/null +++ b/alignment/alignment.py @@ -0,0 +1,42 @@ +import argparse +import cv2 +import numpy as np +import sys +import mxnet as mx +import datetime + +class Alignment: + def __init__(self, prefix, epoch, ctx_id=0): + print('loading',prefix, epoch) + ctx = mx.gpu(ctx_id) + sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch) + all_layers = sym.get_internals() + sym = all_layers['heatmap_output'] + image_size = (128, 128) + self.image_size = image_size + model = mx.mod.Module(symbol=sym, context=ctx, label_names = None) + #model = mx.mod.Module(symbol=sym, context=ctx) + model.bind(for_training=False, data_shapes=[('data', (1, 3, image_size[0], image_size[1]))]) + model.set_params(arg_params, aux_params) + self.model = model + + def get(self, img): + rimg = cv2.resize(img, (self.image_size[1], self.image_size[0])) + img = cv2.cvtColor(rimg, cv2.COLOR_BGR2RGB) + img = np.transpose(img, (2,0,1)) #3*112*112, RGB + input_blob = np.zeros( (1, 3, self.image_size[1], self.image_size[0]),dtype=np.uint8 ) + input_blob[0] = img + data = mx.nd.array(input_blob) + db = mx.io.DataBatch(data=(data,)) + self.model.forward(db, is_train=False) + alabel = self.model.get_outputs()[-1].asnumpy()[0] + ret = np.zeros( (alabel.shape[0], 2), dtype=np.float32) + for i in xrange(alabel.shape[0]): + a = cv2.resize(alabel[i], (self.image_size[1], self.image_size[0])) + ind = np.unravel_index(np.argmax(a, axis=None), a.shape) + #ret[i] = (ind[0], ind[1]) #h, w + ret[i] = (ind[1], ind[0]) #w, h + return ret + + + diff --git a/alignment/test.py b/alignment/test.py new file mode 100644 index 0000000..273ed4c --- /dev/null +++ b/alignment/test.py @@ -0,0 +1,41 @@ +import cv2 +import sys +import numpy as np +import datetime +from alignment import Alignment +sys.path.append('../SSH') +from ssh_detector import SSHDetector + +long_max = 1200 +t = 2 + +detector = SSHDetector('../SSH/model/e2ef', 0) +alignment = Alignment('./model/3d_I5', 12) + +f = '../sample-images/t2.jpg' +if len(sys.argv)>1: + f = sys.argv[1] +img = cv2.imread(f) +print(img.shape) +if img.shape[0]>long_max or img.shape[1]>long_max: + scale = float(long_max) / max(img.shape[0], img.shape[1]) + img = cv2.resize(img, (0,0), fx=scale, fy=scale) + print('resize to', img.shape) +for i in xrange(t-1): #warmup + faces = detector.detect(img) +timea = datetime.datetime.now() +faces = detector.detect(img) +timeb = datetime.datetime.now() +diff = timeb - timea +print('detection uses', diff.total_seconds(), 'seconds') +print('find', faces.shape[0], 'faces') + +for face in faces: + w = face[2] - face[0] + h = face[3] - face[1] + size = int(max(w, h)*1.3) + wc = int( (face[2]+face[0])/2 ) + hc = int( (face[3]+face[1])/2 ) + ebox = cv2.getRectSubPix(img, (size, size), (wc, hc)) + landmark = alignment.get(ebox) + print(landmark.shape) diff --git a/sample-images/t2.jpg b/sample-images/t2.jpg new file mode 100644 index 0000000..dcca930 Binary files /dev/null and b/sample-images/t2.jpg differ