Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, ImageFilter
|
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 apply_clahe(image):
|
16 |
+
# Convert to LAB color space
|
17 |
+
lab = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)
|
18 |
+
l, a, b = cv2.split(lab)
|
19 |
+
# Apply CLAHE to the L-channel
|
20 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
|
21 |
+
cl = clahe.apply(l)
|
22 |
+
# Merge the CLAHE enhanced L-channel back with A and B channels
|
23 |
+
limg = cv2.merge((cl,a,b))
|
24 |
+
# Convert back to RGB color space
|
25 |
+
final = cv2.cvtColor(limg, cv2.COLOR_LAB2RGB)
|
26 |
+
return final
|
27 |
+
|
28 |
+
def enhance_image_pil(image_pil, brightness=1.0, contrast=1.0):
|
29 |
+
# Apply brightness and contrast enhancements
|
30 |
+
enhancer_brightness = ImageEnhance.Brightness(image_pil)
|
31 |
+
image_pil = enhancer_brightness.enhance(brightness)
|
32 |
+
enhancer_contrast = ImageEnhance.Contrast(image_pil)
|
33 |
+
image_pil = enhancer_contrast.enhance(contrast)
|
34 |
+
# Apply edge enhancement
|
35 |
+
image_pil = image_pil.filter(ImageFilter.EDGE_ENHANCE)
|
36 |
+
return image_pil
|
37 |
+
|
38 |
+
def process_image(img_path, brightness, contrast):
|
39 |
+
# Read the input image
|
40 |
+
image = cv2.imread(str(img_path))
|
41 |
+
# Colorize the image
|
42 |
+
output = img_colorization(image[..., ::-1])
|
43 |
+
result = output[OutputKeys.OUTPUT_IMG].astype(np.uint8)
|
44 |
+
# Apply CLAHE
|
45 |
+
result_clahe = apply_clahe(result)
|
46 |
+
# Convert to PIL Image for further enhancements
|
47 |
+
result_pil = Image.fromarray(result_clahe)
|
48 |
+
# Apply brightness, contrast, and edge enhancements
|
49 |
+
enhanced_pil = enhance_image_pil(result_pil, brightness, contrast)
|
50 |
+
# Save the enhanced image to a temporary file
|
51 |
+
temp_dir = tempfile.mkdtemp()
|
52 |
+
enhanced_path = os.path.join(temp_dir, 'enhanced.png')
|
53 |
+
enhanced_pil.save(enhanced_path)
|
54 |
+
return [img_path, enhanced_path], enhanced_path
|
55 |
+
|
56 |
+
title = "🌈 Color Restorization Model"
|
57 |
+
description = "Upload a black & white photo to restore it in color using a deep learning model."
|
58 |
+
|
59 |
+
with gr.Blocks(title=title) as demo:
|
60 |
+
gr.Markdown(f"## {title}")
|
61 |
+
gr.Markdown(description)
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
with gr.Column():
|
65 |
+
input_image = gr.Image(type="filepath", label="Upload B&W Image")
|
66 |
+
brightness_slider = gr.Slider(0.5, 2.0, value=1.0, label="Brightness")
|
67 |
+
contrast_slider = gr.Slider(0.5, 2.0, value=1.0, label="Contrast")
|
68 |
+
submit_btn = gr.Button("Colorize")
|
69 |
+
with gr.Column():
|
70 |
+
comparison = gr.Gallery(label="Original vs Colorized").style(grid=[2], height="auto")
|
71 |
+
download_btn = gr.File(label="Download Colorized Image")
|
72 |
+
|
73 |
+
submit_btn.click(
|
74 |
+
fn=process_image,
|
75 |
+
inputs=[input_image, brightness_slider, contrast_slider],
|
76 |
+
outputs=[comparison, download_btn]
|
77 |
+
)
|
78 |
+
|
79 |
+
demo.launch(enable_queue=True)
|