Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import cv2
|
3 |
+
import tempfile
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
import gradio as gr
|
7 |
+
import numpy as np
|
8 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
9 |
+
from modelscope.outputs import OutputKeys
|
10 |
+
from modelscope.pipelines import pipeline
|
11 |
+
from modelscope.utils.constant import Tasks
|
12 |
+
|
13 |
+
# Load the colorization model into memory once at startup
|
14 |
+
img_colorization = pipeline(
|
15 |
+
Tasks.image_colorization,
|
16 |
+
model="iic/cv_ddcolor_image-colorization"
|
17 |
+
)
|
18 |
+
|
19 |
+
def colorize_image(img_path: str) -> str:
|
20 |
+
"""
|
21 |
+
Reads a B&W image from disk, runs the colorization model,
|
22 |
+
writes the colorized result to a temp file, and returns its path.
|
23 |
+
"""
|
24 |
+
image = cv2.imread(str(img_path))
|
25 |
+
output = img_colorization(image[..., ::-1])
|
26 |
+
result = output[OutputKeys.OUTPUT_IMG].astype(np.uint8)
|
27 |
+
|
28 |
+
temp_dir = tempfile.mkdtemp()
|
29 |
+
out_path = os.path.join(temp_dir, "colorized.png")
|
30 |
+
cv2.imwrite(out_path, result)
|
31 |
+
return out_path
|
32 |
+
|
33 |
+
def enhance_image(
|
34 |
+
img_path: str,
|
35 |
+
brightness: float = 1.0,
|
36 |
+
contrast: float = 1.0,
|
37 |
+
edge_enhance: bool = False
|
38 |
+
) -> str:
|
39 |
+
"""
|
40 |
+
Opens a colorized image from disk, applies brightness, contrast,
|
41 |
+
and optional edge enhancement, saves to a temp file, and returns its path.
|
42 |
+
"""
|
43 |
+
image = Image.open(img_path)
|
44 |
+
|
45 |
+
# Adjust brightness
|
46 |
+
image = ImageEnhance.Brightness(image).enhance(brightness)
|
47 |
+
# Adjust contrast
|
48 |
+
image = ImageEnhance.Contrast(image).enhance(contrast)
|
49 |
+
# Optionally apply an edge enhancement filter
|
50 |
+
if edge_enhance:
|
51 |
+
image = image.filter(ImageFilter.EDGE_ENHANCE)
|
52 |
+
|
53 |
+
temp_dir = tempfile.mkdtemp()
|
54 |
+
enhanced_path = os.path.join(temp_dir, "enhanced.png")
|
55 |
+
image.save(enhanced_path)
|
56 |
+
return enhanced_path
|
57 |
+
|
58 |
+
def process_image(
|
59 |
+
img_path: str,
|
60 |
+
brightness: float,
|
61 |
+
contrast: float,
|
62 |
+
edge_enhance: bool,
|
63 |
+
output_format: str
|
64 |
+
):
|
65 |
+
"""
|
66 |
+
1) Colorizes the uploaded B&W image.
|
67 |
+
2) Applies the chosen brightness/contrast/edge-enhancement.
|
68 |
+
3) Re‐saves in the user’s chosen format (PNG/JPEG/TIFF).
|
69 |
+
Returns:
|
70 |
+
- A list [original_path, final_path] for side-by-side display.
|
71 |
+
- The final image’s file path for download.
|
72 |
+
"""
|
73 |
+
# Step 1: colorize
|
74 |
+
colorized_path = colorize_image(img_path)
|
75 |
+
# Step 2: enhancement
|
76 |
+
enhanced_path = enhance_image(colorized_path, brightness, contrast, edge_enhance)
|
77 |
+
# Step 3: convert to chosen format
|
78 |
+
img = Image.open(enhanced_path)
|
79 |
+
temp_dir = tempfile.mkdtemp()
|
80 |
+
filename = f"colorized_image.{output_format.lower()}"
|
81 |
+
output_path = os.path.join(temp_dir, filename)
|
82 |
+
img.save(output_path, format=output_format.upper())
|
83 |
+
|
84 |
+
# Return ([original, enhanced], download_path)
|
85 |
+
return ([img_path, enhanced_path], output_path)
|
86 |
+
|
87 |
+
# Title and description shown at the top of the interface
|
88 |
+
TITLE = "🌈 Color Restorization Model"
|
89 |
+
DESCRIPTION = "Upload a black & white photo to restore it in color using a deep learning model."
|
90 |
+
|
91 |
+
# Build the Gradio Blocks interface
|
92 |
+
with gr.Blocks(title=TITLE) as app:
|
93 |
+
gr.Markdown(f"## {TITLE}")
|
94 |
+
gr.Markdown(DESCRIPTION)
|
95 |
+
|
96 |
+
with gr.Row():
|
97 |
+
with gr.Column():
|
98 |
+
input_image = gr.Image(
|
99 |
+
type="filepath",
|
100 |
+
label="Upload B&W Image",
|
101 |
+
tool="editor" # Enables zoom/pan on the uploaded image
|
102 |
+
)
|
103 |
+
brightness_slider = gr.Slider(
|
104 |
+
minimum=0.5, maximum=2.0, value=1.0,
|
105 |
+
label="Brightness"
|
106 |
+
)
|
107 |
+
contrast_slider = gr.Slider(
|
108 |
+
minimum=0.5, maximum=2.0, value=1.0,
|
109 |
+
label="Contrast"
|
110 |
+
)
|
111 |
+
edge_enhance_checkbox = gr.Checkbox(
|
112 |
+
label="Apply Edge Enhancement"
|
113 |
+
)
|
114 |
+
output_format_dropdown = gr.Dropdown(
|
115 |
+
choices=["PNG", "JPEG", "TIFF"],
|
116 |
+
value="PNG",
|
117 |
+
label="Output Format"
|
118 |
+
)
|
119 |
+
submit_btn = gr.Button("Colorize")
|
120 |
+
|
121 |
+
with gr.Column():
|
122 |
+
comparison_gallery = gr.Gallery(
|
123 |
+
label="Original vs Colorized",
|
124 |
+
columns=2, # two images side by side
|
125 |
+
height="auto"
|
126 |
+
)
|
127 |
+
download_btn = gr.File(label="Download Colorized Image")
|
128 |
+
|
129 |
+
submit_btn.click(
|
130 |
+
fn=process_image,
|
131 |
+
inputs=[
|
132 |
+
input_image,
|
133 |
+
brightness_slider,
|
134 |
+
contrast_slider,
|
135 |
+
edge_enhance_checkbox,
|
136 |
+
output_format_dropdown
|
137 |
+
],
|
138 |
+
outputs=[comparison_gallery, download_btn]
|
139 |
+
)
|
140 |
+
|
141 |
+
# Launch in “production” mode: bind to 0.0.0.0 on configurable port
|
142 |
+
if __name__ == "__main__":
|
143 |
+
port = int(os.environ.get("PORT", 7860))
|
144 |
+
app.queue().launch(server_name="0.0.0.0", server_port=port)
|