saketh-005 commited on
Commit
660fdf1
·
verified ·
1 Parent(s): 4c3e2dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -1,27 +1,43 @@
1
  import gradio as gr
2
- import face_recognition
3
  import numpy as np
4
 
 
 
 
5
  def detect_faces(image):
6
- image = np.array(image)
7
- locations = face_recognition.face_locations(image)
8
- encodings = face_recognition.face_encodings(image, locations)
9
 
10
  faces = []
11
- for (top, right, bottom, left), encoding in zip(locations, encodings):
12
- cx = (left + right) / 2
13
- cy = (top + bottom) / 2
14
- faces.append({
15
- "cx": cx,
16
- "cy": cy,
17
- "box": {
18
- "top": top,
19
- "right": right,
20
- "bottom": bottom,
21
- "left": left
22
- },
23
- "embedding": encoding.tolist()
24
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  return faces
27
 
 
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
 
42
  return faces
43