Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,13 +8,18 @@ import mediapipe as mp
|
|
| 8 |
from collections import defaultdict
|
| 9 |
|
| 10 |
# -------------------- CONFIG --------------------
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
imgsz = 1024
|
| 14 |
|
| 15 |
# -------------------- INIT --------------------
|
| 16 |
app = Flask(__name__)
|
| 17 |
-
|
|
|
|
| 18 |
mp_face_mesh = mp.solutions.face_mesh
|
| 19 |
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True)
|
| 20 |
|
|
@@ -55,35 +60,42 @@ def analyze():
|
|
| 55 |
hull = cv2.convexHull(points)
|
| 56 |
face_area = cv2.contourArea(hull)
|
| 57 |
face_polygon = Polygon(hull.reshape(-1, 2))
|
| 58 |
-
break # only first face
|
| 59 |
else:
|
| 60 |
return jsonify({"success": False, "error": "No face detected"}), 400
|
| 61 |
|
| 62 |
-
# --------------------
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
skin_percentages
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# -------------------- RUN --------------------
|
| 89 |
if __name__ == "__main__":
|
|
|
|
| 8 |
from collections import defaultdict
|
| 9 |
|
| 10 |
# -------------------- CONFIG --------------------
|
| 11 |
+
model_paths = {
|
| 12 |
+
"pores": "pores.pt",
|
| 13 |
+
"newpig": "newpig.pt",
|
| 14 |
+
"wrinkle": "wrinkle.pt"
|
| 15 |
+
}
|
| 16 |
+
default_conf_threshold = 0.05
|
| 17 |
imgsz = 1024
|
| 18 |
|
| 19 |
# -------------------- INIT --------------------
|
| 20 |
app = Flask(__name__)
|
| 21 |
+
models = {name: YOLO(path) for name, path in model_paths.items()}
|
| 22 |
+
|
| 23 |
mp_face_mesh = mp.solutions.face_mesh
|
| 24 |
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True)
|
| 25 |
|
|
|
|
| 60 |
hull = cv2.convexHull(points)
|
| 61 |
face_area = cv2.contourArea(hull)
|
| 62 |
face_polygon = Polygon(hull.reshape(-1, 2))
|
| 63 |
+
break # only process the first face
|
| 64 |
else:
|
| 65 |
return jsonify({"success": False, "error": "No face detected"}), 400
|
| 66 |
|
| 67 |
+
# -------------------- RUN ALL MODELS --------------------
|
| 68 |
+
all_percentages = {}
|
| 69 |
+
|
| 70 |
+
for model_name, model in models.items():
|
| 71 |
+
# Set confidence threshold per model
|
| 72 |
+
conf = 0.03 if model_name == "newpig" else default_conf_threshold
|
| 73 |
+
|
| 74 |
+
results = model(img, conf=conf, imgsz=imgsz)
|
| 75 |
+
boxes_xy = results[0].boxes.xyxy.cpu().numpy()
|
| 76 |
+
boxes_cls = results[0].boxes.cls.cpu().numpy().astype(int)
|
| 77 |
+
|
| 78 |
+
class_polygons = defaultdict(list)
|
| 79 |
+
|
| 80 |
+
for i, cls_id in enumerate(boxes_cls):
|
| 81 |
+
x1, y1, x2, y2 = boxes_xy[i].astype(int)
|
| 82 |
+
det_poly = shapely_box(x1, y1, x2, y2)
|
| 83 |
+
if face_polygon.intersects(det_poly):
|
| 84 |
+
intersection = det_poly.intersection(face_polygon)
|
| 85 |
+
if intersection.area > 0:
|
| 86 |
+
class_polygons[cls_id].append(intersection)
|
| 87 |
+
|
| 88 |
+
# Calculate percentages
|
| 89 |
+
skin_percentages = {}
|
| 90 |
+
for cls_id, polys in class_polygons.items():
|
| 91 |
+
union_poly = unary_union(polys)
|
| 92 |
+
pixels = union_poly.area
|
| 93 |
+
percentage = (pixels / face_area) * 100 if face_area > 0 else 0.0
|
| 94 |
+
skin_percentages[model.names.get(cls_id, str(cls_id))] = round(percentage, 2)
|
| 95 |
+
|
| 96 |
+
all_percentages[model_name] = skin_percentages
|
| 97 |
+
|
| 98 |
+
return jsonify({"success": True, "percentages": all_percentages})
|
| 99 |
|
| 100 |
# -------------------- RUN --------------------
|
| 101 |
if __name__ == "__main__":
|