saketh-005 commited on
Commit
1a84c6b
·
verified ·
1 Parent(s): 7445760

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -23
app.py CHANGED
@@ -1,41 +1,33 @@
1
  import gradio as gr
2
- import mediapipe as mp
3
  import numpy as np
 
4
 
5
- mp_face = mp.solutions.face_detection
6
- face_detection = mp_face.FaceDetection(model_selection=0, min_detection_confidence=0.5)
7
 
8
  def detect_faces(image):
9
  image_np = np.array(image)
10
- results = face_detection.process(image_np)
11
 
12
- faces = []
13
-
14
- if results.detections:
15
- for detection in results.detections:
16
- bbox = detection.location_data.relative_bounding_box
17
 
18
- h, w, _ = image_np.shape
19
-
20
- xmin = int(bbox.xmin * w)
21
- ymin = int(bbox.ymin * h)
22
- width = int(bbox.width * w)
23
- height = int(bbox.height * h)
24
 
25
- xmax = xmin + width
26
- ymax = ymin + height
27
 
 
 
28
  cx = (xmin + xmax) / 2
29
  cy = (ymin + ymax) / 2
30
 
31
  faces.append({
32
- "cx": cx,
33
- "cy": cy,
34
  "box": {
35
- "xmin": xmin,
36
- "ymin": ymin,
37
- "xmax": xmax,
38
- "ymax": ymax
39
  }
40
  })
41
 
 
1
  import gradio as gr
 
2
  import numpy as np
3
+ from ultralytics import YOLO
4
 
5
+ # Use lightweight face model
6
+ model = YOLO("yolov8n.pt") # lightweight model
7
 
8
  def detect_faces(image):
9
  image_np = np.array(image)
 
10
 
11
+ results = model(image_np)
 
 
 
 
12
 
13
+ faces = []
 
 
 
 
 
14
 
15
+ for r in results:
16
+ boxes = r.boxes.xyxy.cpu().numpy()
17
 
18
+ for box in boxes:
19
+ xmin, ymin, xmax, ymax = box
20
  cx = (xmin + xmax) / 2
21
  cy = (ymin + ymax) / 2
22
 
23
  faces.append({
24
+ "cx": float(cx),
25
+ "cy": float(cy),
26
  "box": {
27
+ "xmin": float(xmin),
28
+ "ymin": float(ymin),
29
+ "xmax": float(xmax),
30
+ "ymax": float(ymax)
31
  }
32
  })
33