Advertisement
Guest User

Untitled

a guest
May 21st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. from face3d.morphable_model import MorphabelModel
  2. from face_alignment
  3.  
  4. self.bfm = MorphabelModel('/home/alex/Documents/stereo/BFM.mat')
  5. self.fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._2halfD, flip_input=False)
  6.  
  7.  
  8. def compute_face_description(self, images, lite_stereo=False):
  9.         def fit(landmarks):
  10.             h, w = images[0].shape[0], images[0].shape[1]
  11.             if landmarks and len(landmarks)==1 :
  12.                 x=np.zeros((landmarks[0].shape[0],2))
  13.                 x[:,:] = landmarks[0][:, 0:2]  # 2d keypoint, which can be detected from image
  14.                 x[:, 0] -= w / 2
  15.                 x[:, 1] -= h / 2
  16.                 x[:, 1] = -x[:, 1]
  17.                 X_ind = self.bfm.kpt_ind
  18.                 fitted_sp, fitted_ep, fitted_s, fitted_angles, fitted_t = self.bfm.fit(x, X_ind, max_iter=3, isShow=False)
  19.                 return fitted_angles
  20.             else:
  21.                 if landmarks:
  22.                     print("Detected landmarks for %d faces, on this stage only 1 should be selected" % len(landmarks))
  23.                 return None
  24.         #print(self.detected_faces)
  25.         start_time = time.time()
  26.         self.landmarks = [self.fa.get_landmarks_from_image(img[...,::-1])
  27.                           for img in images]
  28.         print('       descriptors found', time.time() - start_time)
  29.  
  30.         if not lite_stereo:
  31.             angles = [fit(land) for land in self.landmarks]
  32.         else:
  33.             angles = [fit(land) if i < len(images)//2 else [0,0,0] for i, land in enumerate(self.landmarks)]
  34.  
  35.         print('       angles calculated', time.time() - start_time)
  36.  
  37.         return angles
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement