Spaces:
Runtime error
Runtime error
from PIL import Image | |
import numpy as np | |
import cv2 as cv | |
from services.api_service import post_data | |
from controllers.utils import image_to_bytes | |
from controllers.fw import get_fw_query_params | |
def draw_bboxs(image: Image, faces: list, color: str): | |
np_image = np.array(image) | |
for face in faces: | |
bbox = face['bbox'] | |
x, y, w, h = bbox['x'], bbox['y'], bbox['width'], bbox['height'] | |
# Convert hex to rgb | |
hex = color[1:] | |
rgb = tuple(int(hex[i:i+2], 16) for i in (0, 2, 4)) | |
cv.rectangle(np_image, (x, y), (x + w, y + h), rgb, 2) | |
return np_image | |
def fd_controller(image: Image, model: str, color: str, fw_option: str = 'none'): | |
request_data = { | |
"image": image_to_bytes(image), | |
"model": model | |
} | |
response = post_data( | |
f"/api/fd/{get_fw_query_params(fw_option)}", files=request_data) | |
if response is None: | |
return None | |
faces = response['faces'] | |
image = draw_bboxs(image, faces, color) | |
return image, faces | |