Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from pathlib import Path
|
8 |
+
import gradio as gr
|
9 |
+
import numpy as np
|
10 |
+
from PIL import Image, ImageEnhance
|
11 |
+
|
12 |
+
# Load the model into memory to make running multiple predictions efficient
|
13 |
+
img_colorization = pipeline(Tasks.image_colorization, model='iic/cv_ddcolor_image-colorization')
|
14 |
+
|
15 |
+
def colorize_image(img_path):
|
16 |
+
image = cv2.imread(str(img_path))
|
17 |
+
output = img_colorization(image[..., ::-1])
|
18 |
+
result = output[OutputKeys.OUTPUT_IMG].astype(np.uint8)
|
19 |
+
temp_dir = tempfile.mkdtemp()
|
20 |
+
out_path = os.path.join(temp_dir, 'colorized.png')
|
21 |
+
cv2.imwrite(out_path, result)
|
22 |
+
return out_path
|
23 |
+
|
24 |
+
def enhance_image(img_path, brightness=1.0, contrast=1.0):
|
25 |
+
image = Image.open(img_path)
|
26 |
+
enhancer_brightness = ImageEnhance.Brightness(image)
|
27 |
+
image = enhancer_brightness.enhance(brightness)
|
28 |
+
enhancer_contrast = ImageEnhance.Contrast(image)
|
29 |
+
image = enhancer_contrast.enhance(contrast)
|
30 |
+
temp_dir = tempfile.mkdtemp()
|
31 |
+
enhanced_path = os.path.join(temp_dir, 'enhanced.png')
|
32 |
+
image.save(enhanced_path)
|
33 |
+
return enhanced_path
|
34 |
+
|
35 |
+
def process_single_image(img_path, brightness, contrast):
|
36 |
+
colorized_path = colorize_image(img_path)
|
37 |
+
enhanced_path = enhance_image(colorized_path, brightness, contrast)
|
38 |
+
return [img_path, enhanced_path], enhanced_path
|
39 |
+
|
40 |
+
def process_batch_images(img_paths, brightness, contrast):
|
41 |
+
results = []
|
42 |
+
for img_path in img_paths:
|
43 |
+
colorized_path = colorize_image(img_path)
|
44 |
+
enhanced_path = enhance_image(colorized_path, brightness, contrast)
|
45 |
+
results.append((enhanced_path, os.path.basename(img_path)))
|
46 |
+
return results
|
47 |
+
|
48 |
+
title = "🌈 Color Restorization Model"
|
49 |
+
description = "Upload black & white photos to restore them in color using a deep learning model."
|
50 |
+
|
51 |
+
with gr.Blocks(title=title) as demo:
|
52 |
+
gr.Markdown(f"## {title}")
|
53 |
+
gr.Markdown(description)
|
54 |
+
|
55 |
+
with gr.Tab("Single Image"):
|
56 |
+
with gr.Row():
|
57 |
+
with gr.Column():
|
58 |
+
input_image = gr.Image(type="filepath", label="Upload B&W Image")
|
59 |
+
brightness_slider = gr.Slider(0.5, 2.0, value=1.0, label="Brightness")
|
60 |
+
contrast_slider = gr.Slider(0.5, 2.0, value=1.0, label="Contrast")
|
61 |
+
submit_btn = gr.Button("Colorize")
|
62 |
+
with gr.Column():
|
63 |
+
comparison = gr.Gallery(label="Original vs Colorized").style(grid=[2], height="auto")
|
64 |
+
download_btn = gr.File(label="Download Colorized Image")
|
65 |
+
|
66 |
+
submit_btn.click(
|
67 |
+
fn=process_single_image,
|
68 |
+
inputs=[input_image, brightness_slider, contrast_slider],
|
69 |
+
outputs=[comparison, download_btn]
|
70 |
+
)
|
71 |
+
|
72 |
+
with gr.Tab("Batch Processing"):
|
73 |
+
with gr.Row():
|
74 |
+
with gr.Column():
|
75 |
+
batch_input = gr.File(file_types=["image"], file_count="multiple", label="Upload Multiple B&W Images")
|
76 |
+
batch_brightness_slider = gr.Slider(0.5, 2.0, value=1.0, label="Brightness")
|
77 |
+
batch_contrast_slider = gr.Slider(0.5, 2.0, value=1.0, label="Contrast")
|
78 |
+
batch_submit_btn = gr.Button("Colorize Batch")
|
79 |
+
with gr.Column():
|
80 |
+
batch_output = gr.Gallery(label="Batch Colorized Images").style(grid=[3], height="auto")
|
81 |
+
|
82 |
+
batch_submit_btn.click(
|
83 |
+
fn=process_batch_images,
|
84 |
+
inputs=[batch_input, batch_brightness_slider, batch_contrast_slider],
|
85 |
+
outputs=batch_output
|
86 |
+
)
|
87 |
+
|
88 |
+
demo.launch(enable_queue=True)
|