JANGALA SAKETH commited on
Commit
8c10e13
·
verified ·
1 Parent(s): 33bdbb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -23
app.py CHANGED
@@ -1,45 +1,59 @@
1
  import gradio as gr
2
  import numpy as np
 
3
  from ultralytics import YOLO
 
4
 
5
- model = YOLO("yolov8n.pt")
 
 
 
6
 
7
- def detect_persons(image):
8
  image_np = np.array(image)
9
- results = model(image_np)
10
 
11
- persons = []
 
 
12
 
13
  for r in results:
14
  for box, cls, conf in zip(r.boxes.xyxy, r.boxes.cls, r.boxes.conf):
15
 
16
- # class 0 in COCO = person
17
  if int(cls) != 0:
18
  continue
19
-
20
  if conf < 0.4:
21
  continue
22
 
23
  xmin, ymin, xmax, ymax = box.cpu().numpy()
24
- cx = (xmin + xmax) / 2
25
- cy = (ymin + ymax) / 2
26
-
27
- persons.append({
28
- "cx": float(cx),
29
- "cy": float(cy),
30
- "confidence": float(conf),
31
- "box": {
32
- "xmin": float(xmin),
33
- "ymin": float(ymin),
34
- "xmax": float(xmax),
35
- "ymax": float(ymax)
36
- }
37
- })
38
-
39
- return persons
 
 
 
 
 
 
 
 
 
40
 
41
  iface = gr.Interface(
42
- fn=detect_persons,
43
  inputs=gr.Image(type="pil"),
44
  outputs="json"
45
  )
 
1
  import gradio as gr
2
  import numpy as np
3
+ import cv2
4
  from ultralytics import YOLO
5
+ import insightface
6
 
7
+ # Load models
8
+ yolo = YOLO("yolov8n.pt")
9
+ face_model = insightface.app.FaceAnalysis(name="buffalo_l")
10
+ face_model.prepare(ctx_id=0)
11
 
12
+ def process_image(image):
13
  image_np = np.array(image)
 
14
 
15
+ results = yolo(image_np)
16
+
17
+ faces_output = []
18
 
19
  for r in results:
20
  for box, cls, conf in zip(r.boxes.xyxy, r.boxes.cls, r.boxes.conf):
21
 
22
+ # Only detect persons
23
  if int(cls) != 0:
24
  continue
 
25
  if conf < 0.4:
26
  continue
27
 
28
  xmin, ymin, xmax, ymax = box.cpu().numpy()
29
+ xmin, ymin, xmax, ymax = map(int, [xmin, ymin, xmax, ymax])
30
+
31
+ person_crop = image_np[ymin:ymax, xmin:xmax]
32
+
33
+ detected_faces = face_model.get(person_crop)
34
+
35
+ for face in detected_faces:
36
+ embedding = face.embedding.tolist()
37
+
38
+ cx = (xmin + xmax) / 2
39
+ cy = (ymin + ymax) / 2
40
+
41
+ faces_output.append({
42
+ "cx": float(cx),
43
+ "cy": float(cy),
44
+ "box": {
45
+ "xmin": xmin,
46
+ "ymin": ymin,
47
+ "xmax": xmax,
48
+ "ymax": ymax
49
+ },
50
+ "embedding": embedding
51
+ })
52
+
53
+ return faces_output
54
 
55
  iface = gr.Interface(
56
+ fn=process_image,
57
  inputs=gr.Image(type="pil"),
58
  outputs="json"
59
  )