Skip to content

convex_hull

convex_hull

ConvexHullSkinSegmentation

Bases: SkinSegmentation

Source code in redesign_pipeline/skin_segmentation/convex_hull.py
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
class ConvexHullSkinSegmentation(SkinSegmentation):

    def __init__(self):
        super().__init__()

    def get_facemesh_coords(self, img, landmark_list):
        """
        Convert the face landmarks to image coordinates.

        Args:
            img (numpy.ndarray): Input image.
            landmark_list (list): List of face landmarks.

        Returns:
            numpy.ndarray: Face landmarks in image coordinates.
        """
        h,w = img.shape[:2]
        xyz = []
        for lm in landmark_list:
            xyz.append((lm.y, lm.x))
        return np.multiply(xyz, [h,w]).astype(np.float32)

    def extraction_mask(self, image, multi_face_landmarks):
        """
        Generate a skin extraction mask based on face landmarks.

        Args:
            image (numpy.ndarray): Input image.
            multi_face_landmarks (list): List of face landmarks.

        Returns:
            numpy.ndarray: Skin extraction mask.
        """

        ### face landmarks ###
        ldmks = []
        if multi_face_landmarks:
            face_landmarks = multi_face_landmarks[0]
            landmarks = [l for l in face_landmarks.landmark]
            ldmks = self.get_facemesh_coords(image, landmarks)

        # face_mask convex hull 

        aviable_ldmks = ldmks[ldmks[:,0] >= 0][:,:]
        hull = ConvexHull(aviable_ldmks)
        verts = [(aviable_ldmks[v,0], aviable_ldmks[v,1]) for v in hull.vertices]
        img = Image.new('L', image.shape[:2], 0)
        ImageDraw.Draw(img).polygon(verts, outline=1, fill=1)
        mask = np.array(img)
        mask = np.expand_dims(mask,axis=0).T

        # left eye convex hull
        left_eye_ldmks = ldmks[MagicLandmarks.left_eye]
        aviable_ldmks = left_eye_ldmks[left_eye_ldmks[:,0] >= 0][:,:]
        if len(aviable_ldmks) > 3:
            hull = ConvexHull(aviable_ldmks)
            verts = [(aviable_ldmks[v,0], aviable_ldmks[v,1]) for v in hull.vertices]
            img = Image.new('L', image.shape[:2], 0)
            ImageDraw.Draw(img).polygon(verts, outline=1, fill=1)
            left_eye_mask = np.array(img)
            left_eye_mask = np.expand_dims(left_eye_mask,axis=0).T
        else:
            left_eye_mask = np.ones((image.shape[0], image.shape[1],1),dtype=np.uint8)

        # right eye convex hull
        right_eye_ldmks = ldmks[MagicLandmarks.right_eye]
        aviable_ldmks = right_eye_ldmks[right_eye_ldmks[:,0] >= 0][:,:]
        if len(aviable_ldmks) > 3:
            hull = ConvexHull(aviable_ldmks)
            verts = [(aviable_ldmks[v,0], aviable_ldmks[v,1]) for v in hull.vertices]
            img = Image.new('L', image.shape[:2], 0)
            ImageDraw.Draw(img).polygon(verts, outline=1, fill=1)
            right_eye_mask = np.array(img)
            right_eye_mask = np.expand_dims(right_eye_mask,axis=0).T
        else:
            right_eye_mask = np.ones((image.shape[0], image.shape[1],1),dtype=np.uint8)

        # mounth convex hull
        mounth_ldmks = ldmks[MagicLandmarks.mounth]
        aviable_ldmks = mounth_ldmks[mounth_ldmks[:,0] >= 0][:,:]
        if len(aviable_ldmks) > 3:
            hull = ConvexHull(aviable_ldmks)
            verts = [(aviable_ldmks[v,0], aviable_ldmks[v,1]) for v in hull.vertices]
            img = Image.new('L', image.shape[:2], 0)
            ImageDraw.Draw(img).polygon(verts, outline=1, fill=1)
            mounth_mask = np.array(img)
            mounth_mask = np.expand_dims(mounth_mask,axis=0).T
        else:
            mounth_mask = np.ones((image.shape[0], image.shape[1],1),dtype=np.uint8)

        # return mask
        final_mask =  mask * (1-left_eye_mask) * (1-right_eye_mask) * (1-mounth_mask)

        return final_mask

extraction_mask(image, multi_face_landmarks)

Generate a skin extraction mask based on face landmarks.

Parameters:

Name Type Description Default
image numpy.ndarray

Input image.

required
multi_face_landmarks list

List of face landmarks.

required

Returns:

Type Description

numpy.ndarray: Skin extraction mask.

Source code in redesign_pipeline/skin_segmentation/convex_hull.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def extraction_mask(self, image, multi_face_landmarks):
    """
    Generate a skin extraction mask based on face landmarks.

    Args:
        image (numpy.ndarray): Input image.
        multi_face_landmarks (list): List of face landmarks.

    Returns:
        numpy.ndarray: Skin extraction mask.
    """

    ### face landmarks ###
    ldmks = []
    if multi_face_landmarks:
        face_landmarks = multi_face_landmarks[0]
        landmarks = [l for l in face_landmarks.landmark]
        ldmks = self.get_facemesh_coords(image, landmarks)

    # face_mask convex hull 

    aviable_ldmks = ldmks[ldmks[:,0] >= 0][:,:]
    hull = ConvexHull(aviable_ldmks)
    verts = [(aviable_ldmks[v,0], aviable_ldmks[v,1]) for v in hull.vertices]
    img = Image.new('L', image.shape[:2], 0)
    ImageDraw.Draw(img).polygon(verts, outline=1, fill=1)
    mask = np.array(img)
    mask = np.expand_dims(mask,axis=0).T

    # left eye convex hull
    left_eye_ldmks = ldmks[MagicLandmarks.left_eye]
    aviable_ldmks = left_eye_ldmks[left_eye_ldmks[:,0] >= 0][:,:]
    if len(aviable_ldmks) > 3:
        hull = ConvexHull(aviable_ldmks)
        verts = [(aviable_ldmks[v,0], aviable_ldmks[v,1]) for v in hull.vertices]
        img = Image.new('L', image.shape[:2], 0)
        ImageDraw.Draw(img).polygon(verts, outline=1, fill=1)
        left_eye_mask = np.array(img)
        left_eye_mask = np.expand_dims(left_eye_mask,axis=0).T
    else:
        left_eye_mask = np.ones((image.shape[0], image.shape[1],1),dtype=np.uint8)

    # right eye convex hull
    right_eye_ldmks = ldmks[MagicLandmarks.right_eye]
    aviable_ldmks = right_eye_ldmks[right_eye_ldmks[:,0] >= 0][:,:]
    if len(aviable_ldmks) > 3:
        hull = ConvexHull(aviable_ldmks)
        verts = [(aviable_ldmks[v,0], aviable_ldmks[v,1]) for v in hull.vertices]
        img = Image.new('L', image.shape[:2], 0)
        ImageDraw.Draw(img).polygon(verts, outline=1, fill=1)
        right_eye_mask = np.array(img)
        right_eye_mask = np.expand_dims(right_eye_mask,axis=0).T
    else:
        right_eye_mask = np.ones((image.shape[0], image.shape[1],1),dtype=np.uint8)

    # mounth convex hull
    mounth_ldmks = ldmks[MagicLandmarks.mounth]
    aviable_ldmks = mounth_ldmks[mounth_ldmks[:,0] >= 0][:,:]
    if len(aviable_ldmks) > 3:
        hull = ConvexHull(aviable_ldmks)
        verts = [(aviable_ldmks[v,0], aviable_ldmks[v,1]) for v in hull.vertices]
        img = Image.new('L', image.shape[:2], 0)
        ImageDraw.Draw(img).polygon(verts, outline=1, fill=1)
        mounth_mask = np.array(img)
        mounth_mask = np.expand_dims(mounth_mask,axis=0).T
    else:
        mounth_mask = np.ones((image.shape[0], image.shape[1],1),dtype=np.uint8)

    # return mask
    final_mask =  mask * (1-left_eye_mask) * (1-right_eye_mask) * (1-mounth_mask)

    return final_mask

get_facemesh_coords(img, landmark_list)

Convert the face landmarks to image coordinates.

Parameters:

Name Type Description Default
img numpy.ndarray

Input image.

required
landmark_list list

List of face landmarks.

required

Returns:

Type Description

numpy.ndarray: Face landmarks in image coordinates.

Source code in redesign_pipeline/skin_segmentation/convex_hull.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def get_facemesh_coords(self, img, landmark_list):
    """
    Convert the face landmarks to image coordinates.

    Args:
        img (numpy.ndarray): Input image.
        landmark_list (list): List of face landmarks.

    Returns:
        numpy.ndarray: Face landmarks in image coordinates.
    """
    h,w = img.shape[:2]
    xyz = []
    for lm in landmark_list:
        xyz.append((lm.y, lm.x))
    return np.multiply(xyz, [h,w]).astype(np.float32)

MagicLandmarks

This class contains usefull lists of landmarks identification numbers.

Source code in redesign_pipeline/skin_segmentation/convex_hull.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class MagicLandmarks():
    """
    This class contains usefull lists of landmarks identification numbers.
    """
    high_prio_forehead = [10, 67, 69, 104, 108, 109, 151, 299, 337, 338]
    high_prio_nose = [3, 4, 5, 6, 45, 51, 115, 122, 131, 134, 142, 174, 195, 196, 197, 198,
                      209, 217, 220, 236, 248, 275, 277, 281, 360, 363, 399, 419, 420, 429, 437, 440, 456]
    high_prio_left_cheek = [36, 47, 50, 100, 101, 116, 117,
                            118, 119, 123, 126, 147, 187, 203, 205, 206, 207, 216]
    high_prio_right_cheek = [266, 280, 329, 330, 346, 347,
                             347, 348, 355, 371, 411, 423, 425, 426, 427, 436]

    mid_prio_forehead = [8, 9, 21, 68, 103, 251,
                         284, 297, 298, 301, 332, 333, 372, 383]
    mid_prio_nose = [1, 44, 49, 114, 120, 121, 128, 168, 188, 351, 358, 412]
    mid_prio_left_cheek = [34, 111, 137, 156, 177, 192, 213, 227, 234]
    mid_prio_right_cheek = [340, 345, 352, 361, 454]
    mid_prio_chin = [135, 138, 169, 170, 199, 208, 210, 211,
                     214, 262, 288, 416, 428, 430, 431, 432, 433, 434]
    mid_prio_mouth = [92, 164, 165, 167, 186, 212, 322, 391, 393, 410]
    # more specific areas
    forehead_left = [21, 71, 68, 54, 103, 104, 63, 70,
                     53, 52, 65, 107, 66, 108, 69, 67, 109, 105]
    forehead_center = [10, 151, 9, 8, 107, 336, 285, 55, 8]
    forehoead_right = [338, 337, 336, 296, 285, 295, 282,
                       334, 293, 301, 251, 298, 333, 299, 297, 332, 284]
    eye_right = [283, 300, 368, 353, 264, 372, 454, 340, 448,
                 450, 452, 464, 417, 441, 444, 282, 276, 446, 368]
    eye_left = [127, 234, 34, 139, 70, 53, 124,
                35, 111, 228, 230, 121, 244, 189, 222, 143]
    nose = [193, 417, 168, 188, 6, 412, 197, 174, 399, 456,
            195, 236, 131, 51, 281, 360, 440, 4, 220, 219, 305]
    mounth_up = [186, 92, 167, 393, 322, 410, 287, 39, 269, 61, 164]
    mounth_down = [43, 106, 83, 18, 406, 335, 273, 424, 313, 194, 204]
    chin = [204, 170, 140, 194, 201, 171, 175,
            200, 418, 396, 369, 421, 431, 379, 424]
    cheek_left_bottom = [215, 138, 135, 210, 212, 57, 216, 207, 192]
    cheek_right_bottom = [435, 427, 416, 364,
                          394, 422, 287, 410, 434, 436]
    cheek_left_top = [116, 111, 117, 118, 119, 100, 47, 126, 101, 123,
                      137, 177, 50, 36, 209, 129, 205, 147, 177, 215, 187, 207, 206, 203]
    cheek_right_top = [349, 348, 347, 346, 345, 447, 323,
                       280, 352, 330, 371, 358, 423, 426, 425, 427, 411, 376]
    # dense zones used for convex hull masks
    left_eye = [157,144, 145, 22, 23, 25, 154, 31, 160, 33, 46, 52, 53, 55, 56, 189, 190, 63, 65, 66, 70, 221, 222, 223, 225, 226, 228, 229, 230, 231, 232, 105, 233, 107, 243, 124]
    right_eye = [384, 385, 386, 259, 388, 261, 265, 398, 276, 282, 283, 285, 413, 293, 296, 300, 441, 442, 445, 446, 449, 451, 334, 463, 336, 464, 467, 339, 341, 342, 353, 381, 373, 249, 253, 255]
    mounth = [391, 393, 11, 269, 270, 271, 287, 164, 165, 37, 167, 40, 43, 181, 313, 314, 186, 57, 315, 61, 321, 73, 76, 335, 83, 85, 90, 106]
    # equispaced facial points - mouth and eyes are excluded.
    equispaced_facial_points = [2, 3, 4, 5, 6, 8, 9, 10, 18, 21, 32, 35, 36, 43, 46, 47, 48, 50, 54, \
             58, 67, 68, 69, 71, 92, 93, 101, 103, 104, 108, 109, 116, 117, \
             118, 123, 132, 134, 135, 138, 139, 142, 148, 149, 150, 151, 152, 182, 187, 188, 193, 197, 201, 205, 206, 207, \
             210, 211, 212, 216, 234, 248, 251, 262, 265, 266, 273, 277, 278, 280, \
             284, 288, 297, 299, 322, 323, 330, 332, 333, 337, 338, 345, \
             346, 361, 363, 364, 367, 368, 371, 377, 379, 411, 412, 417, 421, 425, 426, 427, 430, 432, 436]