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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Video Subtitle Remover - Google Colab\n",
    "\n",
    "This notebook allows you to run Video Subtitle Remover in Google Colab with free GPU access.\n",
    "\n",
    "**New in this version:**\n",
    "- \ud83d\udcdd Subtitle extraction to SRT files with OCR\n",
    "- \ud83e\udd16 Automatic model downloading from Hugging Face\n",
    "- \u26a1 Improved performance\n",
    "\n",
    "**Requirements:**\n",
    "- Google account\n",
    "- Video file (upload to Google Drive or use sample)\n",
    "\n",
    "**Colab Environment:**\n",
    "- Python: 3.10\n",
    "- CUDA: 12.2 (Tesla T4 GPU)\n",
    "- GPU Memory: 15GB\n",
    "\n",
    "**Recommended Algorithm:** STTN (fastest for Colab's limited runtime)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 1: Check GPU and Environment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Check GPU availability\n",
    "!nvidia-smi\n",
    "\n",
    "import torch\n",
    "print(f\"Python version: 3.10\")\n",
    "print(f\"PyTorch version: {torch.__version__}\")\n",
    "print(f\"CUDA available: {torch.cuda.is_available()}\")\n",
    "if torch.cuda.is_available():\n",
    "    print(f\"CUDA version: {torch.version.cuda}\")\n",
    "    print(f\"GPU: {torch.cuda.get_device_name(0)}\")\n",
    "    print(f\"GPU Memory: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.1f} GB\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2: Clone Repository"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Clone the repository\n",
    "!git clone https://huggingface.co/Rasta02/dataku\n",
    "%cd dataku"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2.5: Verify Installation\n",
    "\n",
    "Quick check that the repository was cloned successfully."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Verify the repository structure\n",
    "import os\n",
    "\n",
    "repo_path = '/content/dataku'\n",
    "if os.path.exists(repo_path):\n",
    "    print('\u2713 Repository cloned successfully')\n",
    "    print(f'  Path: {repo_path}')\n",
    "else:\n",
    "    print('\u274c Repository not found')\n",
    "    print('  Please run Step 2 first')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 3: Install Dependencies\n",
    "\n",
    "Colab already has many packages. We'll install missing ones."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install dependencies\n",
    "# Note: Colab already has torch, torchvision, opencv-python, numpy, etc.\n",
    "!pip install -q filesplit==3.0.2 albumentations scikit-image imgaug pyclipper lmdb\n",
    "!pip install -q PyYAML omegaconf tqdm easydict scikit-learn pandas webdataset\n",
    "!pip install -q protobuf av einops paddleocr paddle2onnx onnxruntime-gpu\n",
    "\n",
    "# Install PaddlePaddle GPU version (compatible with Colab)\n",
    "!pip install -q paddlepaddle-gpu==2.6.2\n",
    "\n",
    "# Advanced Inpainting Models (Optional - only install if using these modes)\n",
    "# Uncomment the line below to enable Stable Diffusion, DiffuEraser, E2FGVI\n",
    "# !pip install -q diffusers transformers accelerate\n",
    "\n",
    "print(\"\u2713 Dependencies installed!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 4: Mount Google Drive (Optional)\n",
    "\n",
    "Mount your Google Drive to access videos stored there."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from google.colab import drive\n",
    "drive.mount('/content/drive')\n",
    "\n",
    "# Your videos will be accessible at:\n",
    "# /content/drive/MyDrive/your_video.mp4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 5: Configure Settings\n",
    "\n",
    "Adjust these settings based on your needs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# === CONFIGURATION ===\n",
    "\n",
    "# Algorithm selection\n",
    "# Options:\n",
    "#   'STTN' - Fast, real-time video (recommended for Colab)\n",
    "#   'LAMA' - High quality for images/animation\n",
    "#   'PROPAINTER' - Best quality, very slow, high VRAM\n",
    "#   'SD' - Stable Diffusion (NEW - requires extra install above)\n",
    "#   'DIFFUERASER' - Specialized subtitle removal (Coming soon)\n",
    "#   'E2FGVI' - Fast flow-guided (Coming soon)\n",
    "ALGORITHM = 'STTN'\n",
    "\n",
    "# STTN Settings (recommended for Colab)\n",
    "STTN_SKIP_DETECTION = True  # Much faster, processes entire subtitle area\n",
    "STTN_MAX_LOAD_NUM = 40      # Reduce if OOM (30-50 for T4 GPU)\n",
    "STTN_NEIGHBOR_STRIDE = 5\n",
    "STTN_REFERENCE_LENGTH = 10\n",
    "\n",
    "# LAMA Settings\n",
    "LAMA_SUPER_FAST = False     # Set True for faster but lower quality\n",
    "\n",
    "# ProPainter Settings (requires 16GB+ GPU, not recommended for Colab)\n",
    "PROPAINTER_MAX_LOAD_NUM = 40  # Very low for T4 GPU\n",
    "\n",
    "# Stable Diffusion Settings (NEW)\n",
    "SD_STEPS = 50                # More steps = better quality but slower\n",
    "SD_GUIDANCE_SCALE = 7.5      # How much to follow the prompt\n",
    "SD_PROMPT = \"natural scene, high quality\"  # Text guidance\n",
    "\n",
    "# Video path (change this)\n",
    "# Option 1: Use sample video\n",
    "VIDEO_PATH = '/content/dataku/test/test.mp4'\n",
    "\n",
    "# Option 2: Use video from Google Drive (uncomment)\n",
    "# VIDEO_PATH = '/content/drive/MyDrive/my_video.mp4'\n",
    "\n",
    "# Subtitle area (optional, in pixels: ymin, ymax, xmin, xmax)\n",
    "# None = auto-detect subtitle area\n",
    "SUBTITLE_AREA = None\n",
    "\n",
    "# Example: Bottom 20% of 1080p video\n",
    "# SUBTITLE_AREA = (864, 1080, 0, 1920)\n",
    "\n",
    "print(f\"Configuration:\")\n",
    "print(f\"  Algorithm: {ALGORITHM}\")\n",
    "print(f\"  Video: {VIDEO_PATH}\")\n",
    "print(f\"  Subtitle area: {SUBTITLE_AREA or 'Auto-detect'}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 6: Apply Configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Modify config.py with our settings\n",
    "import sys\n",
    "sys.path.insert(0, '/content/dataku')\n",
    "sys.path.insert(0, '/content/dataku/backend')\n",
    "\n",
    "from backend import config\n",
    "from backend.config import InpaintMode\n",
    "\n",
    "# Apply algorithm selection\n",
    "if ALGORITHM == 'STTN':\n",
    "    config.MODE = InpaintMode.STTN\n",
    "    config.STTN_SKIP_DETECTION = STTN_SKIP_DETECTION\n",
    "    config.STTN_MAX_LOAD_NUM = STTN_MAX_LOAD_NUM\n",
    "    config.STTN_NEIGHBOR_STRIDE = STTN_NEIGHBOR_STRIDE\n",
    "    config.STTN_REFERENCE_LENGTH = STTN_REFERENCE_LENGTH\n",
    "elif ALGORITHM == 'LAMA':\n",
    "    config.MODE = InpaintMode.LAMA\n",
    "    config.LAMA_SUPER_FAST = LAMA_SUPER_FAST\n",
    "elif ALGORITHM == 'PROPAINTER':\n",
    "    config.MODE = InpaintMode.PROPAINTER\n",
    "    config.PROPAINTER_MAX_LOAD_NUM = PROPAINTER_MAX_LOAD_NUM\n",
    "elif ALGORITHM == 'SD':\n",
    "    config.MODE = InpaintMode.STABLE_DIFFUSION\n",
    "    config.SD_STEPS = SD_STEPS\n",
    "    config.SD_GUIDANCE_SCALE = SD_GUIDANCE_SCALE\n",
    "    config.SD_PROMPT = SD_PROMPT\n",
    "elif ALGORITHM == 'DIFFUERASER':\n",
    "    config.MODE = InpaintMode.DIFFUERASER\n",
    "    print('\u26a0\ufe0f  DiffuEraser not yet implemented, will fall back to LAMA')\n",
    "elif ALGORITHM == 'E2FGVI':\n",
    "    config.MODE = InpaintMode.E2FGVI\n",
    "    print('\u26a0\ufe0f  E2FGVI not yet implemented, will fall back to STTN')\n",
    "\n",
    "print(f\"\u2713 Configuration applied!\")\n",
    "print(f\"  Using device: {config.device}\")\n",
    "print(f\"  Mode: {config.MODE.value}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 7: Process Video"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import multiprocessing\n",
    "from backend.main import SubtitleRemover\n",
    "import os\n",
    "\n",
    "# Check if video exists\n",
    "if not os.path.exists(VIDEO_PATH):\n",
    "    print(f\"\u274c Error: Video not found at {VIDEO_PATH}\")\n",
    "    print(\"Please upload your video or update VIDEO_PATH\")\n",
    "else:\n",
    "    print(f\"Processing video: {VIDEO_PATH}\")\n",
    "    print(f\"This may take several minutes...\")\n",
    "    print(f\"\")\n",
    "    \n",
    "    # Set multiprocessing start method\n",
    "    try:\n",
    "        multiprocessing.set_start_method(\"spawn\")\n",
    "    except:\n",
    "        pass\n",
    "    \n",
    "    # Create SubtitleRemover instance\n",
    "    sr = SubtitleRemover(VIDEO_PATH, sub_area=SUBTITLE_AREA, gui_mode=False)\n",
    "    \n",
    "    # Run processing\n",
    "    sr.run()\n",
    "    \n",
    "    # Output location\n",
    "    output_path = sr.video_out_name\n",
    "    print(f\"\\n\u2713 Processing complete!\")\n",
    "    print(f\"Output saved to: {output_path}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 8: Download Result\n",
    "\n",
    "Download the processed video to your computer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from google.colab import files\n",
    "import os\n",
    "\n",
    "# Get output filename\n",
    "video_name = os.path.basename(VIDEO_PATH)\n",
    "video_name_no_ext = os.path.splitext(video_name)[0]\n",
    "output_file = f\"{video_name_no_ext}_no_sub.mp4\"\n",
    "output_path = os.path.join(os.path.dirname(VIDEO_PATH), output_file)\n",
    "\n",
    "if os.path.exists(output_path):\n",
    "    print(f\"Downloading: {output_file}\")\n",
    "    files.download(output_path)\n",
    "else:\n",
    "    print(f\"\u274c Output file not found: {output_path}\")\n",
    "    print(\"Check if processing completed successfully.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 9: Save to Google Drive (Optional)\n",
    "\n",
    "Save the output video to Google Drive instead of downloading."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shutil\n",
    "import os\n",
    "\n",
    "# Destination in Google Drive\n",
    "drive_output_path = '/content/drive/MyDrive/video_subtitle_remover_output/'\n",
    "\n",
    "# Create directory if it doesn't exist\n",
    "os.makedirs(drive_output_path, exist_ok=True)\n",
    "\n",
    "# Copy output file\n",
    "if os.path.exists(output_path):\n",
    "    dest_file = os.path.join(drive_output_path, output_file)\n",
    "    shutil.copy(output_path, dest_file)\n",
    "    print(f\"\u2713 Saved to Google Drive: {dest_file}\")\n",
    "else:\n",
    "    print(f\"\u274c Output file not found\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Troubleshooting\n",
    "\n",
    "### Out of Memory (OOM)\n",
    "```python\n",
    "# Reduce batch size\n",
    "STTN_MAX_LOAD_NUM = 30\n",
    "# Or use LAMA\n",
    "ALGORITHM = 'LAMA'\n",
    "```\n",
    "\n",
    "### Processing too slow\n",
    "```python\n",
    "# Enable skip detection\n",
    "STTN_SKIP_DETECTION = True\n",
    "# Or use super fast mode\n",
    "ALGORITHM = 'LAMA'\n",
    "LAMA_SUPER_FAST = True\n",
    "```\n",
    "\n",
    "### Subtitles not removed\n",
    "```python\n",
    "# Set manual subtitle area (bottom 20% of 1080p)\n",
    "SUBTITLE_AREA = (864, 1080, 0, 1920)\n",
    "```\n",
    "\n",
    "### GPU not detected\n",
    "1. Runtime \u2192 Change runtime type \u2192 Hardware accelerator \u2192 GPU\n",
    "2. Restart runtime\n",
    "\n",
    "### Session timeout\n",
    "- Colab free tier has time limits\n",
    "- Process smaller videos (<10 min)\n",
    "- Or use Colab Pro for longer sessions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Advanced: Batch Processing\n",
    "\n",
    "Process multiple videos from Google Drive:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import glob\n",
    "import os\n",
    "from backend.main import SubtitleRemover\n",
    "\n",
    "# Folder containing videos\n",
    "video_folder = '/content/dataku/test/'\n",
    "\n",
    "# Find all MP4 files\n",
    "video_files = glob.glob(os.path.join(video_folder, '*.mp4'))\n",
    "\n",
    "print(f\"Found {len(video_files)} videos to process\")\n",
    "\n",
    "for i, video_path in enumerate(video_files, 1):\n",
    "    print(f\"\\n[{i}/{len(video_files)}] Processing: {os.path.basename(video_path)}\")\n",
    "    \n",
    "    try:\n",
    "        sr = SubtitleRemover(video_path, sub_area=SUBTITLE_AREA, gui_mode=False)\n",
    "        sr.run()\n",
    "        print(f\"\u2713 Complete: {sr.video_out_name}\")\n",
    "    except Exception as e:\n",
    "        print(f\"\u274c Error: {e}\")\n",
    "        continue\n",
    "\n",
    "print(f\"\\n\u2713 Batch processing complete!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tips for Best Results\n",
    "\n",
    "1. **Use STTN with skip detection** - Fastest for Colab\n",
    "2. **Keep videos under 10 minutes** - Avoid session timeout\n",
    "3. **Set manual subtitle area** - For better accuracy\n",
    "4. **Monitor GPU memory** - Use `!nvidia-smi` in separate cell\n",
    "5. **Save to Drive frequently** - Avoid data loss on timeout\n",
    "\n",
    "## Performance Expectations (Colab T4 GPU)\n",
    "\n",
    "| Video Length | Algorithm | Time |\n",
    "|--------------|-----------|------|\n",
    "| 1 min 720p | STTN (skip) | ~30s |\n",
    "| 5 min 720p | STTN (skip) | ~2min |\n",
    "| 1 min 720p | LAMA | ~3min |\n",
    "| 5 min 720p | LAMA | ~15min |\n",
    "\n",
    "ProPainter is not recommended for Colab due to memory limitations."
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "gpuType": "T4",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}