Saad0KH commited on
Commit
8d7dd3a
·
verified ·
1 Parent(s): 190a01a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -1
app.py CHANGED
@@ -33,4 +33,37 @@ def detect_person(
33
  return bboxes
34
 
35
 
36
- def extract
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  return bboxes
34
 
35
 
36
+ def extract_persons(image: np.ndarray, bboxes: np.ndarray) -> list[np.ndarray]:
37
+ person_images = []
38
+ for bbox in bboxes:
39
+ x1, y1, x2, y2 = bbox
40
+ person_image = image[y1:y2, x1:x2] # Crop the detected person
41
+ person_images.append(person_image)
42
+ return person_images
43
+
44
+
45
+ detector = load_model()
46
+ detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
47
+
48
+
49
+ def detect(image: np.ndarray) -> list[np.ndarray]:
50
+ image = image[:, :, ::-1] # RGB -> BGR
51
+ bboxes = detect_person(image, detector)
52
+ person_images = extract_persons(image, bboxes) # Extract each person as a separate image
53
+ return [person_img[:, :, ::-1] for person_img in person_images] # BGR -> RGB
54
+
55
+
56
+ examples = sorted(pathlib.Path("images").glob("*.jpg"))
57
+
58
+ demo = gr.Interface(
59
+ fn=detect,
60
+ inputs=gr.Image(label="Input", type="numpy"),
61
+ outputs=gr.Image(label="Detected Persons", type="numpy", shape=(None, None)), # Multiple outputs
62
+ examples=examples,
63
+ examples_per_page=30,
64
+ title=TITLE,
65
+ description=DESCRIPTION,
66
+ )
67
+
68
+ if __name__ == "__main__":
69
+ demo.queue(max_size=10).launch()