blazeface_facemesh
blazeface_facemesh
BlazefaceFacemesh
Bases: FaceDetector
This class implements a face detection and landmark extraction algorithm based on the BlazeFace model. It extends the FaceDetector class and provides the functionality to detect and draw facial landmarks in images.
Methods
init
The constructor of the class takes an optional boolean argument tracking. It initializes the parent class FaceDetector and sets up the face mesh classifier using the face_mesh solution from the mp module. If tracking is set to True, the classifier is set up to track faces in video frames. If tracking is False, the classifier is set up to detect faces in still images. The class also sets up several lists of facial landmarks and two lists to store detection and tracking times.
fs_to_landmark_list
This method takes a list of facial landmarks fs and returns a list of landmarks with duplicates removed.
get_facemesh_coords
This method takes a set of face detection results results, a list of facial landmarks landmark_list, and an image img. It returns the x and y coordinates of the landmarks as a list of tuples. The coordinates are scaled to the size of the image.
detect_faces
This method takes an image as input and returns a set of face detection results. It first converts the image from BGR to RGB and then runs the face mesh classifier on the image. The detection time is recorded and stored in the det_times list.
coords_for_landmark
This method takes a set of face detection results results, a list of facial landmarks lm, and an image image. It returns the x and y coordinates of the landmarks sorted in a counterclockwise order.
rgb_means
This method takes a set of facial landmark coordinates coords and an image image. It returns the mean RGB values of the pixels within the region defined by the landmark coordinates.
draw_landmarks
This method takes a set of face detection results results, a list of facial landmarks lm_list, and an image image. It draws the facial landmarks on the image and returns the result.
Source code in redesign_pipeline/face_detectors/blazeface_facemesh.py
49 50 51 52 53 54 55 56 57 58 59 60 61 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
__init__(tracking=False)
Initializes a new instance of the BlazefaceFacemesh class.
bool
A boolean value indicating whether to enable tracking (default: False).
Source code in redesign_pipeline/face_detectors/blazeface_facemesh.py
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 | |
coords_for_landmark(results, lm, image)
Extracts the coordinates of the specified landmark and returns them as a numpy array.
Parameters
Any
The results of the BlazeFace Mesh detection.
List[int]
The indices of the landmarks whose coordinates are to be extracted.
np.ndarray
The input image containing the face.
Returns
np.ndarray A numpy array containing the coordinates of the specified landmark.
Source code in redesign_pipeline/face_detectors/blazeface_facemesh.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
detect_faces(image)
Detects faces in an image using BlazefaceFacemesh.
- image (np.ndarray): An image.
- The results of the facemesh detection.
Source code in redesign_pipeline/face_detectors/blazeface_facemesh.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
draw_landmarks(results, lm_list, image, masks)
Draws the face mesh landmarks on the given image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results |
dict
|
The dictionary containing the results of the face mesh detection. |
required |
lm_list |
List[int]
|
The list of landmarks to draw. |
required |
image |
numpy.ndarray
|
The input image. |
required |
masks |
List[Union[None, numpy.ndarray]]
|
A list of masks to apply to the image. |
required |
Returns:
| Type | Description |
|---|---|
np.ndarray
|
numpy.ndarray: The image with the face mesh landmarks drawn on it. |
Source code in redesign_pipeline/face_detectors/blazeface_facemesh.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
fs_to_landmark_list(fs)
Converts a facemesh landmark list from a list of pairs to a list of integers.
Args
fs: (List[List[int]]): A facemesh landmark list.
Returns
A list of integers representing the facemesh landmarks.
Source code in redesign_pipeline/face_detectors/blazeface_facemesh.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
get_facemesh_coords(results, landmark_list, img)
Returns the x, y coordinates of the facemesh landmarks.
- results (any): The results of the facemesh detection.
- landmark_list (List[int]): A list of integers representing the facemesh landmarks.
- img (np.ndarray): An image.
- An array of x, y coordinates of the facemesh landmarks.
Source code in redesign_pipeline/face_detectors/blazeface_facemesh.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
rgb_means(coords, image, masks)
Calculates the RGB means of a given region of interest in an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords |
numpy.ndarray
|
The coordinates of the region of interest in the image. |
required |
image |
numpy.ndarray
|
The input image. |
required |
masks |
List[Union[None, numpy.ndarray]]
|
A list of masks to apply to the image. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[float, float, float]
|
Tuple[float, float, float]: The RGB means of the region of interest in the image. |
Source code in redesign_pipeline/face_detectors/blazeface_facemesh.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |