Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -44,24 +44,20 @@ def extract_persons(image: np.ndarray, bboxes: np.ndarray) -> list[np.ndarray]:
|
|
44 |
return person_images
|
45 |
|
46 |
|
47 |
-
def
|
48 |
-
"""Convert a NumPy image array to a
|
49 |
-
|
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[
|
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 [
|
65 |
|
66 |
|
67 |
examples = sorted(pathlib.Path("images").glob("*.jpg"))
|
|
|
44 |
return person_images
|
45 |
|
46 |
|
47 |
+
def convert_to_pil_image(image: np.ndarray) -> Image.Image:
|
48 |
+
"""Convert a NumPy image array to a PIL Image."""
|
49 |
+
return Image.fromarray(image)
|
|
|
|
|
|
|
|
|
50 |
|
51 |
|
52 |
detector = load_model()
|
53 |
detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
|
54 |
|
55 |
|
56 |
+
def detect(image: np.ndarray) -> list[Image.Image]:
|
57 |
image = image[:, :, ::-1] # RGB -> BGR
|
58 |
bboxes = detect_person(image, detector)
|
59 |
person_images = extract_persons(image, bboxes) # Extract each person as a separate image
|
60 |
+
return [convert_to_pil_image(person_img[:, :, ::-1]) for person_img in person_images] # BGR -> RGB
|
61 |
|
62 |
|
63 |
examples = sorted(pathlib.Path("images").glob("*.jpg"))
|