added transparent mask to draw_landmarks

This commit is contained in:
iperov 2019-03-14 12:16:21 +04:00
parent 5bd3f875fa
commit 9823421a44
4 changed files with 18 additions and 11 deletions

View file

@ -247,7 +247,8 @@ def mirror_landmarks (landmarks, val):
result[:,0] = val - result[:,0] - 1
return result
def draw_landmarks (image, image_landmarks, color=(0,255,0)):
def draw_landmarks (image, image_landmarks, color=(0,255,0), transparent_mask=False):
image = image.copy()
if len(image_landmarks) != 68:
raise Exception('get_image_eye_mask works only with 68 landmarks')
@ -272,13 +273,19 @@ def draw_landmarks (image, image_landmarks, color=(0,255,0)):
for x, y in jaw:
cv2.circle(image, (x, y), 2, color, lineType=cv2.LINE_AA)
def draw_rect_landmarks (image, rect, image_landmarks, face_size, face_type):
if transparent_mask:
mask = get_image_hull_mask (image.shape, image_landmarks)
image = image * (1-mask) + image * mask / 2
return image
def draw_rect_landmarks (image, rect, image_landmarks, face_size, face_type, transparent_mask=False):
image = draw_landmarks(image, image_landmarks, transparent_mask=transparent_mask)
image_utils.draw_rect (image, rect, (255,0,0), 2 )
draw_landmarks(image, image_landmarks)
image_to_face_mat = get_transform_mat (image_landmarks, face_size, face_type)
points = transform_points ( [ (0,0), (0,face_size-1), (face_size-1, face_size-1), (face_size-1,0) ], image_to_face_mat, True)
image_utils.draw_polygon (image, points, (0,0,255), 2)
return image
def calc_face_pitch(landmarks):
if not isinstance(landmarks, np.ndarray):

View file

@ -141,7 +141,7 @@ class ExtractSubprocessor(Subprocessor):
continue
if self.debug:
LandmarksProcessor.draw_rect_landmarks (debug_image, rect, image_landmarks, self.image_size, self.face_type)
debug_image = LandmarksProcessor.draw_rect_landmarks (debug_image, rect, image_landmarks, self.image_size, self.face_type, transparent_mask=True)
output_file = '{}_{}{}'.format(str(self.output_path / filename_path.stem), str(face_idx), '.jpg')
face_idx += 1
@ -457,10 +457,10 @@ class ExtractSubprocessor(Subprocessor):
image = cv2.warpAffine(image, mat,(w,h) )
view_landmarks = LandmarksProcessor.transform_points (view_landmarks, mat)
LandmarksProcessor.draw_rect_landmarks (image, view_rect, view_landmarks, self.image_size, self.face_type)
image = LandmarksProcessor.draw_rect_landmarks (image, view_rect, view_landmarks, self.image_size, self.face_type)
if self.rect_locked:
LandmarksProcessor.draw_landmarks(image, view_landmarks, (255,255,0) )
image = LandmarksProcessor.draw_landmarks(image, view_landmarks, (255,255,0) )
self.redraw_needed = False
io.show_image (self.wnd_name, image)

View file

@ -63,7 +63,7 @@ def add_landmarks_debug_images(input_path):
if img is not None:
face_landmarks = dflimg.get_landmarks()
LandmarksProcessor.draw_landmarks(img, face_landmarks)
img = LandmarksProcessor.draw_landmarks(img, face_landmarks, transparent_mask=True)
output_file = '{}{}'.format( str(Path(str(input_path)) / filepath.stem), '_debug.jpg')
cv2_imwrite(output_file, img, [int(cv2.IMWRITE_JPEG_QUALITY), 50] )

View file

@ -48,13 +48,13 @@ class SampleProcessor(object):
is_face_sample = sample.landmarks is not None
if debug and is_face_sample:
LandmarksProcessor.draw_landmarks (sample_bgr, sample.landmarks, (0, 1, 0))
sample_bgr = LandmarksProcessor.draw_landmarks (sample_bgr, sample.landmarks, (0, 1, 0))
close_sample = sample.close_target_list[ np.random.randint(0, len(sample.close_target_list)) ] if sample.close_target_list is not None else None
close_sample_bgr = close_sample.load_bgr() if close_sample is not None else None
if debug and close_sample_bgr is not None:
LandmarksProcessor.draw_landmarks (close_sample_bgr, close_sample.landmarks, (0, 1, 0))
close_sample_bgr = LandmarksProcessor.draw_landmarks (close_sample_bgr, close_sample.landmarks, (0, 1, 0))
params = image_utils.gen_warp_params(sample_bgr, sample_process_options.random_flip, rotation_range=sample_process_options.rotation_range, scale_range=sample_process_options.scale_range, tx_range=sample_process_options.tx_range, ty_range=sample_process_options.ty_range )