Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from modelscope.pipelines import pipeline
|
| 5 |
+
from modelscope.utils.constant import Tasks
|
| 6 |
+
from modelscope.outputs import OutputKeys
|
| 7 |
+
from gradio_imageslider import ImageSlider
|
| 8 |
+
|
| 9 |
+
# Initialize the DDColor pipeline with a specified model revision
|
| 10 |
+
img_colorization = pipeline(
|
| 11 |
+
task=Tasks.image_colorization,
|
| 12 |
+
model='iic/cv_ddcolor_image-colorization',
|
| 13 |
+
model_revision='v1.02' # Specify the model revision to avoid warnings
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
def colorize_image(input_image):
|
| 17 |
+
# Convert PIL Image to NumPy array in RGB format
|
| 18 |
+
image_np = np.array(input_image.convert("RGB"))
|
| 19 |
+
|
| 20 |
+
# Perform colorization
|
| 21 |
+
result = img_colorization(image_np)
|
| 22 |
+
|
| 23 |
+
# Extract the output image
|
| 24 |
+
output_image = result[OutputKeys.OUTPUT_IMG]
|
| 25 |
+
|
| 26 |
+
# Convert NumPy array back to PIL Image
|
| 27 |
+
output_pil = Image.fromarray(output_image.astype(np.uint8))
|
| 28 |
+
|
| 29 |
+
return (input_image, output_pil)
|
| 30 |
+
|
| 31 |
+
# Define the Gradio interface
|
| 32 |
+
demo = gr.Interface(
|
| 33 |
+
fn=colorize_image,
|
| 34 |
+
inputs=gr.Image(label="Upload Grayscale Image"),
|
| 35 |
+
outputs=ImageSlider(label="Colorization Result"),
|
| 36 |
+
title="DDColor Image Colorization",
|
| 37 |
+
description="Upload a black and white image to generate its colorized version using the DDColor model.",
|
| 38 |
+
examples=["./input.jpg"]
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
demo.launch()
|