def fit_submanifold_landmarks_to_image(template, original, Xlm, face_d, face_p, landmarks=list(range(68))): ''' Fit the submanifold to the template and take the top-K. Xlm is a N x 68 x 2 list of landmarks. ''' lossX = numpy.empty((len(Xlm), ), dtype=numpy.float64) MX = numpy.empty((len(Xlm), 2, 3), dtype=numpy.float64) nfail = 0 for i in range(len(Xlm)): lm = Xlm[i] try: M, loss = alignface.fit_face_landmarks( Xlm[i], template, landmarks=landmarks, image_dims=original.shape[:2]) lossX[i] = loss MX[i] = M except alignface.FitError: lossX[i] = float('inf') MX[i] = 0 nfail += 1 if nfail > 1: print('fit submanifold, {} errors.'.format(nfail)) a = numpy.argsort(lossX) return a, lossX, MX
responseid = args[1] args = args[2:] if command == 'ping': channel.send((('okay', responseid), {})) elif command == 'forward_landmarks': # X is pickled N x H x W x 3 images # Xlm is pickled N x 68 x 2 landmarks # scores is pickled N x K attribute scores X = pickle.loads(kwargs['X']) Xlm = pickle.loads(kwargs['Xlm']) result = [] for x, lm in zip(X, Xlm): # lm,x=alignface.detect_landmarks('../../images/jpg-816x1000/vvdd-11.jpg',face_d,face_p) M, loss = alignface.fit_face_landmarks( lm, fm.celeba_template, image_dims=[160, 160], twoscale=False) Y = alignface.warp_to_template(x, M, image_dims=[160, 160]) Y = numpy.expand_dims(Y, 0) Y = fm.preprocess(Y) Y = fm.forward(Y) scores = fm.predict_attributes(Y) result.append(scores[0]) result = numpy.asarray(result) channel.send((('okay', responseid), { 'scores': pickle.dumps(result) })) elif command == 'forward_images': # X is pickled list of N strings # scores is pickled N x K attributes scores