Skip to content
Permalink
Browse files

added transparent mask to draw_landmarks

  • Loading branch information...
iperov committed Mar 14, 2019
1 parent 5bd3f87 commit 9823421a44f3aacc754a6a3d4e36bfc195faa5c1
Showing with 18 additions and 11 deletions.
  1. +12 −5 facelib/LandmarksProcessor.py
  2. +3 −3 mainscripts/Extractor.py
  3. +1 −1 mainscripts/Util.py
  4. +2 −2 samples/SampleProcessor.py
@@ -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')

@@ -271,14 +272,20 @@ def draw_landmarks (image, image_landmarks, color=(0,255,0)):
# jaw big circles
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):
image_utils.draw_rect (image, rect, (255,0,0), 2 )
draw_landmarks(image, image_landmarks)

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 )

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):
@@ -141,7 +141,7 @@ def process_data(self, data):
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 @@ def on_result (self, host_dict, data, result):
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)
@@ -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] )
@@ -48,13 +48,13 @@ def process (sample, sample_process_options, output_sample_types, debug):
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 )

0 comments on commit 9823421

Please sign in to comment.
You can’t perform that action at this time.