Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,102 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from skimage import color as skcolor
|
| 5 |
+
|
| 6 |
+
# Import Zhang full model (download logic is inside zhang_colorizer.py)
|
| 7 |
+
from zhang_colorizer import zhang_colorize
|
| 8 |
+
|
| 9 |
+
# -----------------------------
|
| 10 |
+
# TEST MODE (subtle tint)
|
| 11 |
+
# -----------------------------
|
| 12 |
+
def test_mode_colorize(pil_img: Image.Image) -> Image.Image:
|
| 13 |
+
img = pil_img.convert("RGB")
|
| 14 |
+
arr = np.array(img).astype(np.float32) / 255.0
|
| 15 |
+
tint = np.array([1.02, 1.0, 0.98], dtype=np.float32)
|
| 16 |
+
arr = np.clip(arr * tint, 0.0, 1.0)
|
| 17 |
+
arr = (arr * 255).astype(np.uint8)
|
| 18 |
+
return Image.fromarray(arr)
|
| 19 |
+
|
| 20 |
+
# -----------------------------
|
| 21 |
+
# DEOLDIFY‑LITE (CPU SAFE)
|
| 22 |
+
# -----------------------------
|
| 23 |
+
def deoldify_lite_colorize(pil_img: Image.Image) -> Image.Image:
|
| 24 |
+
img = pil_img.convert("RGB")
|
| 25 |
+
arr = np.array(img).astype(np.float32) / 255.0
|
| 26 |
+
|
| 27 |
+
lab = skcolor.rgb2lab(arr)
|
| 28 |
+
L = lab[:, :, 0]
|
| 29 |
+
a = lab[:, :, 1]
|
| 30 |
+
b = lab[:, :, 2]
|
| 31 |
+
|
| 32 |
+
a *= 1.35
|
| 33 |
+
b *= 1.35
|
| 34 |
+
a += 2.0
|
| 35 |
+
b += 1.0
|
| 36 |
+
|
| 37 |
+
lab_out = np.stack([L, a, b], axis=-1)
|
| 38 |
+
rgb_out = skcolor.lab2rgb(lab_out)
|
| 39 |
+
rgb_out = np.clip(rgb_out, 0.0, 1.0)
|
| 40 |
+
rgb_out = (rgb_out * 255).astype(np.uint8)
|
| 41 |
+
|
| 42 |
+
return Image.fromarray(rgb_out)
|
| 43 |
+
|
| 44 |
+
# -----------------------------
|
| 45 |
+
# MAIN PIPELINE
|
| 46 |
+
# -----------------------------
|
| 47 |
+
def colorize_image(input_image, mode):
|
| 48 |
+
if input_image is None:
|
| 49 |
+
return None
|
| 50 |
|
| 51 |
+
pil_img = input_image.convert("RGB")
|
| 52 |
+
|
| 53 |
+
if mode == "Test Mode (Very Subtle)":
|
| 54 |
+
return test_mode_colorize(pil_img)
|
| 55 |
+
|
| 56 |
+
if mode == "DeOldify‑Lite (Art Mode)":
|
| 57 |
+
return deoldify_lite_colorize(pil_img)
|
| 58 |
+
|
| 59 |
+
if mode == "Zhang ECCV (Realistic Strong Color)":
|
| 60 |
+
return zhang_colorize(pil_img)
|
| 61 |
+
|
| 62 |
+
return test_mode_colorize(pil_img)
|
| 63 |
+
|
| 64 |
+
# -----------------------------
|
| 65 |
+
# GRADIO UI
|
| 66 |
+
# -----------------------------
|
| 67 |
+
with gr.Blocks(title="Biker Image Colorizer – CPU (3 Modes)") as demo:
|
| 68 |
+
gr.Markdown(
|
| 69 |
+
"""
|
| 70 |
+
# Biker Image Colorizer – CPU Edition
|
| 71 |
+
**Three modes:**
|
| 72 |
+
- Test Mode (very subtle)
|
| 73 |
+
- DeOldify‑Lite (artistic strong color)
|
| 74 |
+
- Zhang ECCV (realistic strong color)
|
| 75 |
+
"""
|
| 76 |
)
|
| 77 |
|
| 78 |
+
with gr.Row():
|
| 79 |
+
with gr.Column():
|
| 80 |
+
input_image = gr.Image(type="pil", label="Input Image")
|
| 81 |
+
mode = gr.Radio(
|
| 82 |
+
choices=[
|
| 83 |
+
"Test Mode (Very Subtle)",
|
| 84 |
+
"DeOldify‑Lite (Art Mode)",
|
| 85 |
+
"Zhang ECCV (Realistic Strong Color)",
|
| 86 |
+
],
|
| 87 |
+
value="Zhang ECCV (Realistic Strong Color)",
|
| 88 |
+
label="Colorization Mode",
|
| 89 |
+
)
|
| 90 |
+
run_btn = gr.Button("Colorize", variant="primary")
|
| 91 |
+
|
| 92 |
+
with gr.Column():
|
| 93 |
+
output_image = gr.Image(type="pil", label="Output Image")
|
| 94 |
+
|
| 95 |
+
run_btn.click(
|
| 96 |
+
fn=colorize_image,
|
| 97 |
+
inputs=[input_image, mode],
|
| 98 |
+
outputs=[output_image],
|
| 99 |
)
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|