Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import gradio as gr
|
|
| 9 |
import numpy as np
|
| 10 |
from PIL import Image, ImageEnhance, ImageFilter
|
| 11 |
|
| 12 |
-
# Load the model
|
| 13 |
img_colorization = pipeline(Tasks.image_colorization, model='iic/cv_ddcolor_image-colorization')
|
| 14 |
|
| 15 |
def colorize_image(img_path):
|
|
@@ -23,10 +23,11 @@ def colorize_image(img_path):
|
|
| 23 |
|
| 24 |
def enhance_image(img_path, brightness=1.0, contrast=1.0, edge_enhance=False):
|
| 25 |
image = Image.open(img_path)
|
| 26 |
-
|
| 27 |
-
image =
|
| 28 |
-
|
| 29 |
-
image =
|
|
|
|
| 30 |
if edge_enhance:
|
| 31 |
image = image.filter(ImageFilter.EDGE_ENHANCE)
|
| 32 |
temp_dir = tempfile.mkdtemp()
|
|
@@ -35,15 +36,18 @@ def enhance_image(img_path, brightness=1.0, contrast=1.0, edge_enhance=False):
|
|
| 35 |
return enhanced_path
|
| 36 |
|
| 37 |
def process_image(img_path, brightness, contrast, edge_enhance, output_format):
|
|
|
|
| 38 |
colorized_path = colorize_image(img_path)
|
|
|
|
| 39 |
enhanced_path = enhance_image(colorized_path, brightness, contrast, edge_enhance)
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
temp_dir = tempfile.mkdtemp()
|
| 43 |
-
|
| 44 |
-
output_path = os.path.join(temp_dir,
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
title = "🌈 Color Restorization Model"
|
| 49 |
description = "Upload a black & white photo to restore it in color using a deep learning model."
|
|
@@ -55,19 +59,27 @@ with gr.Blocks(title=title) as demo:
|
|
| 55 |
with gr.Row():
|
| 56 |
with gr.Column():
|
| 57 |
input_image = gr.Image(type="filepath", label="Upload B&W Image", tool="editor")
|
| 58 |
-
brightness_slider = gr.Slider(0.5, 2.0, value=1.0, label="Brightness"
|
| 59 |
-
contrast_slider = gr.Slider(0.5, 2.0, value=1.0, label="Contrast"
|
| 60 |
-
edge_enhance_checkbox = gr.Checkbox(label="Apply Edge Enhancement"
|
| 61 |
-
output_format_dropdown = gr.Dropdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
submit_btn = gr.Button("Colorize")
|
| 63 |
with gr.Column():
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
download_btn = gr.File(label="Download Colorized Image")
|
| 66 |
|
| 67 |
submit_btn.click(
|
| 68 |
fn=process_image,
|
| 69 |
inputs=[input_image, brightness_slider, contrast_slider, edge_enhance_checkbox, output_format_dropdown],
|
| 70 |
-
outputs=[
|
| 71 |
)
|
| 72 |
|
| 73 |
demo.launch(enable_queue=True)
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
from PIL import Image, ImageEnhance, ImageFilter
|
| 11 |
|
| 12 |
+
# Load the model once at startup for efficiency
|
| 13 |
img_colorization = pipeline(Tasks.image_colorization, model='iic/cv_ddcolor_image-colorization')
|
| 14 |
|
| 15 |
def colorize_image(img_path):
|
|
|
|
| 23 |
|
| 24 |
def enhance_image(img_path, brightness=1.0, contrast=1.0, edge_enhance=False):
|
| 25 |
image = Image.open(img_path)
|
| 26 |
+
# Adjust brightness
|
| 27 |
+
image = ImageEnhance.Brightness(image).enhance(brightness)
|
| 28 |
+
# Adjust contrast
|
| 29 |
+
image = ImageEnhance.Contrast(image).enhance(contrast)
|
| 30 |
+
# Optionally apply edge enhancement
|
| 31 |
if edge_enhance:
|
| 32 |
image = image.filter(ImageFilter.EDGE_ENHANCE)
|
| 33 |
temp_dir = tempfile.mkdtemp()
|
|
|
|
| 36 |
return enhanced_path
|
| 37 |
|
| 38 |
def process_image(img_path, brightness, contrast, edge_enhance, output_format):
|
| 39 |
+
# Step 1: colorize
|
| 40 |
colorized_path = colorize_image(img_path)
|
| 41 |
+
# Step 2: enhance (brightness / contrast / edge)
|
| 42 |
enhanced_path = enhance_image(colorized_path, brightness, contrast, edge_enhance)
|
| 43 |
+
# Step 3: convert to chosen format
|
| 44 |
+
img = Image.open(enhanced_path)
|
| 45 |
temp_dir = tempfile.mkdtemp()
|
| 46 |
+
filename = f'colorized_image.{output_format.lower()}'
|
| 47 |
+
output_path = os.path.join(temp_dir, filename)
|
| 48 |
+
img.save(output_path, format=output_format.upper())
|
| 49 |
+
# Return side-by-side (original, enhanced) plus the single downloadable file
|
| 50 |
+
return ([img_path, enhanced_path], output_path)
|
| 51 |
|
| 52 |
title = "🌈 Color Restorization Model"
|
| 53 |
description = "Upload a black & white photo to restore it in color using a deep learning model."
|
|
|
|
| 59 |
with gr.Row():
|
| 60 |
with gr.Column():
|
| 61 |
input_image = gr.Image(type="filepath", label="Upload B&W Image", tool="editor")
|
| 62 |
+
brightness_slider = gr.Slider(0.5, 2.0, value=1.0, label="Brightness")
|
| 63 |
+
contrast_slider = gr.Slider(0.5, 2.0, value=1.0, label="Contrast")
|
| 64 |
+
edge_enhance_checkbox = gr.Checkbox(label="Apply Edge Enhancement")
|
| 65 |
+
output_format_dropdown = gr.Dropdown(
|
| 66 |
+
choices=["PNG", "JPEG", "TIFF"],
|
| 67 |
+
value="PNG",
|
| 68 |
+
label="Output Format"
|
| 69 |
+
)
|
| 70 |
submit_btn = gr.Button("Colorize")
|
| 71 |
with gr.Column():
|
| 72 |
+
comparison_gallery = gr.Gallery(
|
| 73 |
+
label="Original vs Colorized",
|
| 74 |
+
columns=2,
|
| 75 |
+
height="auto"
|
| 76 |
+
)
|
| 77 |
download_btn = gr.File(label="Download Colorized Image")
|
| 78 |
|
| 79 |
submit_btn.click(
|
| 80 |
fn=process_image,
|
| 81 |
inputs=[input_image, brightness_slider, contrast_slider, edge_enhance_checkbox, output_format_dropdown],
|
| 82 |
+
outputs=[comparison_gallery, download_btn]
|
| 83 |
)
|
| 84 |
|
| 85 |
demo.launch(enable_queue=True)
|