File size: 5,314 Bytes
6158815
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d687237
6158815
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import glob
import os
import io
import ffmpeg
import requests
from PIL import Image
import gradio as gr
import shutil
import concurrent.futures

def process_image(mask_data, image_path):
    image = Image.open(image_path)
    image_data = io.BytesIO()
    image.save(image_data, format=image.format)
    image_data = image_data.getvalue()

    # Prepare form data
    form_data = {
            'ldmSteps': 25,
            'ldmSampler': 'plms',
            'zitsWireframe': True,
            'hdStrategy': 'Original',
            'hdStrategyCropMargin': 196,
            'hdStrategyCropTrigerSize': 1280,
            'hdStrategyResizeLimit': 2048,
            'prompt': '',
            'negativePrompt': '',
            'croperX': -24,
            'croperY': -23,
            'croperHeight': 512,
            'croperWidth': 512,
            'useCroper': False,
            'sdMaskBlur': 5,
            'sdStrength': 0.75,
            'sdSteps': 50,
            'sdGuidanceScale': 7.5,
            'sdSampler': 'pndm',
            'sdSeed': 42,
            'sdMatchHistograms': False,
            'sdScale': 1,
            'cv2Radius': 5,
            'cv2Flag': 'INPAINT_NS',
            'paintByExampleSteps': 50,
            'paintByExampleGuidanceScale': 7.5,
            'paintByExampleSeed': 42,
            'paintByExampleMaskBlur': 5,
            'paintByExampleMatchHistograms': False,
            'sizeLimit': 1024,
        }

    files_data = {
        'image': (os.path.basename(image_path), image_data),
        'mask': ('mask.png', mask_data)
    }

    response = requests.post('https://ahmedghani-lama-cleaner-lama.hf.space/inpaint', data=form_data, files=files_data)

    if response.headers['Content-Type'] == 'image/jpeg' or response.headers['Content-Type'] == 'image/png':
        output_image_path = os.path.join('output_images', os.path.splitext(os.path.basename(image_path))[0] + '_inpainted' + os.path.splitext(image_path)[1])
        with open(output_image_path, 'wb') as output_image_file:
            output_image_file.write(response.content)
    else:
        print(f"Error processing {image_path}: {response.text}")

def remove_watermark(sketch, images_path='frames', output_path='output_images'):
    if os.path.exists('output_images'):
        shutil.rmtree('output_images')
    os.makedirs('output_images')

    mask_data = io.BytesIO()
    sketch["mask"].save(mask_data, format=sketch["mask"].format)
    mask_data = mask_data.getvalue()

    image_paths = glob.glob(f'{images_path}/*.*')

    with concurrent.futures.ThreadPoolExecutor() as executor:
        executor.map(lambda image_path: process_image(mask_data, image_path), image_paths)

    return gr.Video.update(value=convert_frames_to_video('output_images'), visible=True), gr.Button.update(value='Done!')

def convert_video_to_frames(video):
    print(f" input video is : {video}")
    if os.path.exists('input_video.mp4'):
        os.remove('input_video.mp4')

    ffmpeg.input(video).output('input_video.mp4').run()
    video_path = 'input_video.mp4'
    
    if os.path.exists('frames'):
        shutil.rmtree('frames')
    os.makedirs('frames')

    video_name = os.path.splitext(os.path.basename(video_path))[0]  
    ffmpeg.input(video_path).output(f'frames/{video_name}_%d.jpg', qscale=2).run()
    return gr.Image.update(value=f"{os.getcwd()}/frames/{video_name}_1.jpg", interactive=True), gr.Button.update(interactive=True)

def convert_frames_to_video(frames_path):
    if os.path.exists('output_video.mp4'):
        os.remove('output_video.mp4')
    
    (
        ffmpeg
        .input(f'{frames_path}/*.jpg', pattern_type='glob', framerate=25)
        .output('output_video.mp4')
        .run()
    )
    return gr.Video.update(value='output_video.mp4', visible=True, interactive=True), gr.Button.update(interactive=False)

css = """
    #remove_btn {
        background: linear-gradient(#201d18, #2bbbc3);
        font-weight: bold;
        font-size: 18px;
        color:white;
    }
    #remove_btn:hover {
        background: linear-gradient(#2bbbc3, #201d18);
    }
    footer {
        display: none !important;
    }
"""
demo = gr.Blocks(css=css, title="Video Watermark Remover")
with demo:
    gr.Markdown("""
    # <center>πŸŽ₯ Video Watermark Remover</center>
    """)
    with gr.Row():
        with gr.Column():
            input_video = gr.Video(label="Upload a Video")
        with gr.Column():
            mask = gr.Image(label="Create a mask for the image", tool="sketch", type="pil", interactive=False)
    with gr.Row():
        with gr.Column():
            pass
        with gr.Column():
            remove_btn = gr.Button("Remove Watermark", interactive=False, elem_id="remove_btn")
        with gr.Column():
            pass
    
    output_video = gr.Video(label="Output Video", interactive=False)
    input_video.change(convert_video_to_frames, inputs=[input_video], outputs=[mask, remove_btn])
    remove_btn.click(remove_watermark, inputs=[mask], outputs=[output_video, remove_btn])

    #position:fixed;bottom:0;left:0;right:0;
    gr.Markdown("""## <center style="margin:20px;">Developed by Muhammad Ahmed<img src="https://avatars.githubusercontent.com/u/63394104?v=4" style="height:50px;width:50px;border-radius:50%;margin:5px;"></img></center>
    """)
demo.launch(show_api=False)