Upload 10 files
Browse files- README.md +9 -30
- __pycache__/app.cpython-313.pyc +0 -0
- app.py +70 -179
- requirements.txt +1 -8
- watermark/__init__.py +2 -3
- watermark/__pycache__/__init__.cpython-313.pyc +0 -0
- watermark/__pycache__/engine.cpython-313.pyc +0 -0
- watermark/__pycache__/video.cpython-313.pyc +0 -0
- watermark/engine.py +109 -184
- watermark/video.py +44 -93
README.md
CHANGED
|
@@ -11,41 +11,20 @@ pinned: true
|
|
| 11 |
|
| 12 |
# Z-Image + Watermark Remover
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
-
##
|
| 17 |
|
| 18 |
-
``
|
| 19 |
-
.
|
| 20 |
-
├── app.py
|
| 21 |
-
├── requirements.txt
|
| 22 |
-
├── README.md
|
| 23 |
-
└── watermark/
|
| 24 |
-
├── __init__.py
|
| 25 |
-
├── engine.py
|
| 26 |
-
└── video.py
|
| 27 |
-
```
|
| 28 |
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
- 🧹 图片去水印
|
| 33 |
-
- 🎬 视频去水印
|
| 34 |
|
| 35 |
## API
|
| 36 |
|
| 37 |
-
|
| 38 |
-
- `/generate_image_prompt`
|
| 39 |
-
- `/watermark_preview`
|
| 40 |
-
- `/remove_image`
|
| 41 |
-
- `/remove_video`
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
## Notes
|
| 46 |
-
|
| 47 |
-
The Space is designed for ZeroGPU. Z-Image, Florence-2 and LaMa are placed at module scope to follow the current ZeroGPU model-loading guidance. GPU functions are explicitly decorated with `@spaces.GPU`.
|
| 48 |
-
|
| 49 |
-
Video processing uses two-pass sparse detection and LaMa inpainting, then attempts to restore the original audio with ffmpeg.
|
| 50 |
-
|
| 51 |
-
Use only on content you own or are authorized to edit.
|
|
|
|
| 11 |
|
| 12 |
# Z-Image + Watermark Remover
|
| 13 |
|
| 14 |
+
ZeroGPU Gradio Space combining Z-Image-Turbo, Florence-2 detection and LaMa inpainting.
|
| 15 |
|
| 16 |
+
## V2 cache fix
|
| 17 |
|
| 18 |
+
The app no longer assumes `/data` is writable. Cache priority:
|
| 19 |
+
`WATERMARK_CACHE_DIR` -> `/data/.cache/watermark` if writable -> `/tmp/watermark` -> `~/.cache/watermark`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
If a writable HF Storage Bucket is mounted at `/data`, optionally set:
|
| 22 |
+
`WATERMARK_CACHE_DIR=/data/.cache/watermark`.
|
| 23 |
|
| 24 |
+
Add `HF_TOKEN` under Space Settings -> Variables and secrets for authenticated Hub access.
|
|
|
|
|
|
|
| 25 |
|
| 26 |
## API
|
| 27 |
|
| 28 |
+
`generate_image`, `generate_image_prompt`, `watermark_preview`, `remove_image`, `remove_video`.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
Test video with short clips first.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__pycache__/app.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-313.pyc and b/__pycache__/app.cpython-313.pyc differ
|
|
|
app.py
CHANGED
|
@@ -1,14 +1,8 @@
|
|
| 1 |
-
import
|
| 2 |
-
import tempfile
|
| 3 |
-
|
| 4 |
import gradio as gr
|
| 5 |
import spaces
|
| 6 |
-
import torch
|
| 7 |
from diffusers import DiffusionPipeline
|
| 8 |
|
| 9 |
-
# =========================
|
| 10 |
-
# Z-Image-Turbo (existing)
|
| 11 |
-
# =========================
|
| 12 |
print("Loading Z-Image-Turbo pipeline...")
|
| 13 |
pipe = DiffusionPipeline.from_pretrained(
|
| 14 |
"Tongyi-MAI/Z-Image-Turbo",
|
|
@@ -18,202 +12,99 @@ pipe = DiffusionPipeline.from_pretrained(
|
|
| 18 |
pipe.to("cuda")
|
| 19 |
print("Z-Image pipeline loaded!")
|
| 20 |
|
|
|
|
| 21 |
|
| 22 |
-
@spaces.GPU(duration=
|
| 23 |
-
def generate_image(prompt, height, width,
|
|
|
|
| 24 |
if randomize_seed:
|
| 25 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 26 |
-
|
|
|
|
| 27 |
image = pipe(
|
| 28 |
-
prompt=prompt,
|
| 29 |
-
|
| 30 |
-
width=int(width),
|
| 31 |
-
num_inference_steps=int(num_inference_steps),
|
| 32 |
-
guidance_scale=0.0,
|
| 33 |
generator=generator,
|
| 34 |
).images[0]
|
| 35 |
return image, seed
|
| 36 |
|
| 37 |
-
|
| 38 |
-
# =========================
|
| 39 |
-
# Watermark Remover
|
| 40 |
-
# =========================
|
| 41 |
-
# IMPORTANT: importing this module loads Florence-2 + LaMa at module scope,
|
| 42 |
-
# matching current ZeroGPU guidance for model placement.
|
| 43 |
-
from watermark import detect_only, preview, remove_image, process_video
|
| 44 |
-
|
| 45 |
-
|
| 46 |
@spaces.GPU(duration=45)
|
| 47 |
-
def wm_preview(image, detection_prompt, max_bbox_percent):
|
| 48 |
return preview(image, float(max_bbox_percent), detection_prompt or "watermark")
|
| 49 |
|
| 50 |
-
|
| 51 |
@spaces.GPU(duration=60)
|
| 52 |
-
def wm_remove_image(image, detection_prompt,
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
detection_prompt or "watermark",
|
| 57 |
-
bool(transparent),
|
| 58 |
-
)
|
| 59 |
-
return result, mask
|
| 60 |
-
|
| 61 |
|
| 62 |
@spaces.GPU(duration=120)
|
| 63 |
-
def wm_remove_video(video_path, detection_prompt
|
|
|
|
|
|
|
| 64 |
if not video_path:
|
| 65 |
-
raise gr.Error("Please upload a video
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
float(fade_in),
|
| 74 |
-
float(fade_out),
|
| 75 |
-
progress,
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
examples = [
|
| 80 |
-
["Young Chinese woman in red Hanfu, intricate embroidery. Impeccable makeup, red floral forehead pattern. Elaborate high bun, golden phoenix headdress, red flowers, beads. Holds round folding fan with lady, trees, bird. Neon lightning-bolt lamp, bright yellow glow, above extended left palm. Soft-lit outdoor night background, silhouetted tiered pagoda, blurred colorful distant lights."],
|
| 81 |
-
["A majestic dragon soaring through clouds at sunset, scales shimmering with iridescent colors, detailed fantasy art style"],
|
| 82 |
-
["Cozy coffee shop interior, warm lighting, rain on windows, plants on shelves, vintage aesthetic, photorealistic"],
|
| 83 |
-
]
|
| 84 |
-
|
| 85 |
-
custom_theme = gr.themes.Soft(
|
| 86 |
-
primary_hue="yellow",
|
| 87 |
-
secondary_hue="amber",
|
| 88 |
-
neutral_hue="slate",
|
| 89 |
-
font=gr.themes.GoogleFont("Inter"),
|
| 90 |
-
text_size="lg",
|
| 91 |
-
spacing_size="md",
|
| 92 |
-
radius_size="lg",
|
| 93 |
-
).set(
|
| 94 |
-
button_primary_background_fill="*primary_500",
|
| 95 |
-
button_primary_background_fill_hover="*primary_600",
|
| 96 |
-
block_title_text_weight="600",
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
with gr.Blocks(fill_height=True, title="AI Image Studio") as demo:
|
| 100 |
-
gr.Markdown("# 🎨 AI Image Studio\n**Z-Image-Turbo + AI Watermark Remover · ZeroGPU**")
|
| 101 |
-
|
| 102 |
with gr.Tabs():
|
| 103 |
-
# -------------------------
|
| 104 |
-
# Tab 1: original Z-Image
|
| 105 |
-
# -------------------------
|
| 106 |
with gr.Tab("🎨 Z-Image"):
|
| 107 |
-
with gr.Row(
|
| 108 |
-
with gr.Column(
|
| 109 |
-
prompt = gr.Textbox(
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
lambda randomize: gr.Number(visible=not randomize),
|
| 126 |
-
inputs=randomize_seed,
|
| 127 |
-
outputs=seed,
|
| 128 |
-
)
|
| 129 |
-
generate_btn = gr.Button("🚀 Generate Image", variant="primary", size="lg")
|
| 130 |
-
gr.Examples(examples=examples, inputs=prompt, label="💡 Try these prompts", examples_per_page=3)
|
| 131 |
-
|
| 132 |
-
with gr.Column(scale=1, min_width=320):
|
| 133 |
-
output_image = gr.Image(label="Generated Image", type="pil", format="png", height=600, show_label=False)
|
| 134 |
-
used_seed = gr.Number(label="🎲 Seed Used", interactive=False)
|
| 135 |
-
|
| 136 |
-
generate_btn.click(
|
| 137 |
-
generate_image,
|
| 138 |
-
inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed],
|
| 139 |
-
outputs=[output_image, used_seed],
|
| 140 |
-
api_name="generate_image",
|
| 141 |
-
)
|
| 142 |
-
prompt.submit(
|
| 143 |
-
generate_image,
|
| 144 |
-
inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed],
|
| 145 |
-
outputs=[output_image, used_seed],
|
| 146 |
-
api_name="generate_image_prompt",
|
| 147 |
-
)
|
| 148 |
|
| 149 |
-
# -------------------------
|
| 150 |
-
# Tab 2: image watermark
|
| 151 |
-
# -------------------------
|
| 152 |
with gr.Tab("🧹 图片去水印"):
|
| 153 |
with gr.Row():
|
| 154 |
with gr.Column():
|
| 155 |
-
|
| 156 |
-
wm_prompt = gr.Textbox(value="watermark", label="
|
| 157 |
-
wm_max = gr.Slider(1,
|
| 158 |
-
wm_transparent = gr.Checkbox(False, label="
|
| 159 |
with gr.Row():
|
| 160 |
-
|
| 161 |
-
|
| 162 |
with gr.Column():
|
| 163 |
-
wm_preview_out = gr.Image(
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
outputs=[wm_preview_out, wm_info, wm_mask_out],
|
| 172 |
-
api_name="watermark_preview",
|
| 173 |
-
)
|
| 174 |
-
wm_remove_btn.click(
|
| 175 |
-
wm_remove_image,
|
| 176 |
-
inputs=[wm_image_in, wm_prompt, wm_max, wm_transparent],
|
| 177 |
-
outputs=[wm_result_out, wm_mask_out],
|
| 178 |
-
api_name="remove_image",
|
| 179 |
-
)
|
| 180 |
|
| 181 |
-
# -------------------------
|
| 182 |
-
# Tab 3: video watermark
|
| 183 |
-
# -------------------------
|
| 184 |
with gr.Tab("🎬 视频去水印"):
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
inputs=[video_in, video_prompt, video_max, video_skip, fade_in, fade_out],
|
| 198 |
-
outputs=video_out,
|
| 199 |
-
api_name="remove_video",
|
| 200 |
-
)
|
| 201 |
-
|
| 202 |
-
gr.Markdown(
|
| 203 |
-
"---\n"
|
| 204 |
-
"**Models:** Tongyi-MAI/Z-Image-Turbo · florence-community/Florence-2-large · LaMa\n\n"
|
| 205 |
-
"Use only on content you own or are authorized to edit."
|
| 206 |
-
)
|
| 207 |
-
|
| 208 |
|
| 209 |
if __name__ == "__main__":
|
| 210 |
-
demo.launch(
|
| 211 |
-
theme=custom_theme,
|
| 212 |
-
css="""
|
| 213 |
-
.gradio-container { max-width: 1400px !important; margin: 0 auto !important; }
|
| 214 |
-
.header-text h1 { font-size: 2.5rem !important; }
|
| 215 |
-
@media (max-width: 768px) { .header-text h1 { font-size: 1.8rem !important; } }
|
| 216 |
-
""",
|
| 217 |
-
footer_links=["api", "gradio"],
|
| 218 |
-
mcp_server=True,
|
| 219 |
-
)
|
|
|
|
| 1 |
+
import torch
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import spaces
|
|
|
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
print("Loading Z-Image-Turbo pipeline...")
|
| 7 |
pipe = DiffusionPipeline.from_pretrained(
|
| 8 |
"Tongyi-MAI/Z-Image-Turbo",
|
|
|
|
| 12 |
pipe.to("cuda")
|
| 13 |
print("Z-Image pipeline loaded!")
|
| 14 |
|
| 15 |
+
from watermark import preview, remove_image, process_video
|
| 16 |
|
| 17 |
+
@spaces.GPU(duration=60)
|
| 18 |
+
def generate_image(prompt, height, width, steps, seed, randomize_seed,
|
| 19 |
+
progress=gr.Progress(track_tqdm=True)):
|
| 20 |
if randomize_seed:
|
| 21 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 22 |
+
seed = int(seed)
|
| 23 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
| 24 |
image = pipe(
|
| 25 |
+
prompt=prompt, height=int(height), width=int(width),
|
| 26 |
+
num_inference_steps=int(steps), guidance_scale=0.0,
|
|
|
|
|
|
|
|
|
|
| 27 |
generator=generator,
|
| 28 |
).images[0]
|
| 29 |
return image, seed
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
@spaces.GPU(duration=45)
|
| 32 |
+
def wm_preview(image, detection_prompt="watermark", max_bbox_percent=30):
|
| 33 |
return preview(image, float(max_bbox_percent), detection_prompt or "watermark")
|
| 34 |
|
|
|
|
| 35 |
@spaces.GPU(duration=60)
|
| 36 |
+
def wm_remove_image(image, detection_prompt="watermark",
|
| 37 |
+
max_bbox_percent=30, transparent=False):
|
| 38 |
+
return remove_image(image, float(max_bbox_percent),
|
| 39 |
+
detection_prompt or "watermark", bool(transparent))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
@spaces.GPU(duration=120)
|
| 42 |
+
def wm_remove_video(video_path, detection_prompt="watermark",
|
| 43 |
+
max_bbox_percent=30, detection_skip=3,
|
| 44 |
+
fade_in=2, fade_out=2, progress=gr.Progress()):
|
| 45 |
if not video_path:
|
| 46 |
+
raise gr.Error("Please upload a video.")
|
| 47 |
+
return process_video(video_path, detection_prompt or "watermark",
|
| 48 |
+
float(max_bbox_percent), max(1, int(detection_skip)),
|
| 49 |
+
max(0, int(fade_in)), max(0, int(fade_out)),
|
| 50 |
+
progress=progress)
|
| 51 |
+
|
| 52 |
+
with gr.Blocks(title="Z-Image + Watermark Remover") as demo:
|
| 53 |
+
gr.Markdown("# 🎨 Z-Image + Watermark Remover")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
with gr.Tabs():
|
|
|
|
|
|
|
|
|
|
| 55 |
with gr.Tab("🎨 Z-Image"):
|
| 56 |
+
with gr.Row():
|
| 57 |
+
with gr.Column():
|
| 58 |
+
prompt = gr.Textbox(label="Prompt", lines=5)
|
| 59 |
+
height = gr.Slider(512, 2048, 1024, 64, label="Height")
|
| 60 |
+
width = gr.Slider(512, 2048, 1024, 64, label="Width")
|
| 61 |
+
steps = gr.Slider(1, 30, 9, 1, label="Inference Steps")
|
| 62 |
+
seed = gr.Number(42, precision=0, label="Seed")
|
| 63 |
+
randomize = gr.Checkbox(True, label="Randomize seed")
|
| 64 |
+
generate_btn = gr.Button("Generate", variant="primary")
|
| 65 |
+
with gr.Column():
|
| 66 |
+
output_image = gr.Image(label="Generated Image", type="pil")
|
| 67 |
+
output_seed = gr.Number(label="Seed used", precision=0)
|
| 68 |
+
generate_btn.click(generate_image,
|
| 69 |
+
[prompt, height, width, steps, seed, randomize],
|
| 70 |
+
[output_image, output_seed], api_name="generate_image")
|
| 71 |
+
prompt.submit(generate_image,
|
| 72 |
+
[prompt, height, width, steps, seed, randomize],
|
| 73 |
+
[output_image, output_seed], api_name="generate_image_prompt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
|
|
|
|
|
|
|
|
|
| 75 |
with gr.Tab("🧹 图片去水印"):
|
| 76 |
with gr.Row():
|
| 77 |
with gr.Column():
|
| 78 |
+
wm_input = gr.Image(type="pil", label="Input image")
|
| 79 |
+
wm_prompt = gr.Textbox(value="watermark", label="Detection prompt")
|
| 80 |
+
wm_max = gr.Slider(1, 90, 30, 1, label="Max bbox area (%)")
|
| 81 |
+
wm_transparent = gr.Checkbox(False, label="Transparent output")
|
| 82 |
with gr.Row():
|
| 83 |
+
detect_btn = gr.Button("Detect / Preview")
|
| 84 |
+
remove_btn = gr.Button("Remove watermark", variant="primary")
|
| 85 |
with gr.Column():
|
| 86 |
+
wm_preview_out = gr.Image(label="Detection preview", type="pil")
|
| 87 |
+
wm_result = gr.Image(label="Result", type="pil")
|
| 88 |
+
wm_mask = gr.Image(label="Mask", type="pil")
|
| 89 |
+
detect_btn.click(wm_preview, [wm_input, wm_prompt, wm_max],
|
| 90 |
+
wm_preview_out, api_name="watermark_preview")
|
| 91 |
+
remove_btn.click(wm_remove_image,
|
| 92 |
+
[wm_input, wm_prompt, wm_max, wm_transparent],
|
| 93 |
+
[wm_result, wm_mask], api_name="remove_image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
|
|
|
|
|
|
|
|
|
| 95 |
with gr.Tab("🎬 视频去水印"):
|
| 96 |
+
gr.Markdown("建议先用短视频测试。Pass 1 检测,Pass 2 修复。")
|
| 97 |
+
video_input = gr.Video(label="Input video", format="mp4")
|
| 98 |
+
video_prompt = gr.Textbox(value="watermark", label="Detection prompt")
|
| 99 |
+
video_max = gr.Slider(1, 90, 30, 1, label="Max bbox area (%)")
|
| 100 |
+
video_skip = gr.Slider(1, 30, 3, 1, label="Detection interval")
|
| 101 |
+
fade_in = gr.Slider(0, 30, 2, 1, label="Fade in frames")
|
| 102 |
+
fade_out = gr.Slider(0, 30, 2, 1, label="Fade out frames")
|
| 103 |
+
video_btn = gr.Button("Remove watermark from video", variant="primary")
|
| 104 |
+
video_output = gr.Video(label="Output video", format="mp4")
|
| 105 |
+
video_btn.click(wm_remove_video,
|
| 106 |
+
[video_input, video_prompt, video_max, video_skip, fade_in, fade_out],
|
| 107 |
+
video_output, api_name="remove_video")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -7,15 +7,8 @@ peft
|
|
| 7 |
safetensors
|
| 8 |
sentencepiece
|
| 9 |
spaces
|
| 10 |
-
|
| 11 |
-
# Watermark detector / image-video processing
|
| 12 |
opencv-python-headless>=4.8,<4.12
|
| 13 |
numpy<2
|
| 14 |
Pillow>=10
|
| 15 |
-
loguru
|
| 16 |
-
tqdm
|
| 17 |
-
psutil
|
| 18 |
-
pyyaml
|
| 19 |
-
|
| 20 |
-
# Video audio preservation
|
| 21 |
imageio-ffmpeg
|
|
|
|
|
|
| 7 |
safetensors
|
| 8 |
sentencepiece
|
| 9 |
spaces
|
|
|
|
|
|
|
| 10 |
opencv-python-headless>=4.8,<4.12
|
| 11 |
numpy<2
|
| 12 |
Pillow>=10
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
imageio-ffmpeg
|
| 14 |
+
tqdm
|
watermark/__init__.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
from .engine import
|
| 2 |
from .video import process_video
|
| 3 |
-
|
| 4 |
-
__all__ = ["detect_only", "preview", "remove_image", "process_video"]
|
|
|
|
| 1 |
+
from .engine import preview, remove_image
|
| 2 |
from .video import process_video
|
| 3 |
+
__all__ = ["preview", "remove_image", "process_video"]
|
|
|
watermark/__pycache__/__init__.cpython-313.pyc
CHANGED
|
Binary files a/watermark/__pycache__/__init__.cpython-313.pyc and b/watermark/__pycache__/__init__.cpython-313.pyc differ
|
|
|
watermark/__pycache__/engine.cpython-313.pyc
CHANGED
|
Binary files a/watermark/__pycache__/engine.cpython-313.pyc and b/watermark/__pycache__/engine.cpython-313.pyc differ
|
|
|
watermark/__pycache__/video.cpython-313.pyc
CHANGED
|
Binary files a/watermark/__pycache__/video.cpython-313.pyc and b/watermark/__pycache__/video.cpython-313.pyc differ
|
|
|
watermark/engine.py
CHANGED
|
@@ -1,192 +1,117 @@
|
|
| 1 |
-
import hashlib
|
| 2 |
-
import os
|
| 3 |
from pathlib import Path
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
import cv2
|
| 7 |
import numpy as np
|
| 8 |
import torch
|
| 9 |
-
from PIL import Image, ImageDraw
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
with torch.inference_mode():
|
| 69 |
-
|
| 70 |
-
input_ids=
|
| 71 |
-
pixel_values=
|
| 72 |
-
max_new_tokens=1024,
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
task="<OPEN_VOCABULARY_DETECTION>",
|
| 81 |
-
image_size=image.size,
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
def detect_only(image: Image.Image, max_bbox_percent=15.0, detection_prompt="watermark"):
|
| 86 |
-
image = image.convert("RGB")
|
| 87 |
-
parsed = _identify(image, detection_prompt)
|
| 88 |
-
key = "<OPEN_VOCABULARY_DETECTION>"
|
| 89 |
-
results = []
|
| 90 |
-
image_area = image.width * image.height
|
| 91 |
-
for bbox in parsed.get(key, {}).get("bboxes", []):
|
| 92 |
-
x1, y1, x2, y2 = map(int, bbox)
|
| 93 |
-
area_percent = ((x2 - x1) * (y2 - y1) / image_area) * 100
|
| 94 |
-
results.append({
|
| 95 |
-
"bbox": [x1, y1, x2, y2],
|
| 96 |
-
"area_percent": round(area_percent, 2),
|
| 97 |
-
"accepted": area_percent <= float(max_bbox_percent),
|
| 98 |
-
})
|
| 99 |
-
return results
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
def make_mask(image: Image.Image, max_bbox_percent=15.0, detection_prompt="watermark"):
|
| 103 |
-
mask = Image.new("L", image.size, 0)
|
| 104 |
-
draw = ImageDraw.Draw(mask)
|
| 105 |
-
for item in detect_only(image, max_bbox_percent, detection_prompt):
|
| 106 |
-
if item["accepted"]:
|
| 107 |
-
draw.rectangle(item["bbox"], fill=255)
|
| 108 |
-
return mask
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
def preview(image: Image.Image, max_bbox_percent=15.0, detection_prompt="watermark"):
|
| 112 |
-
image = image.convert("RGB")
|
| 113 |
-
detections = detect_only(image, max_bbox_percent, detection_prompt)
|
| 114 |
mask = Image.new("L", image.size, 0)
|
| 115 |
draw = ImageDraw.Draw(mask)
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
if
|
| 122 |
-
draw.rectangle(
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
def
|
| 137 |
-
if
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
if
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
return
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
def _lama_inpaint(image: Image.Image, mask: Image.Image):
|
| 153 |
-
image_np = np.array(image.convert("RGB"))
|
| 154 |
-
mask_np = np.array(mask.convert("L"))
|
| 155 |
-
if not np.any(mask_np > 0):
|
| 156 |
-
return image.copy()
|
| 157 |
-
|
| 158 |
-
# Keep very large inputs manageable while retaining the original aspect ratio.
|
| 159 |
-
max_side = 1600
|
| 160 |
-
scale = min(1.0, max_side / max(image_np.shape[:2]))
|
| 161 |
-
if scale < 1.0:
|
| 162 |
-
new_size = (max(8, int(image_np.shape[1] * scale)), max(8, int(image_np.shape[0] * scale)))
|
| 163 |
-
image_small = cv2.resize(image_np, new_size, interpolation=cv2.INTER_AREA)
|
| 164 |
-
mask_small = cv2.resize(mask_np, new_size, interpolation=cv2.INTER_NEAREST)
|
| 165 |
-
else:
|
| 166 |
-
image_small, mask_small = image_np, mask_np
|
| 167 |
-
|
| 168 |
-
image_small, mask_small, orig_h, orig_w = _pad_to_8(image_small, mask_small)
|
| 169 |
-
image_tensor = torch.from_numpy(_norm_image(image_small)).unsqueeze(0).to(DEVICE)
|
| 170 |
-
mask_tensor = torch.from_numpy((mask_small > 127).astype("float32")).unsqueeze(0).unsqueeze(0).to(DEVICE)
|
| 171 |
-
|
| 172 |
-
with torch.inference_mode():
|
| 173 |
-
result = lama_model(image_tensor, mask_tensor)[0].permute(1, 2, 0).detach().float().cpu().numpy()
|
| 174 |
-
|
| 175 |
-
result = np.clip(result * 255, 0, 255).astype(np.uint8)
|
| 176 |
-
result = result[:orig_h, :orig_w]
|
| 177 |
-
if scale < 1.0:
|
| 178 |
-
result = cv2.resize(result, (image_np.shape[1], image_np.shape[0]), interpolation=cv2.INTER_LANCZOS4)
|
| 179 |
-
return Image.fromarray(result, "RGB")
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
def remove_image(image: Image.Image, max_bbox_percent=15.0, detection_prompt="watermark", transparent=False):
|
| 183 |
-
image = image.convert("RGB")
|
| 184 |
-
mask = make_mask(image, max_bbox_percent, detection_prompt)
|
| 185 |
if transparent:
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
arr[:, :, 3] = 255 - np.array(mask)
|
| 189 |
-
result = Image.fromarray(arr, "RGBA")
|
| 190 |
-
else:
|
| 191 |
-
result = _lama_inpaint(image, mask)
|
| 192 |
return result, mask
|
|
|
|
|
|
|
|
|
|
| 1 |
from pathlib import Path
|
| 2 |
+
import os
|
| 3 |
+
import urllib.request
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import torch
|
| 6 |
+
from PIL import Image, ImageDraw, ImageFilter
|
| 7 |
+
|
| 8 |
+
MODEL_CACHE = None
|
| 9 |
+
FLORENCE_MODEL = None
|
| 10 |
+
FLORENCE_PROCESSOR = None
|
| 11 |
+
LAMA_MODEL = None
|
| 12 |
+
|
| 13 |
+
def _get_writable_cache():
|
| 14 |
+
candidates = []
|
| 15 |
+
if os.getenv("WATERMARK_CACHE_DIR"):
|
| 16 |
+
candidates.append(Path(os.environ["WATERMARK_CACHE_DIR"]))
|
| 17 |
+
candidates += [Path("/data/.cache/watermark"),
|
| 18 |
+
Path("/tmp/watermark"),
|
| 19 |
+
Path.home()/".cache"/"watermark"]
|
| 20 |
+
for p in candidates:
|
| 21 |
+
try:
|
| 22 |
+
p.mkdir(parents=True, exist_ok=True)
|
| 23 |
+
t = p/".write_test"
|
| 24 |
+
t.write_text("ok", encoding="utf-8")
|
| 25 |
+
t.unlink(missing_ok=True)
|
| 26 |
+
print(f"Watermark cache: {p}")
|
| 27 |
+
return p
|
| 28 |
+
except (PermissionError, OSError):
|
| 29 |
+
continue
|
| 30 |
+
raise RuntimeError("No writable watermark cache directory available.")
|
| 31 |
+
|
| 32 |
+
def _cache_dir():
|
| 33 |
+
global MODEL_CACHE
|
| 34 |
+
if MODEL_CACHE is None:
|
| 35 |
+
MODEL_CACHE = _get_writable_cache()
|
| 36 |
+
return MODEL_CACHE
|
| 37 |
+
|
| 38 |
+
def _load_models():
|
| 39 |
+
global FLORENCE_MODEL, FLORENCE_PROCESSOR, LAMA_MODEL
|
| 40 |
+
if FLORENCE_MODEL is not None and LAMA_MODEL is not None:
|
| 41 |
+
return
|
| 42 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 43 |
+
cache = _cache_dir()
|
| 44 |
+
model_id = os.getenv("FLORENCE_MODEL_ID", "microsoft/Florence-2-large")
|
| 45 |
+
print(f"Loading Florence-2: {model_id}")
|
| 46 |
+
FLORENCE_PROCESSOR = AutoProcessor.from_pretrained(
|
| 47 |
+
model_id, cache_dir=str(cache/"hf"), trust_remote_code=True)
|
| 48 |
+
FLORENCE_MODEL = AutoModelForCausalLM.from_pretrained(
|
| 49 |
+
model_id, torch_dtype=torch.bfloat16,
|
| 50 |
+
cache_dir=str(cache/"hf"), trust_remote_code=True).to("cuda").eval()
|
| 51 |
+
lama = cache/"big-lama.pt"
|
| 52 |
+
if not lama.exists():
|
| 53 |
+
urllib.request.urlretrieve(
|
| 54 |
+
os.getenv("LAMA_URL",
|
| 55 |
+
"https://github.com/Sanster/models/releases/download/add_big_lama/big-lama.pt"),
|
| 56 |
+
lama)
|
| 57 |
+
LAMA_MODEL = torch.jit.load(str(lama), map_location="cuda").eval()
|
| 58 |
+
print("Watermark models loaded.")
|
| 59 |
+
|
| 60 |
+
def _boxes(image, prompt):
|
| 61 |
+
_load_models()
|
| 62 |
+
task = "<OPEN_VOCABULARY_DETECTION>"
|
| 63 |
+
inputs = FLORENCE_PROCESSOR(text=task+prompt, images=image, return_tensors="pt")
|
| 64 |
+
inputs = {k:(v.to("cuda") if hasattr(v,"to") else v) for k,v in inputs.items()}
|
| 65 |
with torch.inference_mode():
|
| 66 |
+
generated = FLORENCE_MODEL.generate(
|
| 67 |
+
input_ids=inputs.get("input_ids"),
|
| 68 |
+
pixel_values=inputs.get("pixel_values"),
|
| 69 |
+
max_new_tokens=1024, num_beams=3)
|
| 70 |
+
decoded = FLORENCE_PROCESSOR.batch_decode(generated, skip_special_tokens=False)[0]
|
| 71 |
+
parsed = FLORENCE_PROCESSOR.post_process_generation(
|
| 72 |
+
decoded, task=task, image_size=image.size)
|
| 73 |
+
data = parsed.get(task, parsed)
|
| 74 |
+
return data.get("bboxes", []) if isinstance(data, dict) else []
|
| 75 |
+
|
| 76 |
+
def _mask(image, boxes, max_pct):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
mask = Image.new("L", image.size, 0)
|
| 78 |
draw = ImageDraw.Draw(mask)
|
| 79 |
+
total = image.width * image.height
|
| 80 |
+
for b in boxes:
|
| 81 |
+
if len(b) != 4: continue
|
| 82 |
+
x1,y1,x2,y2 = map(float,b)
|
| 83 |
+
area=max(0,x2-x1)*max(0,y2-y1)
|
| 84 |
+
if total and area/total*100 <= max_pct:
|
| 85 |
+
draw.rectangle((x1,y1,x2,y2), fill=255)
|
| 86 |
+
return mask.filter(ImageFilter.MaxFilter(7))
|
| 87 |
+
|
| 88 |
+
def _lama(image, mask):
|
| 89 |
+
_load_models()
|
| 90 |
+
a=np.asarray(image.convert("RGB"), dtype=np.float32)/255.0
|
| 91 |
+
m=np.asarray(mask, dtype=np.float32)/255.0
|
| 92 |
+
it=torch.from_numpy(a).permute(2,0,1).unsqueeze(0).cuda()*2-1
|
| 93 |
+
mt=(torch.from_numpy(m).unsqueeze(0).unsqueeze(0).cuda()>0.5).float()
|
| 94 |
+
with torch.inference_mode(): out=LAMA_MODEL(it,mt)
|
| 95 |
+
if isinstance(out,(tuple,list)): out=out[0]
|
| 96 |
+
out=((out.clamp(-1,1)+1)/2).squeeze(0).permute(1,2,0).cpu().numpy()
|
| 97 |
+
return Image.fromarray((out*255).clip(0,255).astype("uint8"))
|
| 98 |
+
|
| 99 |
+
def preview(image, max_bbox_percent=30, detection_prompt="watermark"):
|
| 100 |
+
if image is None: raise ValueError("Please upload an image.")
|
| 101 |
+
boxes=_boxes(image, detection_prompt)
|
| 102 |
+
out=image.convert("RGB").copy()
|
| 103 |
+
d=ImageDraw.Draw(out)
|
| 104 |
+
for b in boxes:
|
| 105 |
+
if len(b)==4: d.rectangle(tuple(map(float,b)), outline="red", width=4)
|
| 106 |
+
return out
|
| 107 |
+
|
| 108 |
+
def remove_image(image, max_bbox_percent=30, detection_prompt="watermark", transparent=False):
|
| 109 |
+
if image is None: raise ValueError("Please upload an image.")
|
| 110 |
+
boxes=_boxes(image, detection_prompt)
|
| 111 |
+
mask=_mask(image, boxes, max_bbox_percent)
|
| 112 |
+
if not boxes: return image.convert("RGBA" if transparent else "RGB"), mask
|
| 113 |
+
result=_lama(image, mask)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
if transparent:
|
| 115 |
+
result=result.convert("RGBA")
|
| 116 |
+
result.putalpha(mask.point(lambda p:255-p))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
return result, mask
|
watermark/video.py
CHANGED
|
@@ -1,97 +1,48 @@
|
|
| 1 |
-
import shutil
|
| 2 |
-
import subprocess
|
| 3 |
-
import tempfile
|
| 4 |
-
|
| 5 |
-
import imageio_ffmpeg
|
| 6 |
from pathlib import Path
|
| 7 |
-
|
| 8 |
import cv2
|
| 9 |
-
|
| 10 |
-
from
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
cap = cv2.VideoCapture(str(input_path))
|
| 22 |
-
if not cap.isOpened():
|
| 23 |
-
raise RuntimeError(f"Cannot open video: {input_path}")
|
| 24 |
-
|
| 25 |
-
fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
|
| 26 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 27 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 28 |
-
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 29 |
-
detection_skip = max(1, min(10, int(detection_skip)))
|
| 30 |
-
fade_in_frames = int(max(0.0, fade_in) * fps)
|
| 31 |
-
fade_out_frames = int(max(0.0, fade_out) * fps)
|
| 32 |
-
|
| 33 |
-
detections = {}
|
| 34 |
-
detection_frames = range(0, total, detection_skip)
|
| 35 |
-
for n, frame_idx in enumerate(detection_frames, 1):
|
| 36 |
-
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
|
| 37 |
-
ok, frame = cap.read()
|
| 38 |
-
if not ok:
|
| 39 |
-
continue
|
| 40 |
-
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 41 |
-
found = detect_only(image, max_bbox_percent, detection_prompt)
|
| 42 |
-
accepted = [x["bbox"] for x in found if x["accepted"]]
|
| 43 |
-
if accepted:
|
| 44 |
-
detections[frame_idx] = accepted
|
| 45 |
-
if progress:
|
| 46 |
-
progress(0.5 * n / max(1, len(range(0, total, detection_skip))))
|
| 47 |
-
|
| 48 |
-
frame_masks = {}
|
| 49 |
-
for frame_idx, boxes in detections.items():
|
| 50 |
-
start = max(0, frame_idx - fade_in_frames)
|
| 51 |
-
end = min(total, frame_idx + detection_skip + fade_out_frames)
|
| 52 |
-
for idx in range(start, end):
|
| 53 |
-
frame_masks.setdefault(idx, [])
|
| 54 |
-
for box in boxes:
|
| 55 |
-
if box not in frame_masks[idx]:
|
| 56 |
-
frame_masks[idx].append(box)
|
| 57 |
-
|
| 58 |
-
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
| 59 |
-
temp_dir = Path(tempfile.mkdtemp(prefix="wm_video_"))
|
| 60 |
-
temp_video = temp_dir / "video_no_audio.mp4"
|
| 61 |
-
writer = cv2.VideoWriter(str(temp_video), cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
|
| 62 |
-
frame_idx = 0
|
| 63 |
-
|
| 64 |
-
while True:
|
| 65 |
-
ok, frame = cap.read()
|
| 66 |
-
if not ok:
|
| 67 |
-
break
|
| 68 |
-
if frame_idx in frame_masks:
|
| 69 |
-
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 70 |
-
mask = Image.new("L", image.size, 0)
|
| 71 |
-
draw = ImageDraw.Draw(mask)
|
| 72 |
-
for x1, y1, x2, y2 in frame_masks[frame_idx]:
|
| 73 |
-
draw.rectangle([x1, y1, x2, y2], fill=255)
|
| 74 |
-
result = inpaint(image, mask)
|
| 75 |
-
frame = cv2.cvtColor(np.array(result), cv2.COLOR_RGB2BGR)
|
| 76 |
-
writer.write(frame)
|
| 77 |
-
frame_idx += 1
|
| 78 |
-
if progress:
|
| 79 |
-
progress(0.5 + 0.5 * frame_idx / max(1, total))
|
| 80 |
-
|
| 81 |
-
cap.release()
|
| 82 |
-
writer.release()
|
| 83 |
-
|
| 84 |
-
# Preserve original audio when ffmpeg is available.
|
| 85 |
try:
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
"
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
finally:
|
| 95 |
-
shutil.rmtree(
|
| 96 |
-
|
| 97 |
-
return str(output_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from pathlib import Path
|
| 2 |
+
import shutil, subprocess, tempfile
|
| 3 |
import cv2
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from .engine import _boxes, _mask, _lama
|
| 6 |
+
|
| 7 |
+
def process_video(video_path, detection_prompt, max_bbox_percent,
|
| 8 |
+
detection_skip, fade_in, fade_out, progress=None):
|
| 9 |
+
cap=cv2.VideoCapture(video_path)
|
| 10 |
+
if not cap.isOpened(): raise ValueError("Unable to open video.")
|
| 11 |
+
fps=cap.get(cv2.CAP_PROP_FPS) or 30
|
| 12 |
+
total=int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 13 |
+
work=Path(tempfile.mkdtemp(prefix="wm_video_"))
|
| 14 |
+
inp=work/"input"; out=work/"output"; inp.mkdir(); out.mkdir()
|
| 15 |
+
detections={}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
+
i=0
|
| 18 |
+
while True:
|
| 19 |
+
ok,frame=cap.read()
|
| 20 |
+
if not ok: break
|
| 21 |
+
p=inp/f"{i:08d}.png"; cv2.imwrite(str(p),frame)
|
| 22 |
+
if i % max(1,int(detection_skip)) == 0:
|
| 23 |
+
detections[i]=_boxes(Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)), detection_prompt)
|
| 24 |
+
i+=1
|
| 25 |
+
if progress: progress(.45*i/max(1,total), desc="Pass 1: detecting")
|
| 26 |
+
cap.release()
|
| 27 |
+
last=[]
|
| 28 |
+
keys=sorted(detections)
|
| 29 |
+
for n in range(i):
|
| 30 |
+
for k in reversed(keys):
|
| 31 |
+
if k<=n: last=detections[k]; break
|
| 32 |
+
image=Image.open(inp/f"{n:08d}.png").convert("RGB")
|
| 33 |
+
if last: _lama(image,_mask(image,last,max_bbox_percent)).save(out/f"{n:08d}.png")
|
| 34 |
+
else: image.save(out/f"{n:08d}.png")
|
| 35 |
+
if progress: progress(.45+.45*(n+1)/max(1,i), desc="Pass 2: removing")
|
| 36 |
+
try:
|
| 37 |
+
import imageio_ffmpeg
|
| 38 |
+
ffmpeg=imageio_ffmpeg.get_ffmpeg_exe()
|
| 39 |
+
except Exception:
|
| 40 |
+
ffmpeg=shutil.which("ffmpeg") or "ffmpeg"
|
| 41 |
+
result=Path(tempfile.gettempdir())/"watermark_removed.mp4"
|
| 42 |
+
cmd=[ffmpeg,"-y","-framerate",str(fps),"-i",str(out/"%08d.png"),
|
| 43 |
+
"-i",str(video_path),"-map","0:v:0","-map","1:a?","-c:v","libx264",
|
| 44 |
+
"-pix_fmt","yuv420p","-c:a","aac","-shortest",str(result)]
|
| 45 |
+
subprocess.run(cmd,check=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
|
| 46 |
+
return str(result)
|
| 47 |
finally:
|
| 48 |
+
shutil.rmtree(work, ignore_errors=True)
|
|
|
|
|
|