def generate_mask_from_skin(face: Face) -> None: from itertools import product mask = np.zeros((IMG_SIZE, IMG_SIZE), dtype=np.bool) mark = np.zeros((IMG_SIZE, IMG_SIZE, 3), dtype=np.float) for x, y in product(range(IMG_SIZE), range(IMG_SIZE)): mark[x][y] = tools.rgb_skin_mark(*(face.rgb_img[x][y])) probe = [ mark[x][y] for x, y in product(range(2 * IMG_SIZE // 4, 3 * IMG_SIZE // 4, 8), range(2 * IMG_SIZE // 4, 3 * IMG_SIZE // 4, 8)) ] probe.sort(key=(lambda x: x[0]**2 + x[1]**2 + x[2]**2)) # Mark of middle [probably] SKIN element mid_y, mid_cr, mid_cy = probe[len(probe) // 2] for x, y in product(range(IMG_SIZE), range(IMG_SIZE)): y, cr, cy = mark[x][y] l_bound = 0.918 u_bound = 1.092 mask[x][y] = (0.3 * mid_y <= y <= 4 * mid_y) and ( l_bound * mid_cr <= cr <= u_bound * mid_cr) and (l_bound * mid_cy <= cy <= u_bound * mid_cy) face.mask = mask # Display the mask if you want tools.show_image(tools.pic_with_applied_mask(face.rgb_img, mask))
def generate_mask(face: Face, points: list((float, float, float))) -> None: mask = np.zeros((IMG_SIZE, IMG_SIZE), dtype=np.bool) for (xs, ys, _) in points: mask[int(xs), int(ys)] = True starting_point = (IMG_SIZE // 2, IMG_SIZE // 2 ) # TODO: maybe some specific landmark (like nose) sys.setrecursionlimit(100000) def _helper(px, py): if min(px, py) < 0 or max(px, py) >= IMG_SIZE: return if mask[px, py]: return mask[px, py] = True _helper(px - 1, py) _helper(px + 1, py) _helper(px, py - 1) _helper(px, py + 1) _helper(starting_point[0], starting_point[1]) face.mask = mask