Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. # это файл из ветки stylegan
  2. from dataloaders.voxceleb2_segmentation_nolandmarks import bbox_to_integer_coords, crop_with_padding
  3.  
  4. import cv2
  5. import dlib
  6. face_detector = dlib.get_frontal_face_detector()
  7.  
  8. def crop_as_in_voxceleb1(image):
  9.     """
  10.        image:
  11.            np.uint8, (H x W x 3)
  12.  
  13.        return:
  14.            np.uint8, (256 x 256 x 3)
  15.    """
  16.     bboxes = self.face_detector(image)
  17.     if bboxes:
  18.         bbox = bboxes[0]
  19.         l_dlib, t_dlib, r_dlib, b_dlib = bbox.left(), bbox.top(), bbox.right(), bbox.bottom()
  20.  
  21.         center_x = (l_dlib + r_dlib) * 0.5
  22.         center_y = (t_dlib + b_dlib) * 0.5
  23.         size = r_dlib - l_dlib
  24.         new_size = size * 1.672
  25.         l_dlib, r_dlib = round(center_x-new_size/2), round(center_x+new_size/2+1)
  26.         t_dlib, b_dlib = round(center_y-new_size/2), round(center_y+new_size/2+1)
  27.     else:
  28.         l, t, r, b = 0, 0, 1.0, 1.0
  29.  
  30.         # Make bbox square and scale it
  31.         SCALE = 0.7083
  32.  
  33.         center_x, center_y = (l + r) * 0.5, (t + b) * 0.5
  34.         height, width = b - t, r - l
  35.         new_box_size = max(height, width)
  36.         l = center_x - new_box_size / 2 * SCALE
  37.         r = center_x + new_box_size / 2 * SCALE
  38.         t = center_y - new_box_size / 2 * SCALE
  39.         b = center_y + new_box_size / 2 * SCALE
  40.  
  41.         l_dlib, t_dlib, r_dlib, b_dlib = bbox_to_integer_coords(t, l, b, r, *image.shape[:2])
  42.  
  43.     image_cropped_voxceleb = crop_with_padding(image, t_dlib, l_dlib, b_dlib, r_dlib)
  44.     image_cropped_voxceleb = cv2.resize(image_cropped_voxceleb, (256, 256),
  45.         interpolation=cv2.INTER_CUBIC if 256 > b_dlib - r_dlib else cv2.INTER_AREA)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement