ahmedghani commited on
Commit
6158815
Β·
1 Parent(s): 345d368

added editing tools

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +149 -0
  3. packages.txt +1 -0
  4. requirements.txt +3 -0
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Editing Tools
3
- emoji: πŸ’©
4
  colorFrom: red
5
  colorTo: blue
6
  sdk: gradio
 
1
  ---
2
  title: Editing Tools
3
+ emoji: πŸ“½οΈπŸ“·πŸŽ₯πŸ“ΉπŸŽ¦πŸ–ΌοΈπŸŽ¨πŸ–ŒοΈ
4
  colorFrom: red
5
  colorTo: blue
6
  sdk: gradio
app.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import glob
2
+ import os
3
+ import io
4
+ import ffmpeg
5
+ import requests
6
+ from PIL import Image
7
+ import gradio as gr
8
+ import shutil
9
+ import concurrent.futures
10
+
11
+ def process_image(mask_data, image_path):
12
+ image = Image.open(image_path)
13
+ image_data = io.BytesIO()
14
+ image.save(image_data, format=image.format)
15
+ image_data = image_data.getvalue()
16
+
17
+ # Prepare form data
18
+ form_data = {
19
+ 'ldmSteps': 25,
20
+ 'ldmSampler': 'plms',
21
+ 'zitsWireframe': True,
22
+ 'hdStrategy': 'Original',
23
+ 'hdStrategyCropMargin': 196,
24
+ 'hdStrategyCropTrigerSize': 1280,
25
+ 'hdStrategyResizeLimit': 2048,
26
+ 'prompt': '',
27
+ 'negativePrompt': '',
28
+ 'croperX': -24,
29
+ 'croperY': -23,
30
+ 'croperHeight': 512,
31
+ 'croperWidth': 512,
32
+ 'useCroper': False,
33
+ 'sdMaskBlur': 5,
34
+ 'sdStrength': 0.75,
35
+ 'sdSteps': 50,
36
+ 'sdGuidanceScale': 7.5,
37
+ 'sdSampler': 'pndm',
38
+ 'sdSeed': 42,
39
+ 'sdMatchHistograms': False,
40
+ 'sdScale': 1,
41
+ 'cv2Radius': 5,
42
+ 'cv2Flag': 'INPAINT_NS',
43
+ 'paintByExampleSteps': 50,
44
+ 'paintByExampleGuidanceScale': 7.5,
45
+ 'paintByExampleSeed': 42,
46
+ 'paintByExampleMaskBlur': 5,
47
+ 'paintByExampleMatchHistograms': False,
48
+ 'sizeLimit': 1024,
49
+ }
50
+
51
+ files_data = {
52
+ 'image': (os.path.basename(image_path), image_data),
53
+ 'mask': ('mask.png', mask_data)
54
+ }
55
+
56
+ response = requests.post('https://ahmedghani-lama-cleaner-lama.hf.space/inpaint', data=form_data, files=files_data)
57
+
58
+ if response.headers['Content-Type'] == 'image/jpeg' or response.headers['Content-Type'] == 'image/png':
59
+ output_image_path = os.path.join('output_images', os.path.splitext(os.path.basename(image_path))[0] + '_inpainted' + os.path.splitext(image_path)[1])
60
+ with open(output_image_path, 'wb') as output_image_file:
61
+ output_image_file.write(response.content)
62
+ else:
63
+ print(f"Error processing {image_path}: {response.text}")
64
+
65
+ def remove_watermark(sketch, images_path='frames', output_path='output_images'):
66
+ if os.path.exists('output_images'):
67
+ shutil.rmtree('output_images')
68
+ os.makedirs('output_images')
69
+
70
+ mask_data = io.BytesIO()
71
+ sketch["mask"].save(mask_data, format=sketch["mask"].format)
72
+ mask_data = mask_data.getvalue()
73
+
74
+ image_paths = glob.glob(f'{images_path}/*.*')
75
+
76
+ with concurrent.futures.ThreadPoolExecutor() as executor:
77
+ executor.map(lambda image_path: process_image(mask_data, image_path), image_paths)
78
+
79
+ return gr.Video.update(value=convert_frames_to_video('output_images'), visible=True), gr.Button.update(value='Done!')
80
+
81
+ def convert_video_to_frames(video):
82
+ print(f" input video is : {video}")
83
+ if os.path.exists('input_video.mp4'):
84
+ os.remove('input_video.mp4')
85
+
86
+ ffmpeg.input(video).output('input_video.mp4').run()
87
+ video_path = 'input_video.mp4'
88
+
89
+ if os.path.exists('frames'):
90
+ shutil.rmtree('frames')
91
+ os.makedirs('frames')
92
+
93
+ video_name = os.path.splitext(os.path.basename(video_path))[0]
94
+ # os.system(f'ffmpeg -i {video_path} -qscale:v 2 frames/{video_name}_%d.jpg')
95
+ ffmpeg.input(video_path).output(f'frames/{video_name}_%d.jpg', qscale=2).run()
96
+ return gr.Image.update(value=f"{os.getcwd()}/frames/{video_name}_1.jpg", interactive=True), gr.Button.update(interactive=True)
97
+
98
+ def convert_frames_to_video(frames_path):
99
+ if os.path.exists('output_video.mp4'):
100
+ os.remove('output_video.mp4')
101
+
102
+ (
103
+ ffmpeg
104
+ .input(f'{frames_path}/*.jpg', pattern_type='glob', framerate=25)
105
+ .output('output_video.mp4')
106
+ .run()
107
+ )
108
+ return gr.Video.update(value='output_video.mp4', visible=True, interactive=True), gr.Button.update(interactive=False)
109
+
110
+ css = """
111
+ #remove_btn {
112
+ background: linear-gradient(#201d18, #2bbbc3);
113
+ font-weight: bold;
114
+ font-size: 18px;
115
+ color:white;
116
+ }
117
+ #remove_btn:hover {
118
+ background: linear-gradient(#2bbbc3, #201d18);
119
+ }
120
+ footer {
121
+ display: none !important;
122
+ }
123
+ """
124
+ demo = gr.Blocks(css=css, title="Video Watermark Remover")
125
+ with demo:
126
+ gr.Markdown("""
127
+ # <center>πŸŽ₯ Video Watermark Remover</center>
128
+ """)
129
+ with gr.Row():
130
+ with gr.Column():
131
+ input_video = gr.Video(label="Upload a Video")
132
+ with gr.Column():
133
+ mask = gr.Image(label="Create a mask for the image", tool="sketch", type="pil", interactive=False)
134
+ with gr.Row():
135
+ with gr.Column():
136
+ pass
137
+ with gr.Column():
138
+ remove_btn = gr.Button("Remove Watermark", interactive=False, elem_id="remove_btn")
139
+ with gr.Column():
140
+ pass
141
+
142
+ output_video = gr.Video(label="Output Video", interactive=False)
143
+ input_video.change(convert_video_to_frames, inputs=[input_video], outputs=[mask, remove_btn])
144
+ remove_btn.click(remove_watermark, inputs=[mask], outputs=[output_video, remove_btn])
145
+
146
+ #position:fixed;bottom:0;left:0;right:0;
147
+ 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>
148
+ """)
149
+ demo.launch(show_api=False)
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ffmpeg
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio==3.22.1
2
+ ffmpeg-python
3
+ glob