Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,55 @@
|
|
| 1 |
import os
|
| 2 |
import cv2
|
| 3 |
import tempfile
|
| 4 |
-
|
| 5 |
-
import gradio as gr
|
| 6 |
from modelscope.pipelines import pipeline
|
| 7 |
from modelscope.utils.constant import Tasks
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
global img_colorization
|
| 13 |
-
img_colorization = pipeline(
|
| 14 |
-
Tasks.image_colorization,
|
| 15 |
-
model='iic/cv_ddcolor_image-colorization',
|
| 16 |
-
model_revision='v1.0.0'
|
| 17 |
-
)
|
| 18 |
|
| 19 |
def inference(img):
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
| 29 |
-
|
| 30 |
-
# Process image
|
| 31 |
-
output = img_colorization(image[..., ::-1])
|
| 32 |
-
result = output['output_img'].astype(np.uint8)
|
| 33 |
-
|
| 34 |
-
# Save result
|
| 35 |
-
out_path = os.path.join(temp_dir, 'colorized.png')
|
| 36 |
-
cv2.imwrite(out_path, result)
|
| 37 |
-
return Path(out_path), "✅ Colorization completed successfully!"
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
with gr.Blocks(theme="
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
Transform your black & white photos into vibrant colorized versions using state-of-the-art AI!
|
| 44 |
-
|
| 45 |
-
Upload an image and watch as our deep learning model automatically adds natural colors.
|
| 46 |
-
""")
|
| 47 |
|
|
|
|
| 48 |
with gr.Row():
|
| 49 |
-
with gr.Column(
|
| 50 |
input_img = gr.Image(
|
| 51 |
-
label="
|
| 52 |
-
type="
|
| 53 |
-
|
| 54 |
-
sources=["upload"],
|
| 55 |
-
interactive=True
|
| 56 |
-
)
|
| 57 |
-
submit_btn = gr.Button("✨ Colorize Image", variant="primary")
|
| 58 |
-
clear_btn = gr.ClearButton()
|
| 59 |
-
|
| 60 |
-
with gr.Column(scale=1):
|
| 61 |
-
output_img = gr.Image(
|
| 62 |
-
label="Colorized Result",
|
| 63 |
-
type="pil",
|
| 64 |
-
height=400,
|
| 65 |
-
interactive=False
|
| 66 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
download_btn = gr.File(label="Download Result")
|
| 68 |
-
status = gr.Textbox(label="Status", interactive=False)
|
| 69 |
|
| 70 |
# Examples section
|
| 71 |
gr.Examples(
|
| 72 |
examples=[
|
| 73 |
-
["examples/
|
| 74 |
-
["examples/
|
| 75 |
-
["examples/
|
| 76 |
],
|
| 77 |
-
inputs=
|
| 78 |
-
outputs=
|
| 79 |
fn=inference,
|
| 80 |
cache_examples=True
|
| 81 |
)
|
|
@@ -84,11 +58,13 @@ with gr.Blocks(theme="soft", title="🎨 AI Color Restoration Studio") as demo:
|
|
| 84 |
submit_btn.click(
|
| 85 |
fn=inference,
|
| 86 |
inputs=[input_img],
|
| 87 |
-
outputs=[output_img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
-
|
| 90 |
-
clear_btn.add([input_img, output_img, status])
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
demo.launch(debug=True)
|
|
|
|
| 1 |
import os
|
| 2 |
import cv2
|
| 3 |
import tempfile
|
| 4 |
+
from modelscope.outputs import OutputKeys
|
|
|
|
| 5 |
from modelscope.pipelines import pipeline
|
| 6 |
from modelscope.utils.constant import Tasks
|
| 7 |
+
import numpy as np
|
| 8 |
+
import gradio as gr
|
| 9 |
|
| 10 |
+
# Load model once at startup
|
| 11 |
+
img_colorization = pipeline(Tasks.image_colorization, model='iic/cv_ddcolor_image-colorization')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def inference(img):
|
| 14 |
+
"""Process input image and return colorized output"""
|
| 15 |
+
image = cv2.imread(str(img))
|
| 16 |
+
output = img_colorization(image[..., ::-1])
|
| 17 |
+
result = output[OutputKeys.OUTPUT_IMG].astype(np.uint8)
|
| 18 |
|
| 19 |
+
# Save result to temporary directory
|
| 20 |
+
temp_dir = tempfile.mkdtemp()
|
| 21 |
+
out_path = os.path.join(temp_dir, 'colorized.png')
|
| 22 |
+
cv2.imwrite(out_path, result)
|
| 23 |
+
return Path(out_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Modern UI design
|
| 26 |
+
with gr.Blocks(theme="default", css=".container {max-width: 1000px; margin: auto;}") as demo:
|
| 27 |
+
# Header section
|
| 28 |
+
gr.Markdown("## 🎨 Image Colorization Studio\n*Transform your black-and-white images into vibrant color masterpieces*")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Input/Output layout
|
| 31 |
with gr.Row():
|
| 32 |
+
with gr.Column():
|
| 33 |
input_img = gr.Image(
|
| 34 |
+
label="Grayscale Image",
|
| 35 |
+
type="filepath",
|
| 36 |
+
elem_id="input-image"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
+
submit_btn = gr.Button("🎨 Colorize Image", variant="primary")
|
| 39 |
+
|
| 40 |
+
with gr.Column():
|
| 41 |
+
output_img = gr.Image(label="Colorized Result", elem_id="output-image")
|
| 42 |
download_btn = gr.File(label="Download Result")
|
|
|
|
| 43 |
|
| 44 |
# Examples section
|
| 45 |
gr.Examples(
|
| 46 |
examples=[
|
| 47 |
+
["examples/vintage.jpg"],
|
| 48 |
+
["examples/portrait.png"],
|
| 49 |
+
["examples/architecture.jpeg"]
|
| 50 |
],
|
| 51 |
+
inputs=input_img,
|
| 52 |
+
outputs=output_img,
|
| 53 |
fn=inference,
|
| 54 |
cache_examples=True
|
| 55 |
)
|
|
|
|
| 58 |
submit_btn.click(
|
| 59 |
fn=inference,
|
| 60 |
inputs=[input_img],
|
| 61 |
+
outputs=[output_img]
|
| 62 |
+
)
|
| 63 |
+
output_img.change(
|
| 64 |
+
fn=lambda img: gr.File.update(value=img) if img else None,
|
| 65 |
+
inputs=[output_img],
|
| 66 |
+
outputs=[download_btn]
|
| 67 |
)
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# Launch configuration
|
| 70 |
+
demo.launch(enable_queue=True)
|
|
|