aisatsu-api / utils.py
vumichien's picture
Update utils.py
beabc59
raw
history blame
1.53 kB
from io import BytesIO
import base64
from PIL import Image
import cv2
import numpy as np
from gtts import gTTS
def tts(text: str, language="ja", encode=False) -> object:
"""Converts text into autoplay html.
Args:
text (str): generated answer of bot
language (str): language of text
Returns:
html: autoplay object
"""
tts_object = gTTS(text=text, lang=language, slow=False)
if encode:
bytes_object = BytesIO()
tts_object.write_to_fp(bytes_object)
bytes_object.seek(0)
b64 = base64.b64encode(bytes_object.getvalue()).decode()
return b64
else:
tts_object.save("temp.mp3")
return "temp.mp3"
def read_image_file(file) -> Image.Image:
image = Image.open(BytesIO(file))
return image
def pil_to_base64(img, format="jpeg", encode=False):
if encode:
bytes_object = BytesIO()
img.save(bytes_object, format)
bytes_object.seek(0)
b64 = base64.b64encode(bytes_object.getvalue()).decode("ascii")
return b64
else:
temp_path = f"temp.{format}"
img.save(temp_path)
return temp_path
def base64_to_pil(img_str):
if "base64," in img_str:
img_str = img_str.split(",")[1]
img_raw = base64.b64decode(img_str)
img = Image.open(BytesIO(img_raw))
return img
def get_hist(image):
hist = cv2.calcHist([np.array(image)], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
hist = cv2.normalize(hist, hist).flatten()
return hist