Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,8 +22,6 @@ from PIL import Image
|
|
| 22 |
import cv2
|
| 23 |
import torch # used for cuda sync & empty_cache
|
| 24 |
|
| 25 |
-
import test
|
| 26 |
-
|
| 27 |
# ----------------- BASIC INFO -----------------
|
| 28 |
CHECKPOINT_URL = "https://github.com/yyang181/colormnet/releases/download/v0.1/DINOv2FeatureV6_LocalAtten_s2_154000.pth"
|
| 29 |
CHECKPOINT_LOCAL = "DINOv2FeatureV6_LocalAtten_s2_154000.pth"
|
|
@@ -38,7 +36,6 @@ DESC = """
|
|
| 38 |
- 参考:`_colormnet_tmp/ref/<视频名>/ref.png`
|
| 39 |
- 输出:`_colormnet_tmp/output/<视频名>/*.png`
|
| 40 |
- 合成视频:`_colormnet_tmp/<视频名>.mp4`
|
| 41 |
-
|
| 42 |
**English**
|
| 43 |
Upload a **B&W video** and a **reference image**, then click “Start Coloring”.
|
| 44 |
This app runs **ZeroGPU scheduling in `app.py`** and calls `test.py` **in-process**.
|
|
@@ -52,10 +49,8 @@ Temp workspace layout:
|
|
| 52 |
PAPER = """
|
| 53 |
### 论文 / Paper
|
| 54 |
**ECCV 2024 — ColorMNet: A Memory-based Deep Spatial-Temporal Feature Propagation Network for Video Colorization**
|
| 55 |
-
|
| 56 |
如果你喜欢这个项目,欢迎到 GitHub 点个 ⭐ Star:
|
| 57 |
**GitHub**: https://github.com/yyang181/colormnet
|
| 58 |
-
|
| 59 |
**BibTeX 引用 / BibTeX Citation**
|
| 60 |
```bibtex
|
| 61 |
@inproceedings{yang2024colormnet,
|
|
@@ -81,12 +76,9 @@ BADGES_HTML = """
|
|
| 81 |
# ----------------- REFERENCE FRAME GUIDE (NO CROPPING) -----------------
|
| 82 |
REF_GUIDE_MD = r"""
|
| 83 |
## 参考帧制作指南 / Reference Frame Guide
|
| 84 |
-
|
| 85 |
**目的 / Goal**
|
| 86 |
为模型提供一张与你的视频关键帧在**姿态、光照、构图**尽量接近的**彩色参考图**,用来指导整段视频的着色风格与主体颜色。
|
| 87 |
-
|
| 88 |
---
|
| 89 |
-
|
| 90 |
### 中文步骤
|
| 91 |
1. **挑帧**:从视频里挑一帧(或相近角度的照片),尽量与要着色的镜头在**姿态 / 光照 / 场景**一致。
|
| 92 |
2. **上色方式**:若你只有黑白参考图、但需要彩色参考,可用 **通义千问·图像编辑(Qwen-Image)**:
|
|
@@ -97,15 +89,12 @@ REF_GUIDE_MD = r"""
|
|
| 97 |
- 可按需多次编辑(如补充「衣服为复古蓝、肤色自然、不要锐化」)
|
| 98 |
3. **保存格式**:PNG/JPG 均可;推荐分辨率 ≥ **480px**(短边)。
|
| 99 |
4. **文件放置**:本应用会自动放置为 `ref/<视频名>/ref.png`。
|
| 100 |
-
|
| 101 |
**注意事项(Do/Don’t)**
|
| 102 |
- ✅ 主体清晰、颜色干净,不要过曝或强滤镜。
|
| 103 |
- ✅ 关键区域(衣服、皮肤、头发、天空等)颜色与目标风格一致。
|
| 104 |
- ❌ 不要更改几何结构(如人脸形状/姿态),**只修改颜色**。
|
| 105 |
- ❌ 避免文字、贴纸、重度风格化滤镜。
|
| 106 |
-
|
| 107 |
---
|
| 108 |
-
|
| 109 |
### English Steps
|
| 110 |
1. **Pick a frame** (or a similar photo) that matches the target shot in **pose / lighting / composition**.
|
| 111 |
2. **Colorizing if your reference is B&W** — use **Qwen-Image (Image Editing)**:
|
|
@@ -115,7 +104,6 @@ REF_GUIDE_MD = r"""
|
|
| 115 |
- Iterate if needed (e.g., “vintage blue jacket, natural skin tone; avoid sharpening”).
|
| 116 |
3. **Format**: PNG/JPG; recommended short side ≥ **480px**.
|
| 117 |
4. **File placement**: The app will place it as `ref/<video_stem>/ref.png`.
|
| 118 |
-
|
| 119 |
**Do / Don’t**
|
| 120 |
- ✅ Clean subject and palette; avoid overexposure/harsh filters.
|
| 121 |
- ✅ Ensure key regions (clothes/skin/hair/sky) match the intended colors.
|
|
@@ -301,25 +289,6 @@ def ensure_correlation_extension_installed():
|
|
| 301 |
|
| 302 |
# ----------------- GRADIO HANDLER -----------------
|
| 303 |
@spaces.GPU(duration=120) # 确保 CUDA 初始化在此函数体内
|
| 304 |
-
def inference_with_gpu(args_list):
|
| 305 |
-
# <<< ZeroGPU 分配后:按需安装 Pytorch-Correlation-extension >>>
|
| 306 |
-
ensure_correlation_extension_installed()
|
| 307 |
-
# --------------------------------------------------------------
|
| 308 |
-
|
| 309 |
-
buf = io.StringIO()
|
| 310 |
-
try:
|
| 311 |
-
with redirect_stdout(buf), redirect_stderr(buf):
|
| 312 |
-
entry = getattr(test, "run_cli", None)
|
| 313 |
-
if entry is None or not callable(entry):
|
| 314 |
-
raise RuntimeError("test.py 未提供可调用的 run_cli(args_list) 接口。")
|
| 315 |
-
entry(args_list)
|
| 316 |
-
log = f"Args: {' '.join(args_list)}\n\n{buf.getvalue()}"
|
| 317 |
-
except Exception as e:
|
| 318 |
-
log = f"Args: {' '.join(args_list)}\n\n{buf.getvalue()}\n\nERROR: {e}"
|
| 319 |
-
|
| 320 |
-
return log
|
| 321 |
-
|
| 322 |
-
|
| 323 |
def gradio_infer(
|
| 324 |
debug_shapes,
|
| 325 |
bw_video, ref_image,
|
|
@@ -328,7 +297,9 @@ def gradio_infer(
|
|
| 328 |
num_proto, top_k, mem_every, deep_update,
|
| 329 |
save_scores, flip, size, reverse
|
| 330 |
):
|
| 331 |
-
|
|
|
|
|
|
|
| 332 |
|
| 333 |
# 1) 基本校验与临时目录
|
| 334 |
if bw_video is None:
|
|
@@ -422,10 +393,10 @@ def gradio_infer(
|
|
| 422 |
ensure_checkpoint()
|
| 423 |
|
| 424 |
# 8) 同进程调用 test.py
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
|
| 430 |
args_list = build_args_list_for_test(
|
| 431 |
d16_batch_path=input_root, # 指向 input_video 根
|
|
@@ -434,19 +405,17 @@ def gradio_infer(
|
|
| 434 |
cfg=user_config
|
| 435 |
)
|
| 436 |
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
log = inference_with_gpu(args_list)
|
| 450 |
|
| 451 |
# 在合成 mp4 之前:清空 CUDA(防止显存占用)
|
| 452 |
try:
|
|
|
|
| 22 |
import cv2
|
| 23 |
import torch # used for cuda sync & empty_cache
|
| 24 |
|
|
|
|
|
|
|
| 25 |
# ----------------- BASIC INFO -----------------
|
| 26 |
CHECKPOINT_URL = "https://github.com/yyang181/colormnet/releases/download/v0.1/DINOv2FeatureV6_LocalAtten_s2_154000.pth"
|
| 27 |
CHECKPOINT_LOCAL = "DINOv2FeatureV6_LocalAtten_s2_154000.pth"
|
|
|
|
| 36 |
- 参考:`_colormnet_tmp/ref/<视频名>/ref.png`
|
| 37 |
- 输出:`_colormnet_tmp/output/<视频名>/*.png`
|
| 38 |
- 合成视频:`_colormnet_tmp/<视频名>.mp4`
|
|
|
|
| 39 |
**English**
|
| 40 |
Upload a **B&W video** and a **reference image**, then click “Start Coloring”.
|
| 41 |
This app runs **ZeroGPU scheduling in `app.py`** and calls `test.py` **in-process**.
|
|
|
|
| 49 |
PAPER = """
|
| 50 |
### 论文 / Paper
|
| 51 |
**ECCV 2024 — ColorMNet: A Memory-based Deep Spatial-Temporal Feature Propagation Network for Video Colorization**
|
|
|
|
| 52 |
如果你喜欢这个项目,欢迎到 GitHub 点个 ⭐ Star:
|
| 53 |
**GitHub**: https://github.com/yyang181/colormnet
|
|
|
|
| 54 |
**BibTeX 引用 / BibTeX Citation**
|
| 55 |
```bibtex
|
| 56 |
@inproceedings{yang2024colormnet,
|
|
|
|
| 76 |
# ----------------- REFERENCE FRAME GUIDE (NO CROPPING) -----------------
|
| 77 |
REF_GUIDE_MD = r"""
|
| 78 |
## 参考帧制作指南 / Reference Frame Guide
|
|
|
|
| 79 |
**目的 / Goal**
|
| 80 |
为模型提供一张与你的视频关键帧在**姿态、光照、构图**尽量接近的**彩色参考图**,用来指导整段视频的着色风格与主体颜色。
|
|
|
|
| 81 |
---
|
|
|
|
| 82 |
### 中文步骤
|
| 83 |
1. **挑帧**:从视频里挑一帧(或相近角度的照片),尽量与要着色的镜头在**姿态 / 光照 / 场景**一致。
|
| 84 |
2. **上色方式**:若你只有黑白参考图、但需要彩色参考,可用 **通义千问·图像编辑(Qwen-Image)**:
|
|
|
|
| 89 |
- 可按需多次编辑(如补充「衣服为复古蓝、肤色自然、不要锐化」)
|
| 90 |
3. **保存格式**:PNG/JPG 均可;推荐分辨率 ≥ **480px**(短边)。
|
| 91 |
4. **文件放置**:本应用会自动放置为 `ref/<视频名>/ref.png`。
|
|
|
|
| 92 |
**注意事项(Do/Don’t)**
|
| 93 |
- ✅ 主体清晰、颜色干净,不要过曝或强滤镜。
|
| 94 |
- ✅ 关键区域(衣服、皮肤、头发、天空等)颜色与目标风格一致。
|
| 95 |
- ❌ 不要更改几何结构(如人脸形状/姿态),**只修改颜色**。
|
| 96 |
- ❌ 避免文字、贴纸、重度风格化滤镜。
|
|
|
|
| 97 |
---
|
|
|
|
| 98 |
### English Steps
|
| 99 |
1. **Pick a frame** (or a similar photo) that matches the target shot in **pose / lighting / composition**.
|
| 100 |
2. **Colorizing if your reference is B&W** — use **Qwen-Image (Image Editing)**:
|
|
|
|
| 104 |
- Iterate if needed (e.g., “vintage blue jacket, natural skin tone; avoid sharpening”).
|
| 105 |
3. **Format**: PNG/JPG; recommended short side ≥ **480px**.
|
| 106 |
4. **File placement**: The app will place it as `ref/<video_stem>/ref.png`.
|
|
|
|
| 107 |
**Do / Don’t**
|
| 108 |
- ✅ Clean subject and palette; avoid overexposure/harsh filters.
|
| 109 |
- ✅ Ensure key regions (clothes/skin/hair/sky) match the intended colors.
|
|
|
|
| 289 |
|
| 290 |
# ----------------- GRADIO HANDLER -----------------
|
| 291 |
@spaces.GPU(duration=120) # 确保 CUDA 初始化在此函数体内
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
def gradio_infer(
|
| 293 |
debug_shapes,
|
| 294 |
bw_video, ref_image,
|
|
|
|
| 297 |
num_proto, top_k, mem_every, deep_update,
|
| 298 |
save_scores, flip, size, reverse
|
| 299 |
):
|
| 300 |
+
# <<< ZeroGPU 分配后:按需安装 Pytorch-Correlation-extension >>>
|
| 301 |
+
ensure_correlation_extension_installed()
|
| 302 |
+
# --------------------------------------------------------------
|
| 303 |
|
| 304 |
# 1) 基本校验与临时目录
|
| 305 |
if bw_video is None:
|
|
|
|
| 393 |
ensure_checkpoint()
|
| 394 |
|
| 395 |
# 8) 同进程调用 test.py
|
| 396 |
+
try:
|
| 397 |
+
import test # 确保 test.py 同目录且提供 run_cli(args_list)
|
| 398 |
+
except Exception as e:
|
| 399 |
+
return None, f"导入 test.py 失败 / Failed to import test.py:\n{e}"
|
| 400 |
|
| 401 |
args_list = build_args_list_for_test(
|
| 402 |
d16_batch_path=input_root, # 指向 input_video 根
|
|
|
|
| 405 |
cfg=user_config
|
| 406 |
)
|
| 407 |
|
| 408 |
+
buf = io.StringIO()
|
| 409 |
+
try:
|
| 410 |
+
with redirect_stdout(buf), redirect_stderr(buf):
|
| 411 |
+
entry = getattr(test, "run_cli", None)
|
| 412 |
+
if entry is None or not callable(entry):
|
| 413 |
+
raise RuntimeError("test.py 未提供可调用的 run_cli(args_list) 接口。")
|
| 414 |
+
entry(args_list)
|
| 415 |
+
log = f"Args: {' '.join(args_list)}\n\n{buf.getvalue()}"
|
| 416 |
+
except Exception as e:
|
| 417 |
+
log = f"Args: {' '.join(args_list)}\n\n{buf.getvalue()}\n\nERROR: {e}"
|
| 418 |
+
return None, log
|
|
|
|
|
|
|
| 419 |
|
| 420 |
# 在合成 mp4 之前:清空 CUDA(防止显存占用)
|
| 421 |
try:
|