Spaces:
Running
Running
File size: 12,241 Bytes
da3a0ac b46d78e a82aad5 b46d78e a82aad5 b46d78e 433e26f b46d78e 433e26f b46d78e 433e26f b46d78e 433e26f b46d78e 433e26f b46d78e 433e26f b46d78e 433e26f b46d78e 433e26f b46d78e da3a0ac b46d78e a82aad5 b46d78e a82aad5 b46d78e a82aad5 b46d78e da3a0ac b46d78e 433e26f da3a0ac b46d78e da3a0ac b46d78e a82aad5 b46d78e 433e26f b46d78e a82aad5 b46d78e 433e26f da3a0ac b46d78e 433e26f b46d78e da3a0ac b46d78e 433e26f da3a0ac b46d78e da3a0ac b46d78e da3a0ac b46d78e 433e26f b46d78e da3a0ac a82aad5 da3a0ac b46d78e 433e26f b46d78e a82aad5 b46d78e da3a0ac b46d78e | 1 2 3 4 5 6 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 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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | """Facial landmark extraction using MediaPipe Face Mesh v2."""
from __future__ import annotations
import logging
from dataclasses import dataclass
from pathlib import Path
import cv2
import mediapipe as mp
import numpy as np
logger = logging.getLogger(__name__)
# Region color map for visualization (BGR)
REGION_COLORS: dict[str, tuple[int, int, int]] = {
"jawline": (255, 255, 255), # white
"eyebrow_left": (0, 255, 0), # green
"eyebrow_right": (0, 255, 0),
"eye_left": (255, 255, 0), # cyan
"eye_right": (255, 255, 0),
"nose": (0, 255, 255), # yellow
"lips": (0, 0, 255), # red
"iris_left": (255, 0, 255), # magenta
"iris_right": (255, 0, 255),
}
# MediaPipe landmark index groups by anatomical region
LANDMARK_REGIONS: dict[str, list[int]] = {
"jawline": [
10,
338,
297,
332,
284,
251,
389,
356,
454,
323,
361,
288,
397,
365,
379,
378,
400,
377,
152,
148,
176,
149,
150,
136,
172,
58,
132,
93,
234,
127,
162,
21,
54,
103,
67,
109,
],
"eye_left": [
33,
7,
163,
144,
145,
153,
154,
155,
133,
173,
157,
158,
159,
160,
161,
246,
],
"eye_right": [
362,
382,
381,
380,
374,
373,
390,
249,
263,
466,
388,
387,
386,
385,
384,
398,
],
"eyebrow_left": [70, 63, 105, 66, 107, 55, 65, 52, 53, 46],
"eyebrow_right": [300, 293, 334, 296, 336, 285, 295, 282, 283, 276],
"nose": [
1,
2,
4,
5,
6,
19,
94,
141,
168,
195,
197,
236,
240,
274,
275,
278,
279,
294,
326,
327,
360,
363,
370,
456,
460,
],
"lips": [
61,
146,
91,
181,
84,
17,
314,
405,
321,
375,
291,
308,
324,
318,
402,
317,
14,
87,
178,
88,
95,
78,
],
"iris_left": [468, 469, 470, 471, 472],
"iris_right": [473, 474, 475, 476, 477],
}
@dataclass(frozen=True)
class FaceLandmarks:
"""Extracted facial landmarks with metadata."""
landmarks: np.ndarray # (478, 3) normalized (x, y, z)
image_width: int
image_height: int
confidence: float
@property
def pixel_coords(self) -> np.ndarray:
"""Convert normalized landmarks to pixel coordinates (478, 2).
Coordinates are clamped to valid image bounds so that extreme
head poses do not produce out-of-range indices.
"""
coords = self.landmarks[:, :2].copy()
coords[:, 0] *= self.image_width
coords[:, 1] *= self.image_height
coords[:, 0] = np.clip(coords[:, 0], 0, self.image_width - 1)
coords[:, 1] = np.clip(coords[:, 1], 0, self.image_height - 1)
return coords
def pixel_coords_at(self, width: int, height: int) -> np.ndarray:
"""Convert normalized landmarks to pixel coordinates at a given size.
Use this when the image has been resized after landmark extraction.
Coordinates are clamped to [0, width-1] x [0, height-1].
"""
coords = self.landmarks[:, :2].copy()
coords[:, 0] *= width
coords[:, 1] *= height
coords[:, 0] = np.clip(coords[:, 0], 0, width - 1)
coords[:, 1] = np.clip(coords[:, 1], 0, height - 1)
return coords
def rescale(self, width: int, height: int) -> FaceLandmarks:
"""Return a copy with updated image dimensions.
Landmarks stay in normalized [0,1] space; only the stored
width/height change, so ``pixel_coords`` returns values at
the new resolution.
"""
return FaceLandmarks(
landmarks=self.landmarks.copy(),
image_width=width,
image_height=height,
confidence=self.confidence,
)
def get_region(self, region: str) -> np.ndarray:
"""Get landmark indices for a named region."""
indices = LANDMARK_REGIONS.get(region, [])
return self.landmarks[indices]
def extract_landmarks(
image: np.ndarray,
min_detection_confidence: float = 0.5,
min_tracking_confidence: float = 0.5,
) -> FaceLandmarks | None:
"""Extract 478 facial landmarks from an image using MediaPipe Face Mesh.
Args:
image: BGR image as numpy array.
min_detection_confidence: Minimum face detection confidence.
min_tracking_confidence: Minimum landmark tracking confidence.
Returns:
FaceLandmarks if a face is detected, None otherwise.
"""
h, w = image.shape[:2]
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Try new Tasks API first (mediapipe >= 0.10.20), fall back to legacy solutions API
try:
landmarks, confidence = _extract_tasks_api(rgb, min_detection_confidence)
except Exception:
logger.debug("Tasks API unavailable, trying Solutions API", exc_info=True)
try:
landmarks, confidence = _extract_solutions_api(
rgb, min_detection_confidence, min_tracking_confidence
)
except Exception:
logger.debug("Both MediaPipe APIs failed", exc_info=True)
return None
if landmarks is None:
return None
return FaceLandmarks(
landmarks=landmarks,
image_width=w,
image_height=h,
confidence=confidence,
)
def _extract_tasks_api(
rgb: np.ndarray,
min_confidence: float,
) -> tuple[np.ndarray | None, float]:
"""Extract landmarks using MediaPipe Tasks API (>= 0.10.20)."""
FaceLandmarker = mp.tasks.vision.FaceLandmarker
FaceLandmarkerOptions = mp.tasks.vision.FaceLandmarkerOptions
RunningMode = mp.tasks.vision.RunningMode
BaseOptions = mp.tasks.BaseOptions
import tempfile
import urllib.request
# Download model if not cached
model_path = Path(tempfile.gettempdir()) / "face_landmarker_v2_with_blendshapes.task"
if not model_path.exists():
url = "https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task"
urllib.request.urlretrieve(url, str(model_path))
options = FaceLandmarkerOptions(
base_options=BaseOptions(model_asset_path=str(model_path)),
running_mode=RunningMode.IMAGE,
num_faces=1,
min_face_detection_confidence=min_confidence,
output_face_blendshapes=False,
output_facial_transformation_matrixes=False,
)
with FaceLandmarker.create_from_options(options) as landmarker:
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb)
result = landmarker.detect(mp_image)
if not result.face_landmarks:
return None, 0.0
face_lms = result.face_landmarks[0]
landmarks = np.array(
[(lm.x, lm.y, lm.z) for lm in face_lms],
dtype=np.float32,
)
# MediaPipe Tasks API doesn't expose per-landmark detection confidence;
# return 1.0 to indicate successful detection
return landmarks, 1.0
def _extract_solutions_api(
rgb: np.ndarray,
min_detection_confidence: float,
min_tracking_confidence: float,
) -> tuple[np.ndarray | None, float]:
"""Extract landmarks using legacy MediaPipe Solutions API."""
with mp.solutions.face_mesh.FaceMesh(
static_image_mode=True,
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=min_detection_confidence,
min_tracking_confidence=min_tracking_confidence,
) as face_mesh:
results = face_mesh.process(rgb)
if not results.multi_face_landmarks:
return None, 0.0
face = results.multi_face_landmarks[0]
landmarks = np.array(
[(lm.x, lm.y, lm.z) for lm in face.landmark],
dtype=np.float32,
)
# Legacy API doesn't expose detection confidence; return 1.0 for success
return landmarks, 1.0
def visualize_landmarks(
image: np.ndarray,
face: FaceLandmarks,
radius: int = 1,
draw_regions: bool = True,
) -> np.ndarray:
"""Draw colored landmark dots on image by anatomical region.
Args:
image: BGR image to draw on (will be copied).
face: Extracted face landmarks.
radius: Dot radius in pixels.
draw_regions: If True, color by region. Otherwise all white.
Returns:
Annotated image copy.
"""
canvas = image.copy()
coords = face.pixel_coords
if draw_regions:
# Build index -> color mapping
idx_to_color: dict[int, tuple[int, int, int]] = {}
for region, indices in LANDMARK_REGIONS.items():
color = REGION_COLORS.get(region, (255, 255, 255))
for idx in indices:
idx_to_color[idx] = color
for i, (x, y) in enumerate(coords):
color = idx_to_color.get(i, (128, 128, 128))
cv2.circle(canvas, (int(x), int(y)), radius, color, -1)
else:
for x, y in coords:
cv2.circle(canvas, (int(x), int(y)), radius, (255, 255, 255), -1)
return canvas
def render_landmark_image(
face: FaceLandmarks,
width: int | None = None,
height: int | None = None,
radius: int = 2,
) -> np.ndarray:
"""Render MediaPipe face mesh tessellation on black canvas.
Draws the full 2556-edge tessellation mesh that CrucibleAI/ControlNetMediaPipeFace
was pre-trained on. This is critical -- the ControlNet expects dense triangulated
wireframes, not sparse dots.
Falls back to colored dots if tessellation connections aren't available.
Args:
face: Extracted face landmarks.
width: Canvas width (defaults to face.image_width).
height: Canvas height (defaults to face.image_height).
radius: Dot radius (used for key landmark dots overlay).
Returns:
BGR image with face mesh on black background.
"""
w = width or face.image_width
h = height or face.image_height
canvas = np.zeros((h, w, 3), dtype=np.uint8)
coords = face.landmarks[:, :2].copy()
coords[:, 0] *= w
coords[:, 1] *= h
pts = coords.astype(np.int32)
# Draw tessellation mesh (what CrucibleAI ControlNet expects)
try:
from mediapipe.tasks.python.vision.face_landmarker import FaceLandmarksConnections
tessellation = FaceLandmarksConnections.FACE_LANDMARKS_TESSELATION
contours = FaceLandmarksConnections.FACE_LANDMARKS_CONTOURS
# Draw tessellation edges (thin, gray-white)
for conn in tessellation:
p1 = tuple(pts[conn.start])
p2 = tuple(pts[conn.end])
cv2.line(canvas, p1, p2, (192, 192, 192), 1, cv2.LINE_AA)
# Draw contour edges on top (brighter, key features)
for conn in contours:
p1 = tuple(pts[conn.start])
p2 = tuple(pts[conn.end])
cv2.line(canvas, p1, p2, (255, 255, 255), 1, cv2.LINE_AA)
except (ImportError, AttributeError):
# Fallback: draw colored dots if tessellation not available
idx_to_color: dict[int, tuple[int, int, int]] = {}
for region, indices in LANDMARK_REGIONS.items():
color = REGION_COLORS.get(region, (128, 128, 128))
for idx in indices:
idx_to_color[idx] = color
for i, (x, y) in enumerate(coords):
color = idx_to_color.get(i, (128, 128, 128))
cv2.circle(canvas, (int(x), int(y)), radius, color, -1)
return canvas
def load_image(path: str | Path) -> np.ndarray:
"""Load an image from disk as BGR numpy array."""
img = cv2.imread(str(path))
if img is None:
raise FileNotFoundError(f"Could not load image: {path}")
return img
|