File size: 3,762 Bytes
9c16f8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import shutil
from pathlib import Path
import logging

# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger("process_broll_watermarks")

# Add ComfyUI Articles directory to sys.path
sys.path.append(r"C:\ComfyUI\Articles")

from quality_gate_broll import detect_watermarks, remove_watermark_with_lama
from collect_broll_agent import generate_ken_burns_clip

ROOT_DIR = Path(r"C:\ComfyUI\Articles")

def main():
    logger.info("Scanning for existing B-roll images in Articles directory...")
    
    # Walk through ROOT_DIR to find Broll folders
    broll_images = []
    for root, dirs, files in os.walk(ROOT_DIR):
        if Path(root).name == "Broll":
            for f in files:
                f_path = Path(root) / f
                # Target only jpg/png images, skipping temporary files, masks, and originals
                if f_path.suffix.lower() in [".jpg", ".jpeg", ".png"]:
                    if f.startswith("tmp_") or f.startswith("std_") or "_original" in f or "_mask" in f:
                        continue
                    broll_images.append(f_path)
                    
    logger.info(f"Found {len(broll_images)} candidate B-roll images.")
    
    flagged_count = 0
    removed_count = 0
    regenerated_count = 0
    
    for i, img_path in enumerate(broll_images, 1):
        logger.info(f"[{i}/{len(broll_images)}] Scanning {img_path.relative_to(ROOT_DIR)}")
        
        detected_corners = detect_watermarks(img_path)
        if detected_corners:
            flagged_count += 1
            logger.info(f"  -> Watermark detected in corners: {detected_corners}")
            
            # Backup original image if not already done
            backup_path = img_path.parent / f"{img_path.name.replace(img_path.suffix, '')}_original{img_path.suffix}"
            if not backup_path.exists():
                shutil.copy2(img_path, backup_path)
                logger.info(f"  -> Created backup: {backup_path.name}")
                
            # Perform watermark removal using LaMa
            # We pass backup_path as input and img_path as output, so we always inpaint from the clean original
            success, reason = remove_watermark_with_lama(backup_path, img_path)
            
            if success:
                removed_count += 1
                logger.info(f"  -> Successfully removed watermark from {img_path.name}")
                
                # Check for corresponding mp4 clip to regenerate
                mp4_path = img_path.parent / f"{img_path.stem}.mp4"
                if mp4_path.exists():
                    logger.info(f"  -> Corresponding video clip found: {mp4_path.name}. Regenerating...")
                    # Re-render clip from the watermarked-free image
                    if generate_ken_burns_clip(img_path, mp4_path):
                        regenerated_count += 1
                        logger.info(f"  -> Successfully regenerated video clip: {mp4_path.name}")
                    else:
                        logger.error(f"  -> Failed to regenerate video clip: {mp4_path.name}")
            else:
                logger.warning(f"  -> Watermark removal failed: {reason}")
                
    logger.info("========================================")
    logger.info("Watermark Processing Finished.")
    logger.info(f"Total images scanned: {len(broll_images)}")
    logger.info(f"Images flagged with watermarks: {flagged_count}")
    logger.info(f"Images successfully inpainted: {removed_count}")
    logger.info(f"Video clips successfully regenerated: {regenerated_count}")
    logger.info("========================================")

if __name__ == "__main__":
    main()