Saad0KH commited on
Commit
7f60f0d
·
verified ·
1 Parent(s): 16cde70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -3
app.py CHANGED
@@ -9,6 +9,7 @@ import insightface
9
  import numpy as np
10
  import onnxruntime as ort
11
  from PIL import Image
 
12
 
13
  TITLE = "insightface Person Detection"
14
  DESCRIPTION = "https://github.com/deepinsight/insightface/tree/master/examples/person_detection"
@@ -43,15 +44,24 @@ def extract_persons(image: np.ndarray, bboxes: np.ndarray) -> list[np.ndarray]:
43
  return person_images
44
 
45
 
 
 
 
 
 
 
 
 
 
46
  detector = load_model()
47
  detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
48
 
49
 
50
- def detect(image: np.ndarray) -> list[np.ndarray]:
51
  image = image[:, :, ::-1] # RGB -> BGR
52
  bboxes = detect_person(image, detector)
53
  person_images = extract_persons(image, bboxes) # Extract each person as a separate image
54
- return [person_img[:, :, ::-1] for person_img in person_images] # BGR -> RGB
55
 
56
 
57
  examples = sorted(pathlib.Path("images").glob("*.jpg"))
@@ -59,7 +69,7 @@ examples = sorted(pathlib.Path("images").glob("*.jpg"))
59
  demo = gr.Interface(
60
  fn=detect,
61
  inputs=gr.Image(label="Input", type="numpy"),
62
- outputs=gr.Gallery(label="Detected Persons"), # Removed the postprocess argument
63
  examples=examples,
64
  cache_examples=False, # Disable caching of examples
65
  examples_per_page=30,
 
9
  import numpy as np
10
  import onnxruntime as ort
11
  from PIL import Image
12
+ import io
13
 
14
  TITLE = "insightface Person Detection"
15
  DESCRIPTION = "https://github.com/deepinsight/insightface/tree/master/examples/person_detection"
 
44
  return person_images
45
 
46
 
47
+ def convert_to_png(image: np.ndarray) -> bytes:
48
+ """Convert a NumPy image array to a PNG byte stream."""
49
+ pil_image = Image.fromarray(image)
50
+ buffer = io.BytesIO()
51
+ pil_image.save(buffer, format="PNG")
52
+ buffer.seek(0)
53
+ return buffer.read()
54
+
55
+
56
  detector = load_model()
57
  detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
58
 
59
 
60
+ def detect(image: np.ndarray) -> list[bytes]:
61
  image = image[:, :, ::-1] # RGB -> BGR
62
  bboxes = detect_person(image, detector)
63
  person_images = extract_persons(image, bboxes) # Extract each person as a separate image
64
+ return [convert_to_png(person_img[:, :, ::-1]) for person_img in person_images] # BGR -> RGB
65
 
66
 
67
  examples = sorted(pathlib.Path("images").glob("*.jpg"))
 
69
  demo = gr.Interface(
70
  fn=detect,
71
  inputs=gr.Image(label="Input", type="numpy"),
72
+ outputs=gr.Gallery(label="Detected Persons"), # Display multiple images in a gallery
73
  examples=examples,
74
  cache_examples=False, # Disable caching of examples
75
  examples_per_page=30,