Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,19 +18,18 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 18 |
import io
|
| 19 |
import asyncio
|
| 20 |
from concurrent.futures import ThreadPoolExecutor
|
| 21 |
-
import logging
|
| 22 |
|
| 23 |
logging.basicConfig(level=logging.INFO)
|
| 24 |
-
logger = logging.getLogger(__name__)
|
| 25 |
|
| 26 |
# ================================================
|
| 27 |
# 2. CONFIG
|
| 28 |
# ================================================
|
| 29 |
BEARD_MODEL_PATH = "models/best_hair_117_epoch_v4.pt"
|
| 30 |
-
SAFE_IMG_SIZE = 768
|
| 31 |
-
|
| 32 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 33 |
-
USE_FP16 = DEVICE.type == "cuda" and torch.cuda.is_available()
|
| 34 |
|
| 35 |
logger.info(f"🚀 Device: {DEVICE}, FP16: {USE_FP16}")
|
| 36 |
|
|
@@ -41,7 +40,7 @@ executor = ThreadPoolExecutor(max_workers=2)
|
|
| 41 |
|
| 42 |
face_processor = None
|
| 43 |
face_parser = None
|
| 44 |
-
beard_model = None
|
| 45 |
|
| 46 |
# ================================================
|
| 47 |
# 3. LOAD MODELS
|
|
@@ -50,6 +49,7 @@ def load_face_parser():
|
|
| 50 |
global face_processor, face_parser
|
| 51 |
if face_parser is not None:
|
| 52 |
return face_processor, face_parser
|
|
|
|
| 53 |
logger.info("Loading Segformer face-parsing...")
|
| 54 |
face_processor = SegformerImageProcessor.from_pretrained("jonathandinu/face-parsing")
|
| 55 |
face_parser = SegformerForSemanticSegmentation.from_pretrained("jonathandinu/face-parsing")
|
|
@@ -58,7 +58,7 @@ def load_face_parser():
|
|
| 58 |
if USE_FP16:
|
| 59 |
face_parser = face_parser.half()
|
| 60 |
logger.info("✅ Face parser loaded!")
|
| 61 |
-
return face_processor, face_parser
|
| 62 |
|
| 63 |
def load_beard_model():
|
| 64 |
global beard_model
|
|
@@ -67,20 +67,18 @@ def load_beard_model():
|
|
| 67 |
raise FileNotFoundError(f"❌ Beard model not found: {BEARD_MODEL_PATH}")
|
| 68 |
logger.info("Loading Beard YOLO model...")
|
| 69 |
beard_model = YOLO(BEARD_MODEL_PATH)
|
| 70 |
-
return beard_model
|
| 71 |
|
| 72 |
# ================================================
|
| 73 |
-
# 4. MASK FUNCTIONS
|
| 74 |
# ================================================
|
| 75 |
-
|
| 76 |
def get_beard_mask_fast(pil_image: Image.Image) -> np.ndarray:
|
| 77 |
temp = f"temp_{uuid.uuid4().hex[:8]}.jpg"
|
| 78 |
try:
|
| 79 |
img_small = pil_image.resize((256, 256), Image.LANCZOS)
|
| 80 |
img_small.save(temp)
|
| 81 |
model = load_beard_model()
|
| 82 |
-
res = model(temp, device=DEVICE.type, conf=0.3, iou=0.5,
|
| 83 |
-
verbose=False, half=USE_FP16, imgsz=256)
|
| 84 |
|
| 85 |
h, w = np.array(pil_image).shape[:2]
|
| 86 |
mask = np.zeros((h, w), dtype=np.float32)
|
|
@@ -104,6 +102,7 @@ def get_beard_mask_fast(pil_image: Image.Image) -> np.ndarray:
|
|
| 104 |
os.remove(temp)
|
| 105 |
|
| 106 |
def get_hair_mask_fast(pil_image: Image.Image) -> np.ndarray:
|
|
|
|
| 107 |
processor, parser = load_face_parser()
|
| 108 |
img_small = pil_image.resize((256, 256), Image.LANCZOS)
|
| 109 |
inputs = processor(images=img_small, return_tensors="pt").to(DEVICE)
|
|
@@ -113,22 +112,36 @@ def get_hair_mask_fast(pil_image: Image.Image) -> np.ndarray:
|
|
| 113 |
with torch.no_grad():
|
| 114 |
out = parser(**inputs)
|
| 115 |
logits = out.logits
|
| 116 |
-
up = torch.nn.functional.interpolate(logits, size=img_small.size[::-1],
|
| 117 |
-
mode="bilinear", align_corners=False)
|
| 118 |
probs = torch.softmax(up.float() if USE_FP16 else up, dim=1)[0]
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
face_cls = list(range(1, 6)) + list(range(8, 13)) + [17, 18]
|
| 122 |
parsing = up.argmax(dim=1).squeeze(0).cpu().numpy()
|
| 123 |
face_m = np.isin(parsing, face_cls).astype(np.float32)
|
| 124 |
-
face_m = cv2.dilate(face_m, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)), iterations=1)
|
| 125 |
-
hair = hair * (1 - face_m)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
| 128 |
hair = cv2.morphologyEx(hair, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 129 |
-
hair = cv2.morphologyEx(hair, cv2.MORPH_CLOSE, kernel, iterations=
|
| 130 |
-
|
| 131 |
-
|
|
|
|
| 132 |
oh, ow = np.array(pil_image).shape[:2]
|
| 133 |
return np.clip(cv2.resize(hair, (ow, oh)), 0, 1)
|
| 134 |
|
|
@@ -138,59 +151,49 @@ def get_masks_sequential(image):
|
|
| 138 |
# ================================================
|
| 139 |
# 5. 🔥 STRONG GREY HAIR - INCREASED INTENSITY
|
| 140 |
# ================================================
|
| 141 |
-
|
| 142 |
def apply_strong_grey_hair(image: Image.Image, hair_mask: np.ndarray, beard_mask: np.ndarray) -> Image.Image:
|
| 143 |
-
"""
|
| 144 |
-
Strong grey effect - very visible but natural
|
| 145 |
-
"""
|
| 146 |
comb = np.maximum(hair_mask, beard_mask)
|
| 147 |
-
|
| 148 |
if np.sum(comb) < 100:
|
| 149 |
logger.warning("⚠️ Small mask area")
|
| 150 |
-
|
| 151 |
# Soften edges
|
| 152 |
comb = cv2.GaussianBlur(comb, (7, 7), 2)
|
| 153 |
-
|
| 154 |
img = np.array(image).astype(np.float32)
|
| 155 |
-
|
| 156 |
# ========================================
|
| 157 |
# STRONG GREY CONVERSION
|
| 158 |
# ========================================
|
| 159 |
hsv = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_RGB2HSV).astype(np.float32)
|
| 160 |
-
|
| 161 |
# 1. COMPLETE desaturation (100%)
|
| 162 |
-
hsv[:,:,1] = hsv[:,:,1] * (1 - 1.0 * comb)
|
| 163 |
-
|
| 164 |
# 2. Increase brightness to grey level (STRONG boost)
|
| 165 |
-
# Old: +80, New: +110 for much lighter grey
|
| 166 |
hsv[:,:,2] = hsv[:,:,2] + (110 * comb)
|
| 167 |
-
hsv[:,:,2] = np.clip(hsv[:,:,2], 120, 230)
|
| 168 |
-
|
| 169 |
# 3. NO hue change - prevents blue/green
|
| 170 |
-
|
| 171 |
-
# Convert back
|
| 172 |
grey_result = cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2RGB).astype(np.float32)
|
| 173 |
-
|
| 174 |
# ========================================
|
| 175 |
# Blend - 85% new, 15% original (stronger)
|
| 176 |
# ========================================
|
| 177 |
-
blend = 0.85 * comb
|
| 178 |
mask_3ch = np.stack([blend, blend, blend], axis=2)
|
| 179 |
-
|
| 180 |
final = grey_result * mask_3ch + img * (1 - mask_3ch)
|
| 181 |
-
|
| 182 |
# ========================================
|
| 183 |
# Very slight warm tint (barely noticeable)
|
| 184 |
# ========================================
|
| 185 |
warm = np.array([5, 3, 0], dtype=np.float32)
|
| 186 |
final = final + (warm * comb[..., None] * 0.2)
|
| 187 |
-
|
| 188 |
final = np.clip(final, 0, 255).astype(np.uint8)
|
| 189 |
-
|
| 190 |
# Optional: Slight sharpening for definition
|
| 191 |
result = Image.fromarray(final)
|
| 192 |
result = result.filter(ImageFilter.UnsharpMask(radius=0.5, percent=50, threshold=0))
|
| 193 |
-
|
| 194 |
return result
|
| 195 |
|
| 196 |
def process_face_whitening(input_image: Image.Image) -> Image.Image:
|
|
@@ -199,24 +202,24 @@ def process_face_whitening(input_image: Image.Image) -> Image.Image:
|
|
| 199 |
logger.info(f"→ Processing image: {input_image.size}")
|
| 200 |
orig = input_image.convert("RGB")
|
| 201 |
ow, oh = orig.size
|
| 202 |
-
|
| 203 |
target_size = min(SAFE_IMG_SIZE, max(ow, oh))
|
| 204 |
if target_size % 2 == 1:
|
| 205 |
target_size -= 1
|
|
|
|
| 206 |
img_resized = orig.resize((target_size, target_size), Image.LANCZOS)
|
| 207 |
|
| 208 |
logger.info(" Generating hair & beard masks...")
|
| 209 |
hair_mask, beard_mask = get_masks_sequential(img_resized)
|
| 210 |
|
| 211 |
logger.info(f"Hair mask sum: {np.sum(hair_mask):.0f}, Beard mask sum: {np.sum(beard_mask):.0f}")
|
| 212 |
-
|
| 213 |
logger.info(" Applying STRONG GREY HAIR...")
|
| 214 |
final_img = apply_strong_grey_hair(img_resized, hair_mask, beard_mask)
|
| 215 |
-
|
| 216 |
gc.collect()
|
| 217 |
logger.info("✅ Processing completed!")
|
| 218 |
return final_img.resize((ow, oh), Image.LANCZOS)
|
| 219 |
-
|
| 220 |
except Exception as e:
|
| 221 |
logger.error(f"❌ Processing error: {e}")
|
| 222 |
logger.error(traceback.format_exc())
|
|
@@ -228,10 +231,11 @@ def process_face_whitening(input_image: Image.Image) -> Image.Image:
|
|
| 228 |
app = FastAPI(title="Strong Grey Hair API")
|
| 229 |
|
| 230 |
app.add_middleware(CORSMiddleware,
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
|
|
|
| 235 |
|
| 236 |
@app.on_event("startup")
|
| 237 |
async def startup_event():
|
|
@@ -245,11 +249,13 @@ async def startup_event():
|
|
| 245 |
async def age_face(file: UploadFile = File(...)):
|
| 246 |
if not file.content_type.startswith("image/"):
|
| 247 |
raise HTTPException(400, "Only image files allowed")
|
|
|
|
| 248 |
contents = await file.read()
|
| 249 |
try:
|
| 250 |
input_image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 251 |
loop = asyncio.get_event_loop()
|
| 252 |
result = await loop.run_in_executor(executor, process_face_whitening, input_image)
|
|
|
|
| 253 |
buf = io.BytesIO()
|
| 254 |
result.save(buf, format="JPEG", quality=92, optimize=True)
|
| 255 |
buf.seek(0)
|
|
|
|
| 18 |
import io
|
| 19 |
import asyncio
|
| 20 |
from concurrent.futures import ThreadPoolExecutor
|
| 21 |
+
import logging
|
| 22 |
|
| 23 |
logging.basicConfig(level=logging.INFO)
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
|
| 26 |
# ================================================
|
| 27 |
# 2. CONFIG
|
| 28 |
# ================================================
|
| 29 |
BEARD_MODEL_PATH = "models/best_hair_117_epoch_v4.pt"
|
| 30 |
+
SAFE_IMG_SIZE = 768
|
|
|
|
| 31 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 32 |
+
USE_FP16 = DEVICE.type == "cuda" and torch.cuda.is_available()
|
| 33 |
|
| 34 |
logger.info(f"🚀 Device: {DEVICE}, FP16: {USE_FP16}")
|
| 35 |
|
|
|
|
| 40 |
|
| 41 |
face_processor = None
|
| 42 |
face_parser = None
|
| 43 |
+
beard_model = None
|
| 44 |
|
| 45 |
# ================================================
|
| 46 |
# 3. LOAD MODELS
|
|
|
|
| 49 |
global face_processor, face_parser
|
| 50 |
if face_parser is not None:
|
| 51 |
return face_processor, face_parser
|
| 52 |
+
|
| 53 |
logger.info("Loading Segformer face-parsing...")
|
| 54 |
face_processor = SegformerImageProcessor.from_pretrained("jonathandinu/face-parsing")
|
| 55 |
face_parser = SegformerForSemanticSegmentation.from_pretrained("jonathandinu/face-parsing")
|
|
|
|
| 58 |
if USE_FP16:
|
| 59 |
face_parser = face_parser.half()
|
| 60 |
logger.info("✅ Face parser loaded!")
|
| 61 |
+
return face_processor, face_parser
|
| 62 |
|
| 63 |
def load_beard_model():
|
| 64 |
global beard_model
|
|
|
|
| 67 |
raise FileNotFoundError(f"❌ Beard model not found: {BEARD_MODEL_PATH}")
|
| 68 |
logger.info("Loading Beard YOLO model...")
|
| 69 |
beard_model = YOLO(BEARD_MODEL_PATH)
|
| 70 |
+
return beard_model
|
| 71 |
|
| 72 |
# ================================================
|
| 73 |
+
# 4. MASK FUNCTIONS (Improved Hair Mask)
|
| 74 |
# ================================================
|
|
|
|
| 75 |
def get_beard_mask_fast(pil_image: Image.Image) -> np.ndarray:
|
| 76 |
temp = f"temp_{uuid.uuid4().hex[:8]}.jpg"
|
| 77 |
try:
|
| 78 |
img_small = pil_image.resize((256, 256), Image.LANCZOS)
|
| 79 |
img_small.save(temp)
|
| 80 |
model = load_beard_model()
|
| 81 |
+
res = model(temp, device=DEVICE.type, conf=0.3, iou=0.5, verbose=False, half=USE_FP16, imgsz=256)
|
|
|
|
| 82 |
|
| 83 |
h, w = np.array(pil_image).shape[:2]
|
| 84 |
mask = np.zeros((h, w), dtype=np.float32)
|
|
|
|
| 102 |
os.remove(temp)
|
| 103 |
|
| 104 |
def get_hair_mask_fast(pil_image: Image.Image) -> np.ndarray:
|
| 105 |
+
"""Improved version - better captures forehead & thin hairs"""
|
| 106 |
processor, parser = load_face_parser()
|
| 107 |
img_small = pil_image.resize((256, 256), Image.LANCZOS)
|
| 108 |
inputs = processor(images=img_small, return_tensors="pt").to(DEVICE)
|
|
|
|
| 112 |
with torch.no_grad():
|
| 113 |
out = parser(**inputs)
|
| 114 |
logits = out.logits
|
| 115 |
+
up = torch.nn.functional.interpolate(logits, size=img_small.size[::-1], mode="bilinear", align_corners=False)
|
|
|
|
| 116 |
probs = torch.softmax(up.float() if USE_FP16 else up, dim=1)[0]
|
| 117 |
+
|
| 118 |
+
# ================== IMPROVED HAIR MASK ==================
|
| 119 |
+
# Lower threshold to catch thin & forehead hairs
|
| 120 |
+
hair = (probs[13].cpu().numpy() > 0.07).astype(np.float32)
|
| 121 |
+
|
| 122 |
+
# Extra soft hair capture for very fine hairs
|
| 123 |
+
soft_hair = (probs[13].cpu().numpy() > 0.03).astype(np.float32)
|
| 124 |
|
| 125 |
+
# Combine strong + soft hairs
|
| 126 |
+
hair = np.maximum(hair, soft_hair * 0.55)
|
| 127 |
+
|
| 128 |
+
# Face mask - exclude inner face but keep more forehead area
|
| 129 |
face_cls = list(range(1, 6)) + list(range(8, 13)) + [17, 18]
|
| 130 |
parsing = up.argmax(dim=1).squeeze(0).cpu().numpy()
|
| 131 |
face_m = np.isin(parsing, face_cls).astype(np.float32)
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
# Reduced dilation so forehead hairs are not suppressed
|
| 134 |
+
face_m = cv2.dilate(face_m, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=1)
|
| 135 |
+
|
| 136 |
+
hair = hair * (1 - face_m)
|
| 137 |
+
|
| 138 |
+
# Morphology - better connection for scattered hairs
|
| 139 |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
| 140 |
hair = cv2.morphologyEx(hair, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 141 |
+
hair = cv2.morphologyEx(hair, cv2.MORPH_CLOSE, kernel, iterations=2) # Increased for better thin hair connection
|
| 142 |
+
|
| 143 |
+
hair = cv2.GaussianBlur(hair, (5, 5), 1.5) # Smoother blend
|
| 144 |
+
|
| 145 |
oh, ow = np.array(pil_image).shape[:2]
|
| 146 |
return np.clip(cv2.resize(hair, (ow, oh)), 0, 1)
|
| 147 |
|
|
|
|
| 151 |
# ================================================
|
| 152 |
# 5. 🔥 STRONG GREY HAIR - INCREASED INTENSITY
|
| 153 |
# ================================================
|
|
|
|
| 154 |
def apply_strong_grey_hair(image: Image.Image, hair_mask: np.ndarray, beard_mask: np.ndarray) -> Image.Image:
|
| 155 |
+
""" Strong grey effect - very visible but natural """
|
|
|
|
|
|
|
| 156 |
comb = np.maximum(hair_mask, beard_mask)
|
|
|
|
| 157 |
if np.sum(comb) < 100:
|
| 158 |
logger.warning("⚠️ Small mask area")
|
| 159 |
+
|
| 160 |
# Soften edges
|
| 161 |
comb = cv2.GaussianBlur(comb, (7, 7), 2)
|
| 162 |
+
|
| 163 |
img = np.array(image).astype(np.float32)
|
| 164 |
+
|
| 165 |
# ========================================
|
| 166 |
# STRONG GREY CONVERSION
|
| 167 |
# ========================================
|
| 168 |
hsv = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_RGB2HSV).astype(np.float32)
|
| 169 |
+
|
| 170 |
# 1. COMPLETE desaturation (100%)
|
| 171 |
+
hsv[:,:,1] = hsv[:,:,1] * (1 - 1.0 * comb)
|
| 172 |
+
|
| 173 |
# 2. Increase brightness to grey level (STRONG boost)
|
|
|
|
| 174 |
hsv[:,:,2] = hsv[:,:,2] + (110 * comb)
|
| 175 |
+
hsv[:,:,2] = np.clip(hsv[:,:,2], 120, 230)
|
| 176 |
+
|
| 177 |
# 3. NO hue change - prevents blue/green
|
|
|
|
|
|
|
| 178 |
grey_result = cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2RGB).astype(np.float32)
|
| 179 |
+
|
| 180 |
# ========================================
|
| 181 |
# Blend - 85% new, 15% original (stronger)
|
| 182 |
# ========================================
|
| 183 |
+
blend = 0.85 * comb
|
| 184 |
mask_3ch = np.stack([blend, blend, blend], axis=2)
|
|
|
|
| 185 |
final = grey_result * mask_3ch + img * (1 - mask_3ch)
|
| 186 |
+
|
| 187 |
# ========================================
|
| 188 |
# Very slight warm tint (barely noticeable)
|
| 189 |
# ========================================
|
| 190 |
warm = np.array([5, 3, 0], dtype=np.float32)
|
| 191 |
final = final + (warm * comb[..., None] * 0.2)
|
|
|
|
| 192 |
final = np.clip(final, 0, 255).astype(np.uint8)
|
| 193 |
+
|
| 194 |
# Optional: Slight sharpening for definition
|
| 195 |
result = Image.fromarray(final)
|
| 196 |
result = result.filter(ImageFilter.UnsharpMask(radius=0.5, percent=50, threshold=0))
|
|
|
|
| 197 |
return result
|
| 198 |
|
| 199 |
def process_face_whitening(input_image: Image.Image) -> Image.Image:
|
|
|
|
| 202 |
logger.info(f"→ Processing image: {input_image.size}")
|
| 203 |
orig = input_image.convert("RGB")
|
| 204 |
ow, oh = orig.size
|
| 205 |
+
|
| 206 |
target_size = min(SAFE_IMG_SIZE, max(ow, oh))
|
| 207 |
if target_size % 2 == 1:
|
| 208 |
target_size -= 1
|
| 209 |
+
|
| 210 |
img_resized = orig.resize((target_size, target_size), Image.LANCZOS)
|
| 211 |
|
| 212 |
logger.info(" Generating hair & beard masks...")
|
| 213 |
hair_mask, beard_mask = get_masks_sequential(img_resized)
|
| 214 |
|
| 215 |
logger.info(f"Hair mask sum: {np.sum(hair_mask):.0f}, Beard mask sum: {np.sum(beard_mask):.0f}")
|
| 216 |
+
|
| 217 |
logger.info(" Applying STRONG GREY HAIR...")
|
| 218 |
final_img = apply_strong_grey_hair(img_resized, hair_mask, beard_mask)
|
| 219 |
+
|
| 220 |
gc.collect()
|
| 221 |
logger.info("✅ Processing completed!")
|
| 222 |
return final_img.resize((ow, oh), Image.LANCZOS)
|
|
|
|
| 223 |
except Exception as e:
|
| 224 |
logger.error(f"❌ Processing error: {e}")
|
| 225 |
logger.error(traceback.format_exc())
|
|
|
|
| 231 |
app = FastAPI(title="Strong Grey Hair API")
|
| 232 |
|
| 233 |
app.add_middleware(CORSMiddleware,
|
| 234 |
+
allow_origins=["*"],
|
| 235 |
+
allow_credentials=True,
|
| 236 |
+
allow_methods=["*"],
|
| 237 |
+
allow_headers=["*"]
|
| 238 |
+
)
|
| 239 |
|
| 240 |
@app.on_event("startup")
|
| 241 |
async def startup_event():
|
|
|
|
| 249 |
async def age_face(file: UploadFile = File(...)):
|
| 250 |
if not file.content_type.startswith("image/"):
|
| 251 |
raise HTTPException(400, "Only image files allowed")
|
| 252 |
+
|
| 253 |
contents = await file.read()
|
| 254 |
try:
|
| 255 |
input_image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 256 |
loop = asyncio.get_event_loop()
|
| 257 |
result = await loop.run_in_executor(executor, process_face_whitening, input_image)
|
| 258 |
+
|
| 259 |
buf = io.BytesIO()
|
| 260 |
result.save(buf, format="JPEG", quality=92, optimize=True)
|
| 261 |
buf.seek(0)
|