Spaces:
Running on Zero
Running on Zero
vitor.fran commited on
Commit ·
9dd50df
1
Parent(s): 5c3f5e7
fix: update faceless_processing with improved detection and face contour polygon erase
Browse files9 unmodified lines1011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768196970712272737426757677787980818283843 unmodified lines888990399192939 unmodified lines103104105106107108109110111112591131141151166 unmodified lines1231241257212674127128129777879130131132133134821351361371381391401411421431441451461471481491508415115286153154888990919215515615715815916016116216316416516616716816917097989917117217317417510110217617717810417918010610718118218318418518618718818919019119219319419519619719819911120020120216 unmodified lines2192202211342222232246 unmodified lines23123223314723423523623763 unmodified lines3013023033043053063073083093102213112233123133144 unmodified lines3193203213223233243253263273283293303313323333343353363373383392353403413423 unmodified lines3463473483493503513522463533543552493563573583593603613623633642512523653663673683693703712573723733747 unmodified lines38238338427138538638738829 unmodified lines4184194204214224234244254264274284294304314324334344353084364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904913103114924934943133144954964974981 unmodified line5005015023225035045055063273283293303313323335075085095105115125135143 unmodified lines51851952034352152252352452552634952752852910 unmodified lines5405415423665435443695455463723735473755485495503 unmodified lines5545555563855573875585593893903913923933943953965605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156164044054066176186196206216224104114124134144154164174184194204214224234244254264274284294304314324334344356236246256266276286296306316326336346356364374384394406376386394426406416426434446446456464476474494504514524534544554564574584594604614624636486496506516526536546556566576586596606616626636646656666674704714724734744754764776686694794806706716726736744856756766776 unmodified lines6846856864984996876886896906916926936946956965016976986997005057015075087027035117047057065157077085187097107117125237137147151 unmodified line71771871953153272053453553672172253872372472526 unmodified lines7527537545717557565747577587597607615807627637645 unmodified lines7707717725925935945955967737747757767777787797806037817827839 unmodified linesfrom rembg import remove, new_sessionimport threading
try: from scipy.interpolate import splprep, splev _SCIPY_AVAILABLE = Trueexcept ImportError: _SCIPY_AVAILABLE = False print("[AI] scipy not available — falling back to approxPolyDP for silhouette smoothing")
try: _SESSION_ISNET = new_session("isnet-general-use")except Exception as e: print(f"[AI] Failed to load rembg session: {e}") _SESSION_ISNET = None
# ---------------------------------------------------------------------------# Neural line-art detector (controlnet_aux / informative-drawings)# This is what produces the clean, continuous "illustrator" strokes that a# Canny edge detector can never achieve.# ---------------------------------------------------------------------------_LINEART_LOCK = threading.Lock()_LINEART_DETECTOR = None_LINEART_AVAILABLE = True
def get_lineart_detector(): """ Lazy-load the controlnet_aux LineartDetector (informative-drawings model from lllyasviel/Annotators). Loaded once and cached. Moves to GPU if torch + CUDA are available, otherwise runs on CPU. """ global _LINEART_DETECTOR, _LINEART_AVAILABLE if _LINEART_DETECTOR is not None or not _LINEART_AVAILABLE: return _LINEART_DETECTOR with _LINEART_LOCK: if _LINEART_DETECTOR is not None: return _LINEART_DETECTOR try: from controlnet_aux import LineartDetector detector = LineartDetector.from_pretrained("lllyasviel/Annotators") try: import torch if torch.cuda.is_available(): detector = detector.to("cuda") print("[AI] LineartDetector loaded on CUDA") else: print("[AI] LineartDetector loaded on CPU") except Exception: print("[AI] LineartDetector loaded (torch device check skipped)") _LINEART_DETECTOR = detector except Exception as e: print(f"[AI] controlnet_aux LineartDetector unavailable, falling back to Canny: {e}") _LINEART_AVAILABLE = False _LINEART_DETECTOR = None return _LINEART_DETECTOR
# Globally download cascades and models at startup if missing (runs on main container with internet access)# Globally download cascades and models at startup if missingdef _download_assets_globally(): try: # 1. Cascades cascade_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "cascades") os.makedirs(cascade_dir, exist_ok=True) xml_urls = { 'haarcascade_frontalface_default.xml': 'https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml', 'haarcascade_frontalface_default.xml': 'https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcape_frontalface_default.xml', 'haarcascade_frontalface_alt2.xml': 'https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_alt2.xml', 'haarcascade_profileface.xml': 'https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_profileface.xml' } # Note: Fix frontalface_default URL typo if raw OpenCV has it xml_urls['haarcascade_frontalface_default.xml'] = 'https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml' for name, url in xml_urls.items(): path = os.path.join(cascade_dir, name) if not os.path.exists(path) or os.path.getsize(path) < 1000:3 unmodified lines except Exception as dl_err: print(f"[AI] Failed to download {name} globally: {dl_err}")
# 2. MediaPipe Model model_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "models") os.makedirs(model_dir, exist_ok=True) model_path = os.path.join(model_dir, "face_landmarker.task")9 unmodified lines print(f"[AI] Global asset download manager failed: {e}")
_download_assets_globally()# Warm up the neural detector at import time (download weights once)get_lineart_detector()
@dataclassclass LineartConfig: # Backward compatibility fields (unused by OpenCV pipeline but required by callers) # Backward compatibility fields prompt: str = "" negative_prompt: str = "" stable_diffusion_model_id: str = ""6 unmodified lines transparency_threshold: float = 0.03 detect_eyebrows: bool = True
# Subject mask white_bg_threshold: int = 245 # pixels above this = background (raised to avoid erasing light subjects) white_bg_threshold: int = 245 mask_close_iterations: int = 4 mask_open_iterations: int = 1 mask_smooth_ksize: int = 5 # GaussianBlur kernel before silhouette (must be odd; 0 = disabled) mask_smooth_threshold: int = 128 # re-binarize after blur min_silhouette_area: float = 200.0 # drop tiny isolated blobs from the silhouette (px²) mask_smooth_ksize: int = 5 mask_smooth_threshold: int = 128 min_silhouette_area: float = 200.0
# Silhouette outline silhouette_line_thickness: int = 4 # outer contour stroke width (px); match interior for uniform look silhouette_line_thickness: int = 4
# --- Neural line-art parameters --- lineart_detect_resolution: int = 1024 lineart_image_resolution: int = 1024 lineart_coarse: bool = True # AUMENTADO: Força bruta para ignorar detalhes fracos lineart_threshold: int = 120 # AUMENTADO: Faca implacável para traços curtos lineart_min_perimeter: float = 90.0 interior_erode_size: int = 1 interior_line_thickness: int = 3
# Edge detection # ---- Canny fallback parametros ---- canny_low: int = 35 canny_high: int = 85 canny_high: int = 75 bilateral_d: int = 11 bilateral_sigma_color: int = 80 bilateral_sigma_space: int = 80 gaussian_ksize: int = 9 min_contour_perimeter: float = 25.0 # filters out short noisy lines interior_line_thickness: int = 3 # thickness of interior detail lines (>=1) bilateral_sigma_color: int = 110 bilateral_sigma_space: int = 110 gaussian_ksize: int = 11 min_contour_perimeter: float = 15.0 min_embroidery_stitch_px: int = 25 enable_detail_pass: bool = False detail_canny_low: int = 15 detail_canny_high: int = 55 detail_gaussian_ksize: int = 5 detail_min_perimeter: float = 30.0 detail_bilateral_d: int = 7 detail_bilateral_sigma: int = 60
# Face detection face_scale_factor: float = 1.05 face_min_neighbors: int = 3 face_min_size: Tuple[int, int] = (25, 25) face_max_y_ratio: float = 0.6 # ignore faces below 60% image height face_nms_iou_thresh: float = 0.4 face_min_size: Tuple[int, int] = (20, 20) face_max_y_ratio: float = 0.65 face_nms_iou_thresh: float = 0.35 face_max_count: int = 6
# Face erase ellipse (ratios relative to face bbox) — fallback parameters face_cx_ratio: float = 0.5 # Face erase ellipse face_cx_ratio: float = 0.50 face_cy_ratio: float = 0.50 face_rx_ratio: float = 0.44 # adjusted for better fallback erasure face_rx_ratio: float = 0.44 face_ry_ratio: float = 0.46 face_erase_blur: int = 8 face_erase_threshold: int = 130 # higher = sharper face mask edge, less spill face_erase_blur: int = 5 face_erase_threshold: int = 90
# MediaPipe mediapipe_num_faces: int = 6
# --- AJUSTE DE SOBRANCELHA --- eyebrow_thickness: int = 2 eyebrow_dilate: int = 0
# Face shape draw_face_oval: bool = True face_oval_thickness: int = 3 # AJUSTE FINO: Remove 45% do topo do oval (o que causava aquele erro bizarro) face_oval_skip_top_ratio: float = 0.45
def _nms_faces(faces: list, iou_thresh: float) -> list: """Non-maximum suppression to remove duplicate face detections.""" if not faces: return [] boxes = np.array(faces, dtype=float)16 unmodified lines
def detect_faces(rgb: np.ndarray, cfg: LineartConfig) -> list: """Detect faces and filter out false positives in lower body.""" h, w = rgb.shape[:2] faces = []
6 unmodified lines model_path = os.path.join(model_dir, "face_landmarker.task") if os.path.exists(model_path): num_faces = 6 num_faces = getattr(cfg, 'mediapipe_num_faces', 6) with _LANDMARKER_LOCK: landmarker = get_landmarker(model_path, num_faces=num_faces) mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb)63 unmodified lines # 3. Filter and Deduplicate faces = [f for f in faces if f[1] < h * cfg.face_max_y_ratio] faces = _nms_faces(faces, cfg.face_nms_iou_thresh) max_faces = getattr(cfg, 'face_max_count', 6) if len(faces) > max_faces: faces = sorted(faces, key=lambda f: f[2] * f[3], reverse=True)[:max_faces] return faces
def build_subject_mask(rgb: np.ndarray, cfg: LineartConfig, alpha_channel: Optional[np.ndarray] = None) -> np.ndarray: """Build subject mask. Uses alpha channel if available, otherwise thresholds white background.""" if alpha_channel is not None: # Use rembg/RGBA alpha directly — much more accurate than color threshold mask = (alpha_channel > 30).astype(np.uint8) * 255 else: white_mask = np.all(rgb > cfg.white_bg_threshold, axis=2)4 unmodified lines return mask
def _smooth_contour_spline(cnt: np.ndarray, n_points: int = 400) -> Optional[np.ndarray]: if not _SCIPY_AVAILABLE: return None pts = cnt[:, 0, :] if len(pts) < 4: return None try: tck, _ = splprep([pts[:, 0].astype(float), pts[:, 1].astype(float)], s=len(pts) * 0.5, per=True, quiet=True) u_new = np.linspace(0, 1, n_points) x_new, y_new = splev(u_new, tck) spline_pts = np.stack([x_new, y_new], axis=1).astype(np.int32) return spline_pts.reshape((-1, 1, 2)) except Exception: return None
def build_silhouette(subject_mask: np.ndarray, cfg: LineartConfig) -> np.ndarray: """Draw the subject silhouette as a smooth, solid-stroke contour.""" mask = subject_mask.copy() h, w = mask.shape[:2] # Dynamic proportional smoothing based on image size to eliminate jaggies completely3 unmodified lines k = max(25, k) mask = cv2.GaussianBlur(mask, (k, k), 0) _, mask = cv2.threshold(mask, cfg.mask_smooth_threshold, 255, cv2.THRESH_BINARY) k_close = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 9)) mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, k_close, iterations=2)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_KCOS) contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) silhouette = np.zeros_like(subject_mask) for cnt in contours: if cv2.contourArea(cnt) >= cfg.min_silhouette_area: if cv2.contourArea(cnt) < cfg.min_silhouette_area: continue smooth = _smooth_contour_spline(cnt) if smooth is not None: cv2.polylines(silhouette, [smooth], isClosed=True, color=255, thickness=cfg.silhouette_line_thickness, lineType=cv2.LINE_AA) else: eps = 0.0012 * cv2.arcLength(cnt, True) smooth = cv2.approxPolyDP(cnt, eps, True) cv2.drawContours(silhouette, [smooth], -1, 255, cfg.silhouette_line_thickness, lineType=cv2.LINE_AA) approx = cv2.approxPolyDP(cnt, eps, True) cv2.drawContours(silhouette, [approx], -1, 255, cfg.silhouette_line_thickness, lineType=cv2.LINE_AA) return silhouette
def build_face_erase_mask(rgb: np.ndarray, faces: list, cfg: LineartConfig) -> np.ndarray: """Build a soft binary mask covering face interior regions using MediaPipe polygon or fallback ellipse.""" h, w = rgb.shape[:2] mask = np.zeros((h, w), dtype=np.uint8) 7 unmodified lines model_path = os.path.join(model_dir, "face_landmarker.task") if os.path.exists(model_path): num_faces = 6 num_faces = getattr(cfg, 'mediapipe_num_faces', 6) with _LANDMARKER_LOCK: landmarker = get_landmarker(model_path, num_faces=num_faces) mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb)29 unmodified lines return (soft > cfg.face_erase_threshold).astype(np.uint8) * 255
def _line_strength_from_detector(pil_lineart: Image.Image, target_hw: Tuple[int, int]) -> np.ndarray: h, w = target_hw gray = np.array(pil_lineart.convert("L")) corners = [gray[0, 0], gray[0, -1], gray[-1, 0], gray[-1, -1]] bg = float(np.median(corners)) if bg > 127: strength = 255 - gray else: strength = gray if strength.shape[:2] != (h, w): strength = cv2.resize(strength, (w, h), interpolation=cv2.INTER_LINEAR) return strength.astype(np.uint8)
def build_interior_edges(rgb: np.ndarray, subject_mask: np.ndarray, cfg: LineartConfig) -> np.ndarray: """Detect structural body lines using Canny, filtering noise.""" h, w = rgb.shape[:2] detector = get_lineart_detector()
if detector is not None: try: pil_in = Image.fromarray(rgb) with _LINEART_LOCK: pil_out = detector( pil_in, detect_resolution=cfg.lineart_detect_resolution, image_resolution=cfg.lineart_image_resolution, coarse=cfg.lineart_coarse, ) strength = _line_strength_from_detector(pil_out, (h, w))
_, edges = cv2.threshold(strength, cfg.lineart_threshold, 255, cv2.THRESH_BINARY)
erode_size = getattr(cfg, 'interior_erode_size', 3) erode_size = max(1, erode_size) if erode_size % 2 == 0: erode_size += 1 kernel_erode = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (erode_size, erode_size)) eroded_mask = cv2.erode(subject_mask, kernel_erode, iterations=1) edges = cv2.bitwise_and(edges, eroded_mask)
# Aspirador de Sujeiras kernel_clean = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) edges = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel_clean, iterations=1)
contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) filtered = np.zeros_like(edges) for cnt in contours: perimeter = cv2.arcLength(cnt, False) area = cv2.contourArea(cnt) # BARREIRA EXTREMA: Elimina pontos isolados e traços curtos if perimeter < cfg.lineart_min_perimeter or area < 40.0: continue cv2.drawContours(filtered, [cnt], -1, 255, cfg.interior_line_thickness)
kernel_dilate = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) filtered = cv2.dilate(filtered, kernel_dilate, iterations=1) filtered = cv2.GaussianBlur(filtered, (3, 3), 0) _, filtered = cv2.threshold(filtered, 127, 255, cv2.THRESH_BINARY) return filtered except Exception as e: print(f"[AI] Neural lineart failed, falling back to Canny: {e}")
return _build_interior_edges_canny(rgb, subject_mask, cfg)
def _build_interior_edges_canny(rgb: np.ndarray, subject_mask: np.ndarray, cfg: LineartConfig) -> np.ndarray: gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY) blurred = cv2.GaussianBlur(gray, (cfg.gaussian_ksize, cfg.gaussian_ksize), 0) blurred = cv2.bilateralFilter(blurred, cfg.bilateral_d, cfg.bilateral_sigma_color, cfg.bilateral_sigma_space) blurred = cv2.bilateralFilter(gray, cfg.bilateral_d, cfg.bilateral_sigma_color, cfg.bilateral_sigma_space) blurred = cv2.GaussianBlur(blurred, (cfg.gaussian_ksize, cfg.gaussian_ksize), 0) edges = cv2.Canny(blurred, cfg.canny_low, cfg.canny_high) # Erode the subject mask so Canny doesn't catch the outer boundary twice
erode_size = max(5, cfg.silhouette_line_thickness * 2 + 3) if erode_size % 2 == 0: erode_size += 11 unmodified line eroded_mask = cv2.erode(subject_mask, kernel_erode, iterations=1) edges = cv2.bitwise_and(edges, eroded_mask)
# Filter out short/noisy contours contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) filtered = np.zeros_like(edges) for cnt in contours: perimeter = cv2.arcLength(cnt, False) if perimeter > cfg.min_contour_perimeter: # Simplify interior edges slightly to remove Canny wobbles while keeping natural flow eps = 0.0012 * perimeter smooth = cv2.approxPolyDP(cnt, eps, False) cv2.drawContours(filtered, [smooth], -1, 255, cfg.interior_line_thickness)
# Mild dilation for uniform, solid strokes if perimeter < cfg.min_contour_perimeter: continue eps = 0.0012 * perimeter smooth = cv2.approxPolyDP(cnt, eps, False) cv2.drawContours(filtered, [smooth], -1, 255, cfg.interior_line_thickness) kernel_dilate = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) filtered = cv2.dilate(filtered, kernel_dilate, iterations=1) return filtered3 unmodified lines_LANDMARKER_CACHE = None_LANDMARKER_NUM_FACES = None
def get_landmarker(model_path, num_faces: int = 1):def get_landmarker(model_path, num_faces: int = 6): global _LANDMARKER_CACHE, _LANDMARKER_NUM_FACES if _LANDMARKER_CACHE is None or _LANDMARKER_NUM_FACES != num_faces: import mediapipe as mp from mediapipe.tasks import python from mediapipe.tasks.python import vision base_options = python.BaseOptions(model_asset_path=model_path) options = vision.FaceLandmarkerOptions( base_options=base_options,10 unmodified lines
def draw_eyebrows(rgb: np.ndarray, cfg: LineartConfig, faces: Optional[list] = None) -> np.ndarray: """Detect and draw eyebrows landmarks using MediaPipe Task API with hybrid global + crop detection.""" h, w, _ = rgb.shape eyebrows_layer = np.zeros((h, w), dtype=np.uint8)
try: import mediapipe as mp from mediapipe.tasks import python from mediapipe.tasks.python import vision
# Determine model path model_path = cfg.mediapipe_model_path if not model_path or not os.path.exists(model_path): model_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "models")3 unmodified lines print("[AI] Downloading face_landmarker.task for eyebrows...") model_url = "https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task" urllib.request.urlretrieve(model_url, model_path) print("[AI] Download complete!")
LEFT_EYEBROW = [70, 63, 105, 66, 107, 55, 65, 52, 53, 46] LEFT_EYEBROW = [70, 63, 105, 66, 107, 55, 65, 52, 53, 46] RIGHT_EYEBROW = [336, 296, 334, 293, 300, 285, 295, 282, 283, 276] eyebrow_thickness = max(1, cfg.interior_line_thickness - 1) processed_centers = []
def is_duplicate(cx, cy): for (pcx, pcy) in processed_centers: dist = np.sqrt((cx - pcx)**2 + (cy - pcy)**2) if dist < 0.08: FACE_OVAL = [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]
eyebrow_thickness = getattr(cfg, 'eyebrow_thickness', cfg.interior_line_thickness) draw_oval = True oval_thickness = getattr(cfg, 'face_oval_thickness', 2) oval_skip_top = getattr(cfg, 'face_oval_skip_top_ratio', 0.18) drawn_centers_px: list = []
def is_duplicate_px(cx_px: float, cy_px: float) -> bool: min_dist = min(w, h) * 0.06 for (pcx, pcy) in drawn_centers_px: if np.hypot(cx_px - pcx, cy_px - pcy) < min_dist: return True return False
def draw_eyebrow_landmarks(face_lms, origin_x, origin_y, scale_x, scale_y): for indices in [LEFT_EYEBROW, RIGHT_EYEBROW]: pts = [] for idx in indices: lm = face_lms[idx] px = int(origin_x + lm.x * scale_x) py = int(origin_y + lm.y * scale_y) pts.append([px, py]) pts_np = np.array(pts, np.int32).reshape((-1, 1, 2)) cv2.polylines(eyebrows_layer, [pts_np], isClosed=False, color=255, thickness=eyebrow_thickness, lineType=cv2.LINE_AA)
if draw_oval: oval_pts = [] for idx in FACE_OVAL: lm = face_lms[idx] px = int(origin_x + lm.x * scale_x) py = int(origin_y + lm.y * scale_y) oval_pts.append([px, py]) oval_np = np.array(oval_pts, np.int32) if len(oval_np) >= 4: if oval_skip_top > 0: y_min = oval_np[:, 1].min() y_max = oval_np[:, 1].max() cutoff = y_min + (y_max - y_min) * oval_skip_top kept = oval_np[oval_np[:, 1] >= cutoff] if len(kept) >= 4: arc = kept.reshape((-1, 1, 2)) cv2.polylines(eyebrows_layer, [arc], isClosed=False, color=255, thickness=oval_thickness, lineType=cv2.LINE_AA) else: cv2.polylines(eyebrows_layer, [oval_np.reshape((-1, 1, 2))], isClosed=True, color=255, thickness=oval_thickness, lineType=cv2.LINE_AA)
if faces is None: faces = detect_faces(rgb, cfg)
with _LANDMARKER_LOCK: landmarker = get_landmarker(model_path) # Phase 1: Global detection (upright / clear faces) num_faces = getattr(cfg, 'mediapipe_num_faces', 6) landmarker = get_landmarker(model_path, num_faces=num_faces)
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb) global_result = landmarker.detect(mp_image) if global_result.face_landmarks: for face_landmarks in global_result.face_landmarks: xs = [lm.x for lm in face_landmarks] ys = [lm.y for lm in face_landmarks] cx, cy = np.mean(xs), np.mean(ys) if not is_duplicate(cx, cy): processed_centers.append((cx, cy)) for indices in [LEFT_EYEBROW, RIGHT_EYEBROW]: points = [] for idx in indices: lm = face_landmarks[idx] points.append([int(lm.x * w), int(lm.y * h)]) pts = np.array(points, np.int32).reshape((-1, 1, 2)) cv2.polylines(eyebrows_layer, [pts], isClosed=False, color=255, thickness=eyebrow_thickness)
# Phase 2: OpenCV crops with 45% margin (for tilted / overlapping faces) for i, (fx, fy, fw, fh) in enumerate(faces): margin_x = int(fw * 0.45) margin_y = int(fh * 0.45) x1 = max(0, fx - margin_x) y1 = max(0, fy - margin_y) x2 = min(w, fx + fw + margin_x) y2 = min(h, fy + fh + margin_y)
crop_w = x2 - x1 crop_h = y2 - y1 if crop_w <= 0 or crop_h <= 0: for face_lms in global_result.face_landmarks: cx_px = np.mean([lm.x for lm in face_lms]) * w cy_px = np.mean([lm.y for lm in face_lms]) * h if not is_duplicate_px(cx_px, cy_px): drawn_centers_px.append((cx_px, cy_px)) draw_eyebrow_landmarks(face_lms, 0, 0, w, h)
for (fx, fy, fw, fh) in faces: mx = int(fw * 0.50); my = int(fh * 0.50) x1 = max(0, fx - mx); y1 = max(0, fy - my) x2 = min(w, fx + fw + mx); y2 = min(h, fy + fh + my) crop_w, crop_h = x2 - x1, y2 - y1 if crop_w < 20 or crop_h < 20: continue
crop_cx = (x1 + crop_w / 2) / w crop_cy = (y1 + crop_h / 2) / h if is_duplicate(crop_cx, crop_cy): face_cx_px = (x1 + x2) / 2; face_cy_px = (y1 + y2) / 2 if is_duplicate_px(face_cx_px, face_cy_px): continue
crop = rgb[y1:y2, x1:x2] target = max(512, crop_w, crop_h) scale = target / max(crop_w, crop_h) cw_scaled = int(crop_w * scale); ch_scaled = int(crop_h * scale) crop_resized = cv2.resize(crop, (512, 512), interpolation=cv2.INTER_CUBIC) crop_resized = cv2.resize(crop, (cw_scaled, ch_scaled), interpolation=cv2.INTER_CUBIC) crop_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=crop_resized) crop_result = landmarker.detect(crop_image)
if crop_result.face_landmarks: face_landmarks = crop_result.face_landmarks[0] xs_global = [(x1 + lm.x * crop_w) / w for lm in face_landmarks] ys_global = [(y1 + lm.y * crop_h) / h for lm in face_landmarks] cx, cy = np.mean(xs_global), np.mean(ys_global) if not is_duplicate(cx, cy): processed_centers.append((cx, cy)) for indices in [LEFT_EYEBROW, RIGHT_EYEBROW]: points = [] for idx in indices: lm = face_landmarks[idx] gx = int(x1 + lm.x * crop_w) gy = int(y1 + lm.y * crop_h) points.append([gx, gy]) pts = np.array(points, np.int32).reshape((-1, 1, 2)) cv2.polylines(eyebrows_layer, [pts], isClosed=False, color=255, thickness=eyebrow_thickness) face_lms = crop_result.face_landmarks[0] cx_px2 = x1 + np.mean([lm.x for lm in face_lms]) * crop_w cy_px2 = y1 + np.mean([lm.y for lm in face_lms]) * crop_h if is_duplicate_px(cx_px2, cy_px2): continue drawn_centers_px.append((cx_px2, cy_px2)) draw_eyebrow_landmarks(face_lms, x1, y1, crop_w, crop_h)
print(f"[eyebrows] Drew eyebrows for {len(drawn_centers_px)} face(s)") except Exception as e: print(f"[AI] Eyebrow extraction error or library missing: {e}")
if eyebrows_layer.any(): dil = getattr(cfg, 'eyebrow_dilate', 1) if dil > 0: kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) eyebrows_layer = cv2.dilate(eyebrows_layer, kernel, iterations=dil) return eyebrows_layer
def make_faceless_lineart( img: Image.Image, cfg: LineartConfig = None) -> Image.Image: """ Generate faceless lineart transparent image from a photo. """def make_faceless_lineart(img: Image.Image, cfg: LineartConfig = None) -> Image.Image: cfg = cfg or LineartConfig() # Preserve alpha channel if image already has a transparent background (e.g. from rembg)
alpha_channel: Optional[np.ndarray] = None if img.mode in ("RGBA", "LA") or (img.mode == "P" and "transparency" in img.info): rgba_img = img.convert("RGBA") alpha_np = np.array(rgba_img)[:, :, 3] # Only use alpha mask if it contains meaningful transparency if np.any(alpha_np < 200): alpha_channel = alpha_np background = Image.new("RGBA", img.size, (255, 255, 255, 255))6 unmodified lines rgb = img_np[:, :, :3] h, w = rgb.shape[:2]
# 1. Subject mask — use alpha if available, else color threshold subject_mask = build_subject_mask(rgb, cfg, alpha_channel) if alpha_channel is None and _SESSION_ISNET is not None: try: cut = remove(Image.fromarray(rgb).convert("RGBA"), session=_SESSION_ISNET, only_mask=True) cut_l = cut.convert("L") if isinstance(cut, Image.Image) else Image.open(io.BytesIO(cut)).convert("L") if cut_l.size != (w, h): cut_l = cut_l.resize((w, h), Image.LANCZOS) alpha_channel = np.array(cut_l) except Exception as e: print(f"[AI] rembg mask for lineart failed: {e}")
# 2. Detect faces subject_mask = build_subject_mask(rgb, cfg, alpha_channel) faces = detect_faces(rgb, cfg) print(f"[lineart] Detected {len(faces)} face(s)")
# 3. Silhouette outline silhouette = build_silhouette(subject_mask, cfg)
# 4. Interior structural edges interior = build_interior_edges(rgb, subject_mask, cfg)
# 5. Erase face details (keep silhouette outline of head shape) face_erase = build_face_erase_mask(rgb, faces, cfg) interior_clean = cv2.bitwise_and(interior, cv2.bitwise_not(face_erase))
# 6. Combine final_lines = cv2.add(silhouette, interior_clean)
# 6.5. Optional eyebrow detection overlay if cfg.detect_eyebrows: eyebrows = draw_eyebrows(rgb, cfg, faces=faces) final_lines = cv2.add(final_lines, eyebrows)
# Avoid drawing rectangular boxes/borders at the image edges border_erase_width = max(cfg.silhouette_line_thickness, cfg.interior_line_thickness) + 2 if border_erase_width > 0: final_lines[0:border_erase_width, :] = 01 unmodified line final_lines[:, 0:border_erase_width] = 0 final_lines[:, -border_erase_width:] = 0
# 7. Build output image alpha = Image.fromarray(final_lines) size = img.size
transparent = Image.new("RGBA", size, (0, 0, 0, 0)) transparent = Image.new("RGBA", img.size, (0, 0, 0, 0)) transparent.putalpha(alpha)
return transparent
26 unmodified lines rgba = img.convert("RGBA") if _has_real_transparency(rgba): return rgba
if _SESSION_ISNET is None: return rgba
mask = remove(rgba, session=_SESSION_ISNET, only_mask=True) alpha = mask.convert("L") if isinstance(mask, Image.Image) else Image.open(io.BytesIO(mask)).convert("L") if alpha.size != rgba.size: alpha = alpha.resize(rgba.size, Image.LANCZOS) alpha = alpha.filter(ImageFilter.GaussianBlur(radius=0.8))
r, g, b, _a = rgba.split() return Image.merge("RGBA", (r, g, b, alpha)) except Exception as exc:5 unmodified lines return remove_background_subject(img)
def build_faceless_embroidery_assets( img: Image.Image, cfg: Optional[LineartConfig] = None, **kwargs) -> Tuple[Image.Image, Image.Image]:
def build_faceless_embroidery_assets(img: Image.Image, cfg: Optional[LineartConfig] = None, **kwargs) -> Tuple[Image.Image, Image.Image]: if cfg is None: cfg = LineartConfig() if "prompt" in kwargs and kwargs["prompt"] is not None: cfg.prompt = kwargs["prompt"] if "negative_prompt" in kwargs and kwargs["negative_prompt"] is not None: cfg.negative_prompt = kwargs["negative_prompt"]
lineart = make_faceless_lineart(img, cfg) cutout = make_faceless_cutout(img) return lineart, cutoutfaceless_processing.py+332-155