File size: 1,176 Bytes
ba9f758
 
 
 
e6cc48e
ba9f758
 
 
 
 
 
 
 
 
 
e6cc48e
 
 
 
 
 
 
6293ac2
e6cc48e
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
import cv2
import numpy as np
import base64
from PIL import Image
from payload_model import PayloadModel

def convert_base64_to_cv2(base64_string: str):
    return cv2.imdecode(np.frombuffer(base64.b64decode(base64_string), np.uint8), cv2.IMREAD_COLOR)

def convert_cv2_to_pil(image: np.ndarray):
    return Image.fromarray(image).convert('RGB')

def convert_base64_to_pil(base64_string: str):
    return convert_cv2_to_pil(convert_base64_to_cv2(base64_string))

def get_images_using_bbox(payload: PayloadModel):
    images = []
    # Forcing that only a single image is received
    cv2_image = convert_base64_to_cv2(payload.input_data[0])
    print(f"Bbox: {payload.bbox}")
    images_bboxes = payload.bbox
    image_bboxes = images_bboxes[0]
    for _, bbox in enumerate(image_bboxes):
        x1, y1, x2, y2 = bbox
        image = cv2_image[y1:y2, x1:x2]
        pil_image = convert_cv2_to_pil(image)
        images.append(pil_image)
    return images

def get_whole_image(payload: PayloadModel):
    images = []
    # Forcing that only a single image is received
    pil_image = convert_base64_to_pil(payload.input_data[0])
    images.append(pil_image)
    return images