chromadb-api / app.py
Saad0KH's picture
Update app.py
ffa93bd verified
raw
history blame
3.85 kB
#!/usr/bin/env python
from __future__ import annotations
import pathlib
import cv2
import gradio as gr
import huggingface_hub
import insightface
import numpy as np
import onnxruntime as ort
from PIL import Image
import io
TITLE = "insightface Person Detection"
DESCRIPTION = "https://github.com/deepinsight/insightface/tree/master/examples/person_detection"
def load_model():
path = huggingface_hub.hf_hub_download("public-data/insightface", "models/scrfd_person_2.5g.onnx")
options = ort.SessionOptions()
options.intra_op_num_threads = 8
options.inter_op_num_threads = 8
session = ort.InferenceSession(
path, sess_options=options, providers=["CPUExecutionProvider", "CUDAExecutionProvider"]
)
model = insightface.model_zoo.retinaface.RetinaFace(model_file=path, session=session)
return model
def detect_person(
img: np.ndarray, detector: insightface.model_zoo.retinaface.RetinaFace
) -> tuple[np.ndarray, np.ndarray]:
bboxes, kpss = detector.detect(img)
bboxes = np.round(bboxes[:, :4]).astype(int)
kpss = np.round(kpss).astype(int)
kpss[:, :, 0] = np.clip(kpss[:, :, 0], 0, img.shape[1])
kpss[:, :, 1] = np.clip(kpss[:, :, 1], 0, img.shape[0])
vbboxes = bboxes.copy()
vbboxes[:, 0] = kpss[:, 0, 0]
vbboxes[:, 1] = kpss[:, 0, 1]
vbboxes[:, 2] = kpss[:, 4, 0]
vbboxes[:, 3] = kpss[:, 4, 1]
return bboxes, vbboxes
def visualize(image: np.ndarray, bboxes: np.ndarray, vbboxes: np.ndarray) -> np.ndarray:
res = image.copy()
for i in range(bboxes.shape[0]):
bbox = bboxes[i]
vbbox = vbboxes[i]
x1, y1, x2, y2 = bbox
vx1, vy1, vx2, vy2 = vbbox
cv2.rectangle(res, (x1, y1), (x2, y2), (0, 255, 0), 1)
alpha = 0.8
color = (255, 0, 0)
for c in range(3):
res[vy1:vy2, vx1:vx2, c] = res[vy1:vy2, vx1:vx2, c] * alpha + color[c] * (1.0 - alpha)
cv2.circle(res, (vx1, vy1), 1, color, 2)
cv2.circle(res, (vx1, vy2), 1, color, 2)
cv2.circle(res, (vx2, vy1), 1, color, 2)
cv2.circle(res, (vx2, vy2), 1, color, 2)
return res
def extract_persons(image: np.ndarray, bboxes: np.ndarray) -> list[np.ndarray]:
person_images = []
for bbox in bboxes:
x1, y1, x2, y2 = bbox
person_image = image[y1:y2, x1:x2] # Crop the detected person
person_images.append(person_image)
return person_images
def convert_to_pil_image(image: np.ndarray) -> Image.Image:
"""Convert a NumPy image array to a PIL Image."""
return Image.fromarray(image)
def convert_to_png(image: np.ndarray) -> bytes:
"""Convert a NumPy image array to PNG bytes."""
pil_image = convert_to_pil_image(image)
buffer = io.BytesIO()
pil_image.save(buffer, format="PNG")
return buffer.getvalue()
detector = load_model()
detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
def detect(image: np.ndarray) -> tuple[bytes, list[bytes]]:
image = image[:, :, ::-1] # RGB -> BGR
bboxes, vbboxes = detect_person(image, detector)
res = visualize(image, bboxes, vbboxes)
person_images = extract_persons(image, bboxes)
# Convert the images to PNG bytes
result_image_bytes = convert_to_png(res[:, :, ::-1]) # BGR -> RGB
person_images_bytes = [convert_to_png(person_img[:, :, ::-1]) for person_img in person_images] # BGR -> RGB
return result_image_bytes, person_images_bytes
examples = sorted(pathlib.Path("images").glob("*.jpg"))
demo = gr.Interface(
fn=detect,
inputs=gr.Image(label="Input", type="numpy"),
outputs=[gr.Image(label="Processed Image", type="bytes"), gr.Gallery(label="Detected Persons", type="bytes")],
examples=examples,
examples_per_page=30,
title=TITLE,
description=DESCRIPTION,
)
if __name__ == "__main__":
demo.queue(max_size=10).launch()