Spaces:
Sleeping
Sleeping
File size: 1,445 Bytes
553c308 8b01f23 553c308 d49628e 553c308 eb90129 553c308 eb90129 553c308 |
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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:
buffer = BytesIO()
img.save(buffer, format)
buffer.seek(0)
return buffer
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 |