File size: 1,017 Bytes
3a63794
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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