anujakkulkarni commited on
Commit
f51ce5a
·
verified ·
1 Parent(s): 3a2497b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -29
app.py CHANGED
@@ -8,13 +8,18 @@ import mediapipe as mp
8
  from collections import defaultdict
9
 
10
  # -------------------- CONFIG --------------------
11
- skin_model_path = "pores.pt" # replace with your model in HF Space
12
- conf_threshold = 0.05
 
 
 
 
13
  imgsz = 1024
14
 
15
  # -------------------- INIT --------------------
16
  app = Flask(__name__)
17
- skin_model = YOLO(skin_model_path)
 
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
- # -------------------- SKIN DETECTION --------------------
63
- results = skin_model(img, conf=conf_threshold, imgsz=imgsz)
64
- boxes_xy = results[0].boxes.xyxy.cpu().numpy()
65
- boxes_conf = results[0].boxes.conf.cpu().numpy()
66
- boxes_cls = results[0].boxes.cls.cpu().numpy().astype(int)
67
-
68
- class_polygons = defaultdict(list)
69
-
70
- for i, (xy, cls_id) in enumerate(zip(boxes_xy, boxes_cls)):
71
- x1, y1, x2, y2 = xy.astype(int)
72
- det_poly = shapely_box(x1, y1, x2, y2)
73
- if face_polygon.intersects(det_poly):
74
- intersection = det_poly.intersection(face_polygon)
75
- if intersection.area > 0:
76
- class_polygons[cls_id].append(intersection)
77
-
78
- # -------------------- CALCULATE PERCENTAGES --------------------
79
- skin_percentages = {}
80
- for cls_id, polys in class_polygons.items():
81
- union_poly = unary_union(polys)
82
- pixels = union_poly.area
83
- percentage = (pixels / face_area) * 100 if face_area > 0 else 0.0
84
- skin_percentages[skin_model.names.get(cls_id, str(cls_id))] = round(percentage, 2)
85
-
86
- return jsonify({"success": True, "percentages": skin_percentages})
 
 
 
 
 
 
 
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__":