Saad0KH commited on
Commit
ffa93bd
·
verified ·
1 Parent(s): 70eacb3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -6
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"
@@ -75,19 +76,27 @@ def convert_to_pil_image(image: np.ndarray) -> Image.Image:
75
  return Image.fromarray(image)
76
 
77
 
 
 
 
 
 
 
 
 
78
  detector = load_model()
79
  detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
80
 
81
 
82
- def detect(image: np.ndarray) -> tuple[Image.Image, list[Image.Image]]:
83
  image = image[:, :, ::-1] # RGB -> BGR
84
  bboxes, vbboxes = detect_person(image, detector)
85
  res = visualize(image, bboxes, vbboxes)
86
  person_images = extract_persons(image, bboxes)
87
- # Convert the images to PIL Image format
88
- result_image = convert_to_pil_image(res[:, :, ::-1]) # BGR -> RGB
89
- person_images_pil = [convert_to_pil_image(person_img[:, :, ::-1]) for person_img in person_images] # BGR -> RGB
90
- return result_image, person_images_pil
91
 
92
 
93
  examples = sorted(pathlib.Path("images").glob("*.jpg"))
@@ -95,7 +104,7 @@ examples = sorted(pathlib.Path("images").glob("*.jpg"))
95
  demo = gr.Interface(
96
  fn=detect,
97
  inputs=gr.Image(label="Input", type="numpy"),
98
- outputs=[gr.Image(label="Processed Image"), gr.Gallery(label="Detected Persons")],
99
  examples=examples,
100
  examples_per_page=30,
101
  title=TITLE,
 
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"
 
76
  return Image.fromarray(image)
77
 
78
 
79
+ def convert_to_png(image: np.ndarray) -> bytes:
80
+ """Convert a NumPy image array to PNG bytes."""
81
+ pil_image = convert_to_pil_image(image)
82
+ buffer = io.BytesIO()
83
+ pil_image.save(buffer, format="PNG")
84
+ return buffer.getvalue()
85
+
86
+
87
  detector = load_model()
88
  detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
89
 
90
 
91
+ def detect(image: np.ndarray) -> tuple[bytes, list[bytes]]:
92
  image = image[:, :, ::-1] # RGB -> BGR
93
  bboxes, vbboxes = detect_person(image, detector)
94
  res = visualize(image, bboxes, vbboxes)
95
  person_images = extract_persons(image, bboxes)
96
+ # Convert the images to PNG bytes
97
+ result_image_bytes = convert_to_png(res[:, :, ::-1]) # BGR -> RGB
98
+ person_images_bytes = [convert_to_png(person_img[:, :, ::-1]) for person_img in person_images] # BGR -> RGB
99
+ return result_image_bytes, person_images_bytes
100
 
101
 
102
  examples = sorted(pathlib.Path("images").glob("*.jpg"))
 
104
  demo = gr.Interface(
105
  fn=detect,
106
  inputs=gr.Image(label="Input", type="numpy"),
107
+ outputs=[gr.Image(label="Processed Image", type="bytes"), gr.Gallery(label="Detected Persons", type="bytes")],
108
  examples=examples,
109
  examples_per_page=30,
110
  title=TITLE,