Spaces:
Sleeping
Sleeping
Update detect.py
Browse files
detect.py
CHANGED
|
@@ -1,14 +1,45 @@
|
|
| 1 |
import logging
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
logger = logging.getLogger(__name__)
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def detect_face_shape(image_path):
|
| 7 |
"""
|
| 8 |
Detects face shape using PIL and trained Mean Face model.
|
| 9 |
"""
|
| 10 |
from PIL import Image
|
| 11 |
-
import numpy as np
|
| 12 |
from classifier import classify_face_shape
|
| 13 |
|
| 14 |
try:
|
|
@@ -22,6 +53,11 @@ def detect_face_shape(image_path):
|
|
| 22 |
if img is None:
|
| 23 |
raise ValueError("Could not load image")
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# Classify directly (classifier handles resizing/grayscale)
|
| 26 |
shape_probs = classify_face_shape(img)
|
| 27 |
|
|
|
|
| 1 |
import logging
|
| 2 |
+
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
logger = logging.getLogger(__name__)
|
| 7 |
|
| 8 |
+
|
| 9 |
+
def _expand_box(x, y, w, h, img_w, img_h, scale=1.2):
|
| 10 |
+
pad_w = int(w * (scale - 1) / 2)
|
| 11 |
+
pad_h = int(h * (scale - 1) / 2)
|
| 12 |
+
x1 = max(0, x - pad_w)
|
| 13 |
+
y1 = max(0, y - pad_h)
|
| 14 |
+
x2 = min(img_w, x + w + pad_w)
|
| 15 |
+
y2 = min(img_h, y + h + pad_h)
|
| 16 |
+
return x1, y1, x2, y2
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _crop_to_face(img):
|
| 20 |
+
cv_img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
| 21 |
+
gray = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY)
|
| 22 |
+
|
| 23 |
+
face_cascade_path = cv2.data.haarcascades + "haarcascade_frontalface_alt2.xml"
|
| 24 |
+
face_cascade = cv2.CascadeClassifier(face_cascade_path)
|
| 25 |
+
if face_cascade.empty():
|
| 26 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
| 27 |
+
|
| 28 |
+
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
| 29 |
+
if len(faces) == 0:
|
| 30 |
+
logger.info("No face detected; using full image for classification.")
|
| 31 |
+
return img
|
| 32 |
+
|
| 33 |
+
x, y, w, h = max(faces, key=lambda f: f[2] * f[3])
|
| 34 |
+
img_h, img_w = gray.shape[:2]
|
| 35 |
+
x1, y1, x2, y2 = _expand_box(x, y, w, h, img_w, img_h, scale=1.25)
|
| 36 |
+
return img.crop((x1, y1, x2, y2))
|
| 37 |
+
|
| 38 |
def detect_face_shape(image_path):
|
| 39 |
"""
|
| 40 |
Detects face shape using PIL and trained Mean Face model.
|
| 41 |
"""
|
| 42 |
from PIL import Image
|
|
|
|
| 43 |
from classifier import classify_face_shape
|
| 44 |
|
| 45 |
try:
|
|
|
|
| 53 |
if img is None:
|
| 54 |
raise ValueError("Could not load image")
|
| 55 |
|
| 56 |
+
# Crop to detected face region to improve accuracy
|
| 57 |
+
if isinstance(image_path, str):
|
| 58 |
+
img = img.convert("RGB")
|
| 59 |
+
img = _crop_to_face(img)
|
| 60 |
+
|
| 61 |
# Classify directly (classifier handles resizing/grayscale)
|
| 62 |
shape_probs = classify_face_shape(img)
|
| 63 |
|