Saad0KH commited on
Commit
2f1c571
·
verified ·
1 Parent(s): 5ab0afc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -1
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,6 +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
 
@@ -59,7 +78,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"), # Display multiple images in a gallery
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 encode_pil_to_bytes(img: Image.Image, format="WEBP", fallback_format="PNG") -> bytes:
48
+ output_bytes = io.BytesIO()
49
+ try:
50
+ img.save(output_bytes, format=format)
51
+ except Exception as e:
52
+ print(f"WebP save failed: {e}, falling back to {fallback_format}")
53
+ img.save(output_bytes, format=fallback_format)
54
+ return output_bytes.getvalue()
55
+
56
+
57
+ def save_image_with_fallback(img: Image.Image, cache_dir: pathlib.Path, fallback_format="PNG") -> pathlib.Path:
58
+ try:
59
+ return gr.processing_utils.save_pil_to_cache(img, cache_dir, format="WEBP")
60
+ except Exception as e:
61
+ print(f"WebP save failed: {e}, falling back to {fallback_format}")
62
+ return gr.processing_utils.save_pil_to_cache(img, cache_dir, format=fallback_format)
63
+
64
+
65
  detector = load_model()
66
  detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
67
 
 
78
  demo = gr.Interface(
79
  fn=detect,
80
  inputs=gr.Image(label="Input", type="numpy"),
81
+ outputs=gr.Gallery(label="Detected Persons", postprocess=save_image_with_fallback), # Display multiple images in a gallery
82
  examples=examples,
83
  cache_examples=False, # Disable caching of examples
84
  examples_per_page=30,