Update main.py
Browse files
main.py
CHANGED
|
@@ -7,16 +7,88 @@ os.environ["XDG_CACHE_HOME"] = os.path.join(os.getcwd(), ".cache")
|
|
| 7 |
from fastapi import FastAPI, File, UploadFile,Form
|
| 8 |
from fastapi.responses import FileResponse, StreamingResponse
|
| 9 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
from src.deoldify import device
|
| 11 |
from src.deoldify.device_id import DeviceId
|
|
|
|
| 12 |
from src.app_utils import get_model_bin
|
| 13 |
-
from colorize_image_function import colorize_image
|
| 14 |
|
| 15 |
-
app = FastAPI()
|
| 16 |
|
| 17 |
device.set(device=DeviceId.CPU)
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@app.post("/upload/")
|
| 22 |
async def upload_file(file: UploadFile = File(...)):
|
|
|
|
| 7 |
from fastapi import FastAPI, File, UploadFile,Form
|
| 8 |
from fastapi.responses import FileResponse, StreamingResponse
|
| 9 |
from fastapi.staticfiles import StaticFiles
|
| 10 |
+
import PIL
|
| 11 |
+
import cv2
|
| 12 |
+
import numpy as np
|
| 13 |
+
import uuid
|
| 14 |
+
from zipfile import ZipFile, ZIP_DEFLATED
|
| 15 |
+
from io import BytesIO
|
| 16 |
+
from random import randint
|
| 17 |
+
from datetime import datetime
|
| 18 |
+
|
| 19 |
from src.deoldify import device
|
| 20 |
from src.deoldify.device_id import DeviceId
|
| 21 |
+
from src.deoldify.visualize import *
|
| 22 |
from src.app_utils import get_model_bin
|
|
|
|
| 23 |
|
|
|
|
| 24 |
|
| 25 |
device.set(device=DeviceId.CPU)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@st.cache_resource
|
| 29 |
+
def load_model(model_dir, option):
|
| 30 |
+
if option.lower() == 'artistic':
|
| 31 |
+
model_url = 'https://data.deepai.org/deoldify/ColorizeArtistic_gen.pth'
|
| 32 |
+
get_model_bin(model_url, os.path.join(model_dir, "ColorizeArtistic_gen.pth"))
|
| 33 |
+
colorizer = get_image_colorizer(artistic=True)
|
| 34 |
+
elif option.lower() == 'stable':
|
| 35 |
+
model_url = "https://www.dropbox.com/s/usf7uifrctqw9rl/ColorizeStable_gen.pth?dl=0"
|
| 36 |
+
get_model_bin(model_url, os.path.join(model_dir, "ColorizeStable_gen.pth"))
|
| 37 |
+
colorizer = get_image_colorizer(artistic=False)
|
| 38 |
+
|
| 39 |
+
return colorizer
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def resize_img(input_img, max_size):
|
| 43 |
+
img = input_img.copy()
|
| 44 |
+
img_height, img_width = img.shape[0],img.shape[1]
|
| 45 |
+
|
| 46 |
+
if max(img_height, img_width) > max_size:
|
| 47 |
+
if img_height > img_width:
|
| 48 |
+
new_width = img_width*(max_size/img_height)
|
| 49 |
+
new_height = max_size
|
| 50 |
+
resized_img = cv2.resize(img,(int(new_width), int(new_height)))
|
| 51 |
+
return resized_img
|
| 52 |
+
|
| 53 |
+
elif img_height <= img_width:
|
| 54 |
+
new_width = img_height*(max_size/img_width)
|
| 55 |
+
new_height = max_size
|
| 56 |
+
resized_img = cv2.resize(img,(int(new_width), int(new_height)))
|
| 57 |
+
return resized_img
|
| 58 |
+
|
| 59 |
+
return img
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def colorize_image(pil_image, img_size=800) -> "PIL.Image":
|
| 63 |
+
# Open the image
|
| 64 |
+
pil_img = pil_image.convert("RGB")
|
| 65 |
+
img_rgb = np.array(pil_img)
|
| 66 |
+
resized_img_rgb = resize_img(img_rgb, img_size)
|
| 67 |
+
resized_pil_img = PIL.Image.fromarray(resized_img_rgb)
|
| 68 |
+
|
| 69 |
+
# Send the image to the model
|
| 70 |
+
output_pil_img = colorizer.plot_transformed_pil_image(resized_pil_img, render_factor=35, compare=False)
|
| 71 |
+
|
| 72 |
+
return output_pil_img
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def image_download_button(pil_image, filename: str, fmt: str, label="Download"):
|
| 76 |
+
if fmt not in ["jpg", "png"]:
|
| 77 |
+
raise Exception(f"Unknown image format (Available: {fmt} - case sensitive)")
|
| 78 |
+
|
| 79 |
+
pil_format = "JPEG" if fmt == "jpg" else "PNG"
|
| 80 |
+
file_format = "jpg" if fmt == "jpg" else "png"
|
| 81 |
+
mime = "image/jpeg" if fmt == "jpg" else "image/png"
|
| 82 |
+
|
| 83 |
+
buf = BytesIO()
|
| 84 |
+
pil_image.save(buf, format=pil_format)
|
| 85 |
+
|
| 86 |
+
return st.download_button(
|
| 87 |
+
label=label,
|
| 88 |
+
data=buf.getvalue(),
|
| 89 |
+
file_name=f'{filename}.{file_format}',
|
| 90 |
+
mime=mime,
|
| 91 |
+
)
|
| 92 |
|
| 93 |
@app.post("/upload/")
|
| 94 |
async def upload_file(file: UploadFile = File(...)):
|