Update api_run.py
Browse files- api_run.py +23 -26
api_run.py
CHANGED
|
@@ -1,38 +1,35 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi.responses import StreamingResponse
|
| 3 |
from deoldify import device
|
| 4 |
from deoldify.device_id import DeviceId
|
| 5 |
from deoldify.visualize import get_image_colorizer
|
| 6 |
-
import
|
| 7 |
-
import os
|
| 8 |
-
from tempfile import NamedTemporaryFile
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
device.set(device=DeviceId.GPU0)
|
| 12 |
|
|
|
|
| 13 |
colorizer = get_image_colorizer(artistic=True)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
async def colorize(file: UploadFile = File(...), render_factor: int = Form(35)):
|
| 19 |
-
# Lưu file upload tạm
|
| 20 |
-
suffix = os.path.splitext(file.filename)[1]
|
| 21 |
-
with NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
|
| 22 |
-
tmp_name = tmp.name
|
| 23 |
-
content = await file.read()
|
| 24 |
-
tmp.write(content)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
result_path = colorizer.plot_transformed_image(
|
| 28 |
-
path=
|
| 29 |
render_factor=render_factor,
|
| 30 |
-
display_render_factor=False,
|
| 31 |
-
figsize=(8,8),
|
| 32 |
watermarked=False
|
| 33 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
with open(result_path, "rb") as f:
|
| 37 |
-
img_bytes = f.read()
|
| 38 |
-
return StreamingResponse(io.BytesIO(img_bytes), media_type="image/png")
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
from deoldify import device
|
| 3 |
from deoldify.device_id import DeviceId
|
| 4 |
from deoldify.visualize import get_image_colorizer
|
| 5 |
+
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
device.set(device=DeviceId.CPU) # Dùng CPU cho an toàn
|
| 8 |
colorizer = get_image_colorizer(artistic=True)
|
| 9 |
|
| 10 |
+
def colorize_image(image, render_factor=35):
|
| 11 |
+
# Lưu tạm
|
| 12 |
+
image.save("temp_input.jpg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Tô màu
|
| 15 |
result_path = colorizer.plot_transformed_image(
|
| 16 |
+
path="temp_input.jpg",
|
| 17 |
render_factor=render_factor,
|
|
|
|
|
|
|
| 18 |
watermarked=False
|
| 19 |
)
|
| 20 |
+
|
| 21 |
+
return Image.open(result_path)
|
| 22 |
+
|
| 23 |
+
# Tạo giao diện Gradio
|
| 24 |
+
demo = gr.Interface(
|
| 25 |
+
fn=colorize_image,
|
| 26 |
+
inputs=[
|
| 27 |
+
gr.Image(type="pil", label="Ảnh cần tô màu"),
|
| 28 |
+
gr.Slider(10, 45, value=35, label="Render Factor")
|
| 29 |
+
],
|
| 30 |
+
outputs=gr.Image(type="pil", label="Ảnh đã tô màu"),
|
| 31 |
+
title="DeOldify - Tô màu ảnh cũ",
|
| 32 |
+
description="Upload ảnh đen trắng để tô màu"
|
| 33 |
+
)
|
| 34 |
|
| 35 |
+
demo.launch()
|
|
|
|
|
|
|
|
|