import numpy as np
import cv2
from albumentations.pytorch import ToTensorV2
import albumentations as alb
import base64
def base64_to_image(base64_code):
img_data = base64.b64decode(base64_code)
img_array = np.fromstring(img_data, np.uint8)
img = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR)
image = np.array(img)
transform = alb.Compose(
[
alb.Resize(height, width),
alb.Normalize(),
ToTensorV2(),
]
)
image = transform(image=image)["image"].unsqueeze(0)
return image
def image_to_base64(image_path):
with open(image_path, "rb") as f:
base64_data = base64.b64encode(f.read())
return base64_data