Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # это файл из ветки stylegan
- from dataloaders.voxceleb2_segmentation_nolandmarks import bbox_to_integer_coords, crop_with_padding
- import cv2
- import dlib
- face_detector = dlib.get_frontal_face_detector()
- def crop_as_in_voxceleb1(image):
- """
- image:
- np.uint8, (H x W x 3)
- return:
- np.uint8, (256 x 256 x 3)
- """
- bboxes = self.face_detector(image)
- if bboxes:
- bbox = bboxes[0]
- l_dlib, t_dlib, r_dlib, b_dlib = bbox.left(), bbox.top(), bbox.right(), bbox.bottom()
- center_x = (l_dlib + r_dlib) * 0.5
- center_y = (t_dlib + b_dlib) * 0.5
- size = r_dlib - l_dlib
- new_size = size * 1.672
- l_dlib, r_dlib = round(center_x-new_size/2), round(center_x+new_size/2+1)
- t_dlib, b_dlib = round(center_y-new_size/2), round(center_y+new_size/2+1)
- else:
- l, t, r, b = 0, 0, 1.0, 1.0
- # Make bbox square and scale it
- SCALE = 0.7083
- center_x, center_y = (l + r) * 0.5, (t + b) * 0.5
- height, width = b - t, r - l
- new_box_size = max(height, width)
- l = center_x - new_box_size / 2 * SCALE
- r = center_x + new_box_size / 2 * SCALE
- t = center_y - new_box_size / 2 * SCALE
- b = center_y + new_box_size / 2 * SCALE
- l_dlib, t_dlib, r_dlib, b_dlib = bbox_to_integer_coords(t, l, b, r, *image.shape[:2])
- image_cropped_voxceleb = crop_with_padding(image, t_dlib, l_dlib, b_dlib, r_dlib)
- image_cropped_voxceleb = cv2.resize(image_cropped_voxceleb, (256, 256),
- interpolation=cv2.INTER_CUBIC if 256 > b_dlib - r_dlib else cv2.INTER_AREA)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement