Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,12 +3,11 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
#
|
| 7 |
proto_path = "colorization_deploy_v2.prototxt"
|
| 8 |
model_path = "colorization_release_v2.caffemodel"
|
| 9 |
pts_path = "pts_in_hull.npy"
|
| 10 |
|
| 11 |
-
# ✅ Load DNN colorization model
|
| 12 |
net = cv2.dnn.readNetFromCaffe(proto_path, model_path)
|
| 13 |
pts = np.load(pts_path)
|
| 14 |
pts = pts.transpose().reshape(2, 313, 1, 1).astype(np.float32)
|
|
@@ -23,33 +22,41 @@ def colorize(img_bgr):
|
|
| 23 |
L_rs = cv2.resize(L, (224, 224))
|
| 24 |
L_rs -= 50
|
| 25 |
net.setInput(cv2.dnn.blobFromImage(L_rs))
|
| 26 |
-
ab_dec = net.forward()[0
|
| 27 |
ab_dec_us = cv2.resize(ab_dec, (w, h))
|
| 28 |
lab_output = np.concatenate((L[:, :, np.newaxis], ab_dec_us), axis=2)
|
| 29 |
bgr_output = cv2.cvtColor(lab_output.astype("uint8"), cv2.COLOR_LAB2BGR)
|
| 30 |
return bgr_output
|
| 31 |
|
| 32 |
-
def restore(img,
|
| 33 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 34 |
-
if
|
| 35 |
img = cv2.medianBlur(img, 3)
|
| 36 |
-
if
|
| 37 |
blur = cv2.GaussianBlur(img, (0, 0), 3)
|
| 38 |
img = cv2.addWeighted(img, 1.5, blur, -0.5, 0)
|
| 39 |
-
if
|
| 40 |
img = colorize(img)
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
with gr.Blocks(title="AI Old Photo Restorer") as demo:
|
| 44 |
-
gr.Markdown("## 🧓📸 AI Old Photo Restorer")
|
| 45 |
with gr.Row():
|
| 46 |
-
with gr.Column():
|
| 47 |
-
inp = gr.Image(label="Upload Photo", type="numpy")
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
btn = gr.Button("Restore")
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
demo.launch()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Load pretrained models
|
| 7 |
proto_path = "colorization_deploy_v2.prototxt"
|
| 8 |
model_path = "colorization_release_v2.caffemodel"
|
| 9 |
pts_path = "pts_in_hull.npy"
|
| 10 |
|
|
|
|
| 11 |
net = cv2.dnn.readNetFromCaffe(proto_path, model_path)
|
| 12 |
pts = np.load(pts_path)
|
| 13 |
pts = pts.transpose().reshape(2, 313, 1, 1).astype(np.float32)
|
|
|
|
| 22 |
L_rs = cv2.resize(L, (224, 224))
|
| 23 |
L_rs -= 50
|
| 24 |
net.setInput(cv2.dnn.blobFromImage(L_rs))
|
| 25 |
+
ab_dec = net.forward()[0].transpose((1, 2, 0))
|
| 26 |
ab_dec_us = cv2.resize(ab_dec, (w, h))
|
| 27 |
lab_output = np.concatenate((L[:, :, np.newaxis], ab_dec_us), axis=2)
|
| 28 |
bgr_output = cv2.cvtColor(lab_output.astype("uint8"), cv2.COLOR_LAB2BGR)
|
| 29 |
return bgr_output
|
| 30 |
|
| 31 |
+
def restore(img, enhance_face, remove_scratches, apply_colorization):
|
| 32 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 33 |
+
if remove_scratches:
|
| 34 |
img = cv2.medianBlur(img, 3)
|
| 35 |
+
if enhance_face:
|
| 36 |
blur = cv2.GaussianBlur(img, (0, 0), 3)
|
| 37 |
img = cv2.addWeighted(img, 1.5, blur, -0.5, 0)
|
| 38 |
+
if apply_colorization:
|
| 39 |
img = colorize(img)
|
| 40 |
+
restored = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 41 |
+
cv2.imwrite("restored_output.png", restored)
|
| 42 |
+
return restored, "restored_output.png"
|
| 43 |
+
|
| 44 |
+
with gr.Blocks(title="AI Old Photo Restorer", theme=gr.themes.Soft()) as demo:
|
| 45 |
+
gr.Markdown("## 🧓 AI Old Photo Restorer")
|
| 46 |
+
gr.Markdown("Give new life to your old, damaged, or black-and-white photos with facial enhancement, scratch removal, and colorization!")
|
| 47 |
|
|
|
|
|
|
|
| 48 |
with gr.Row():
|
| 49 |
+
with gr.Column(scale=1):
|
| 50 |
+
inp = gr.Image(label="📤 Upload Old Photo", type="numpy", image_mode='RGB')
|
| 51 |
+
enhance_face = gr.Checkbox(label="✨ Enhance Facial Details", value=True)
|
| 52 |
+
remove_scratches = gr.Checkbox(label="🩹 Remove Scratches", value=True)
|
| 53 |
+
apply_colorization = gr.Checkbox(label="🎨 Colorize Black & White Photo", value=True)
|
| 54 |
+
btn = gr.Button("🔁 Restore Photo", variant="primary")
|
| 55 |
+
clear_btn = gr.ClearButton([inp, enhance_face, remove_scratches, apply_colorization])
|
| 56 |
+
with gr.Column(scale=1):
|
| 57 |
+
out_image = gr.Image(label="✅ Restored Output", interactive=False)
|
| 58 |
+
download = gr.File(label="⬇️ Download Restored Image")
|
| 59 |
+
|
| 60 |
+
btn.click(restore, [inp, enhance_face, remove_scratches, apply_colorization], [out_image, download])
|
| 61 |
|
| 62 |
demo.launch()
|