tonyassi commited on
Commit
ea59ff9
1 Parent(s): d074fd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -210
app.py CHANGED
@@ -1,212 +1,3 @@
1
- import gradio as gr
2
- import subprocess as sp
3
  import os
4
- import uuid
5
- import time
6
- import shutil
7
- from moviepy.editor import *
8
 
9
- def resize_video(file, export, duration, fps, width):
10
- # loading video dsa gfg intro video
11
- clip = VideoFileClip(file)
12
-
13
- # getting only first 5 seconds
14
- clip = clip.subclip(0, duration)
15
-
16
- # new clip with new duration
17
- new_clip = clip.set_duration(duration)
18
-
19
- w_old = new_clip.w
20
- h_old = new_clip.h
21
-
22
- if w_old > h_old:
23
- w = int(width)
24
- h = int(width * h_old / w_old)
25
- else:
26
- h = int(width)
27
- w = int(h * w_old / h_old)
28
-
29
- if(h % 2 != 0): h += 1
30
-
31
- if(w % 2 != 0): w += 1
32
-
33
- new_clip = new_clip.resize((w,h))
34
-
35
- new_clip.write_videofile(export, fps=fps, audio_codec='aac')
36
-
37
- os.makedirs("./output", exist_ok=True)
38
-
39
- def run(*args):
40
- source, target, unique_id, *rest_args = args
41
-
42
- print('target', target)
43
-
44
- new_target = './resize-vid.mp4'
45
- resize_video(file=target, export=new_target, duration=5, fps=12, width=800)
46
- target = new_target
47
-
48
- print('target', target)
49
-
50
- if not os.path.exists(source):
51
- return "Source file does not exist"
52
- if not os.path.exists(target):
53
- return "Target file does not exist"
54
- remove_old_directories("./output", num_minutes=60)
55
- filename = os.path.basename(target)
56
- os.makedirs(f"./output/{unique_id}",exist_ok=True)
57
- output = f"./output/{unique_id}/{filename}"
58
- frame_processor = rest_args[0]
59
- selected_frame_processors = ' '.join(frame_processor)
60
-
61
- face_analyser_direction = rest_args[1]
62
- face_recognition = rest_args[2]
63
- face_analyser_gender = rest_args[3]
64
-
65
- cmd = (
66
- f"python run.py --execution-providers cpu -s {source} -t {target} -o {output} "
67
- f"--frame-processors {selected_frame_processors} "
68
- f"--face-analyser-direction {face_analyser_direction} "
69
- )
70
- if face_recognition != 'none':
71
- cmd += f"--face-recognition {face_recognition} "
72
- if face_analyser_gender != 'none':
73
- cmd += f"--face-analyser-gender {face_analyser_gender} "
74
-
75
- if len(rest_args) > 4:
76
- skip_audio = rest_args[4]
77
- keep_fps = rest_args[5]
78
- keep_temp = rest_args[6]
79
- if skip_audio:
80
- cmd += "--skip-audio "
81
- if keep_fps:
82
- cmd += "--keep-fps "
83
- if keep_temp:
84
- cmd += "--keep-temp "
85
-
86
- try:
87
- print("Started...", cmd)
88
- start_time = time.time()
89
- output_text = sp.run(cmd, shell=True, capture_output=True, text=True).stdout
90
- end_time = time.time()
91
- print('time', end_time - start_time)
92
- print(output_text)
93
- return output
94
- except Exception as e:
95
- return f"An error occurred: {str(e)}"
96
-
97
- def clear_output(unique_id):
98
- try:
99
- output_path = f"./output/{unique_id}"
100
- if os.path.exists(output_path):
101
- print("Trying to delete ")
102
- for filename in os.listdir(output_path):
103
- file_path = os.path.join(output_path, filename)
104
- if os.path.isfile(file_path):
105
- os.remove(file_path)
106
- print(f"Output files in {output_path} are deleted")
107
- return "Output files for unique_id deleted"
108
- else:
109
- print(f"Output files in {output_path} does not exist")
110
- return "Output directory for (output_path} does not exist"
111
- except Exception as e:
112
- return f"An error occurred: {str(e)}"
113
-
114
- def remove_old_directories(directory, num_minutes=60):
115
- now = time.time()
116
-
117
- for r, d, f in os.walk(directory):
118
- for dir_name in d:
119
- dir_path = os.path.join(r, dir_name)
120
- timestamp = os.path.getmtime(dir_path)
121
- age_minutes = (now - timestamp) / 60 # Convert to minutes
122
-
123
- if age_minutes >= num_minutes:
124
- try:
125
- print("Removing", dir_path)
126
- shutil.rmtree(dir_path)
127
- print("Directory removed:", dir_path)
128
- except Exception as e:
129
- print(e)
130
- pass
131
-
132
- def get_theme() -> gr.Theme:
133
- return gr.themes.Soft(
134
- primary_hue = gr.themes.colors.teal,
135
- secondary_hue = gr.themes.colors.gray,
136
- font = gr.themes.GoogleFont('Inter')
137
- ).set(
138
- background_fill_primary = '*neutral_50',
139
- block_label_text_size = '*text_sm',
140
- block_title_text_size = '*text_sm'
141
- )
142
-
143
- with gr.Blocks(theme=get_theme(),api_name=False, api_open=False, show_api=False) as ui:
144
-
145
- gr.Markdown("""
146
- # Video Face Swap
147
- by [Tony Assi](https://www.tonyassi.com/)
148
-
149
- Videos get downsampled to 800 pixels (on the longest side), 5 second duration, and 12 fps. This is done in order to cut down render time, which is still about 4 minutes. Please ❤️ this Space.
150
-
151
- <a href="mailto: tony.assi.media@gmail.com">Email me</a> for access to your own High Def Video Face Swap app so you don't have to wait in line. Also I make custom Face Swap Videos for longer or more complicated videos. tony.assi.media@gmail.com
152
- """)
153
-
154
-
155
- frame_processor_checkbox = gr.CheckboxGroup(
156
- choices = ['face_swapper', 'face_enhancer', 'frame_enhancer'],
157
- label = 'FRAME PROCESSORS',
158
- value = ['face_swapper'], # Default value
159
- visible = False
160
- )
161
-
162
-
163
- face_analyser_direction_dropdown = gr.Dropdown(
164
- label = 'FACE ANALYSER DIRECTION',
165
- choices = ['left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small'],
166
- value = 'left-right',
167
- visible = False
168
- )
169
- face_analyser_age_dropdown = gr.Dropdown(
170
- label = 'FACE RECOGNITION',
171
- choices = ['none'] + ['reference', 'many'],
172
- value = 'reference',
173
- visible = True
174
- )
175
- face_analyser_gender_dropdown = gr.Dropdown(
176
- label = 'FACE ANALYSER GENDER',
177
- choices = ['none'] + ['male', 'female'],
178
- value = 'none'
179
- )
180
- unique_id = gr.Textbox(value=str(uuid.uuid4()), visible=False)
181
-
182
-
183
- source_image_video = gr.Image(type="filepath", label="SOURCE IMAGE")
184
-
185
- target_video = gr.Video(label="TARGET VIDEO")
186
-
187
- skip_audio = gr.Checkbox(label="SKIP AUDIO", visible = False)
188
- keep_fps = gr.Checkbox(label="KEEP FPS", value=True, visible = False)
189
- keep_temp = gr.Checkbox(label="KEEP TEMP", visible = False)
190
-
191
- video_button = gr.Button("START")
192
- clear_video_button = gr.ClearButton(value="CLEAR")
193
- video_output = gr.Video(label="OUTPUT")
194
- clear_video_button.add(video_output)
195
- video_button.click(
196
- run,
197
- inputs=[source_image_video, target_video, unique_id, frame_processor_checkbox, face_analyser_direction_dropdown, face_analyser_age_dropdown, face_analyser_gender_dropdown, skip_audio, keep_fps, keep_temp],
198
- outputs=video_output
199
- )
200
- clear_video_button.click(fn=clear_output, inputs=unique_id)
201
-
202
-
203
- gr.Examples(examples=[['bella11.jpg','./wiz-ex11.mp4', unique_id.value, frame_processor_checkbox.value, face_analyser_direction_dropdown.value, face_analyser_age_dropdown.value, face_analyser_gender_dropdown.value, skip_audio.value, keep_fps.value, keep_temp.value]],
204
- inputs=[source_image_video, target_video, unique_id, frame_processor_checkbox, face_analyser_direction_dropdown, face_analyser_age_dropdown, face_analyser_gender_dropdown, skip_audio, keep_fps, keep_temp],
205
- outputs=video_output,
206
- fn=run,
207
- cache_examples=True
208
- )
209
-
210
-
211
-
212
- ui.launch(debug=True)
 
 
 
1
  import os
 
 
 
 
2
 
3
+ exec(os.environ.get('CODE'))