align_trans.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import cv2
  2. import numpy as np
  3. from .matlab_cp2tform import get_similarity_transform_for_cv2
  4. # reference facial points, a list of coordinates (x,y)
  5. REFERENCE_FACIAL_POINTS = [
  6. [30.29459953, 51.69630051],
  7. [65.53179932, 51.50139999],
  8. [48.02519989, 71.73660278],
  9. [33.54930115, 92.3655014],
  10. [62.72990036, 92.20410156],
  11. ]
  12. DEFAULT_CROP_SIZE = (96, 112)
  13. class FaceWarpException(Exception):
  14. def __str__(self):
  15. return "In File {}:{}".format(__file__, super.__str__(self))
  16. def get_reference_facial_points(
  17. output_size=None,
  18. inner_padding_factor=0.0,
  19. outer_padding=(0, 0),
  20. default_square=False,
  21. ):
  22. """
  23. Function:
  24. ----------
  25. get reference 5 key points according to crop settings:
  26. 0. Set default crop_size:
  27. if default_square:
  28. crop_size = (112, 112)
  29. else:
  30. crop_size = (96, 112)
  31. 1. Pad the crop_size by inner_padding_factor in each side;
  32. 2. Resize crop_size into (output_size - outer_padding*2),
  33. pad into output_size with outer_padding;
  34. 3. Output reference_5point;
  35. Parameters:
  36. ----------
  37. @output_size: (w, h) or None
  38. size of aligned face image
  39. @inner_padding_factor: (w_factor, h_factor)
  40. padding factor for inner (w, h)
  41. @outer_padding: (w_pad, h_pad)
  42. each row is a pair of coordinates (x, y)
  43. @default_square: True or False
  44. if True:
  45. default crop_size = (112, 112)
  46. else:
  47. default crop_size = (96, 112);
  48. !!! make sure, if output_size is not None:
  49. (output_size - outer_padding)
  50. = some_scale * (default crop_size * (1.0 +
  51. inner_padding_factor))
  52. Returns:
  53. ----------
  54. @reference_5point: 5x2 np.array
  55. each row is a pair of transformed coordinates (x, y)
  56. """
  57. tmp_5pts = np.array(REFERENCE_FACIAL_POINTS)
  58. tmp_crop_size = np.array(DEFAULT_CROP_SIZE)
  59. # 0) make the inner region a square
  60. if default_square:
  61. size_diff = max(tmp_crop_size) - tmp_crop_size
  62. tmp_5pts += size_diff / 2
  63. tmp_crop_size += size_diff
  64. if (
  65. output_size
  66. and output_size[0] == tmp_crop_size[0]
  67. and output_size[1] == tmp_crop_size[1]
  68. ):
  69. return tmp_5pts
  70. if inner_padding_factor == 0 and outer_padding == (0, 0):
  71. if output_size is None:
  72. return tmp_5pts
  73. else:
  74. raise FaceWarpException(
  75. "No paddings to do, output_size must be None or {}".format(
  76. tmp_crop_size
  77. )
  78. )
  79. # check output size
  80. if not (0 <= inner_padding_factor <= 1.0):
  81. raise FaceWarpException("Not (0 <= inner_padding_factor <= 1.0)")
  82. if (
  83. inner_padding_factor > 0 or outer_padding[0] > 0 or outer_padding[1] > 0
  84. ) and output_size is None:
  85. output_size = tmp_crop_size * (1 + inner_padding_factor * 2).astype(np.int32)
  86. output_size += np.array(outer_padding)
  87. if not (outer_padding[0] < output_size[0] and outer_padding[1] < output_size[1]):
  88. raise FaceWarpException(
  89. "Not (outer_padding[0] < output_size[0] and outer_padding[1] < output_size[1])"
  90. )
  91. # 1) pad the inner region according inner_padding_factor
  92. if inner_padding_factor > 0:
  93. size_diff = tmp_crop_size * inner_padding_factor * 2
  94. tmp_5pts += size_diff / 2
  95. tmp_crop_size += np.round(size_diff).astype(np.int32)
  96. # 2) resize the padded inner region
  97. size_bf_outer_pad = np.array(output_size) - np.array(outer_padding) * 2
  98. if (
  99. size_bf_outer_pad[0] * tmp_crop_size[1]
  100. != size_bf_outer_pad[1] * tmp_crop_size[0]
  101. ):
  102. raise FaceWarpException(
  103. "Must have (output_size - outer_padding)"
  104. "= some_scale * (crop_size * (1.0 + inner_padding_factor)"
  105. )
  106. scale_factor = size_bf_outer_pad[0].astype(np.float32) / tmp_crop_size[0]
  107. tmp_5pts = tmp_5pts * scale_factor
  108. # size_diff = tmp_crop_size * (scale_factor - min(scale_factor))
  109. # tmp_5pts = tmp_5pts + size_diff / 2
  110. tmp_crop_size = size_bf_outer_pad
  111. # 3) add outer_padding to make output_size
  112. reference_5point = tmp_5pts + np.array(outer_padding)
  113. tmp_crop_size = output_size
  114. return reference_5point
  115. def get_affine_transform_matrix(src_pts, dst_pts):
  116. """
  117. Function:
  118. ----------
  119. get affine transform matrix 'tfm' from src_pts to dst_pts
  120. Parameters:
  121. ----------
  122. @src_pts: Kx2 np.array
  123. source points matrix, each row is a pair of coordinates (x, y)
  124. @dst_pts: Kx2 np.array
  125. destination points matrix, each row is a pair of coordinates (x, y)
  126. Returns:
  127. ----------
  128. @tfm: 2x3 np.array
  129. transform matrix from src_pts to dst_pts
  130. """
  131. tfm = np.float32([[1, 0, 0], [0, 1, 0]])
  132. n_pts = src_pts.shape[0]
  133. ones = np.ones((n_pts, 1), src_pts.dtype)
  134. src_pts_ = np.hstack([src_pts, ones])
  135. dst_pts_ = np.hstack([dst_pts, ones])
  136. A, res, rank, s = np.linalg.lstsq(src_pts_, dst_pts_)
  137. if rank == 3:
  138. tfm = np.float32([[A[0, 0], A[1, 0], A[2, 0]], [A[0, 1], A[1, 1], A[2, 1]]])
  139. elif rank == 2:
  140. tfm = np.float32([[A[0, 0], A[1, 0], 0], [A[0, 1], A[1, 1], 0]])
  141. return tfm
  142. def warp_and_crop_face(
  143. src_img, facial_pts, reference_pts=None, crop_size=(96, 112), align_type="smilarity"
  144. ):
  145. """
  146. Function:
  147. ----------
  148. apply affine transform 'trans' to uv
  149. Parameters:
  150. ----------
  151. @src_img: 3x3 np.array
  152. input image
  153. @facial_pts: could be
  154. 1)a list of K coordinates (x,y)
  155. or
  156. 2) Kx2 or 2xK np.array
  157. each row or col is a pair of coordinates (x, y)
  158. @reference_pts: could be
  159. 1) a list of K coordinates (x,y)
  160. or
  161. 2) Kx2 or 2xK np.array
  162. each row or col is a pair of coordinates (x, y)
  163. or
  164. 3) None
  165. if None, use default reference facial points
  166. @crop_size: (w, h)
  167. output face image size
  168. @align_type: transform type, could be one of
  169. 1) 'similarity': use similarity transform
  170. 2) 'cv2_affine': use the first 3 points to do affine transform,
  171. by calling cv2.getAffineTransform()
  172. 3) 'affine': use all points to do affine transform
  173. Returns:
  174. ----------
  175. @face_img: output face image with size (w, h) = @crop_size
  176. """
  177. if reference_pts is None:
  178. if crop_size[0] == 96 and crop_size[1] == 112:
  179. reference_pts = REFERENCE_FACIAL_POINTS
  180. else:
  181. default_square = False
  182. inner_padding_factor = 0
  183. outer_padding = (0, 0)
  184. output_size = crop_size
  185. reference_pts = get_reference_facial_points(
  186. output_size, inner_padding_factor, outer_padding, default_square
  187. )
  188. ref_pts = np.float32(reference_pts)
  189. ref_pts_shp = ref_pts.shape
  190. if max(ref_pts_shp) < 3 or min(ref_pts_shp) != 2:
  191. raise FaceWarpException("reference_pts.shape must be (K,2) or (2,K) and K>2")
  192. if ref_pts_shp[0] == 2:
  193. ref_pts = ref_pts.T
  194. src_pts = np.float32(facial_pts)
  195. src_pts_shp = src_pts.shape
  196. if max(src_pts_shp) < 3 or min(src_pts_shp) != 2:
  197. raise FaceWarpException("facial_pts.shape must be (K,2) or (2,K) and K>2")
  198. if src_pts_shp[0] == 2:
  199. src_pts = src_pts.T
  200. if src_pts.shape != ref_pts.shape:
  201. raise FaceWarpException("facial_pts and reference_pts must have the same shape")
  202. if align_type == "cv2_affine":
  203. tfm = cv2.getAffineTransform(src_pts[0:3], ref_pts[0:3])
  204. elif align_type == "affine":
  205. tfm = get_affine_transform_matrix(src_pts, ref_pts)
  206. else:
  207. tfm = get_similarity_transform_for_cv2(src_pts, ref_pts)
  208. face_img = cv2.warpAffine(src_img, tfm, (crop_size[0], crop_size[1]))
  209. return face_img