-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
908 lines (785 loc) · 32.4 KB
/
app.py
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
import glob
import os
from collections import namedtuple
import cv2
import numpy as np
import torch
import yaml
from gradio import (
Blocks,
Row,
Column,
Slider,
Tab,
PlayableVideo,
Button,
Radio,
HighlightedText,
Textbox,
Checkbox,
HTML,
Progress,
update,
)
from tqdm import tqdm
from backend.interpolate import DeepInterpolate, Interpolate, InterpolateEngine
from backend.upscale.upscale_series import UpscaleSeries
from backend.utils.file_utils import (
count_images_in_directory,
create_directory,
get_files,
remove_directory,
)
from backend.utils.simple_icons import SimpleIcons
class SimpleLog:
"""
Class to manage simple logging to the console
Collect log message and optionally print to the console
"""
def __init__(self, verbose: bool):
self.verbose = verbose
self.messages = []
def log(self, message: str) -> None:
"""Add a new log message"""
self.messages.append(message)
if self.verbose:
print(message)
def reset(self):
self.messages = []
self.log("log messages cleared")
log = SimpleLog(verbose=False)
class SimpleConfig:
"""Manage a simple YAML config file"""
def __new__(cls, path: str = "lib/config.yaml"):
if not hasattr(cls, "instance"):
cls.instance = super(SimpleConfig, cls).__new__(cls)
cls.instance.init(path)
return cls.instance
def init(self, path: str):
"""Load the config"""
with open(path, encoding="utf-8") as file:
self.config = yaml.load(file, Loader=yaml.FullLoader)
def get(self, key: str):
"""Get top-level config value"""
return self.config[key]
def config_obj(self):
"""Create an Object.Properties version of the config"""
return namedtuple("ConfigObj", self.config.keys())(*self.config.values())
config = SimpleConfig(path="lib/config.yaml").config_obj()
"""Construct the Gradio Blocks UI"""
app_header = HTML("🎬 Framester", elem_id="appheading")
with Blocks(
analytics_enabled=True, title="Framester", css=config.user_interface["css_file"]
) as app:
input_video_info = {}
def get_video_info(input_video):
global input_video_info
input_video_Obj = cv2.VideoCapture(input_video)
input_video_info["FPS"] = int(input_video_Obj.get(cv2.CAP_PROP_FPS))
input_video_info["height"] = int(input_video_Obj.get(cv2.CAP_PROP_FRAME_HEIGHT))
input_video_info["width"] = int(input_video_Obj.get(cv2.CAP_PROP_FRAME_WIDTH))
input_video_info["frame_count"] = int(
input_video_Obj.get(cv2.CAP_PROP_FRAME_COUNT)
)
return input_video_info
def update_splits_info(input_video_path: str, num_splits: float):
global input_video_info
def max_steps(num_splits: int) -> int:
"""Computing the count of work steps needed based on the number of splits"""
# Before splitting, there's one existing region between the before and after frames.
# Each split doubles the number of regions.
# Work steps = the final number of regions - the existing region.
return 2**num_splits - 1
"""Given a count of splits/search depth/search precision, compute the count of work steps"""
total_steps = int(max_steps(num_splits))
input_video_info = get_video_info(input_video_path)
return {
info_output_vi: update(value=total_steps),
info_new_fps: update(value=input_video_info["FPS"] * (total_steps + 1)),
}
def convert_png_to_mp4(input_images_dir: str, output_path: str):
global input_video_info
"""Convert button handler"""
frame_rate = count_images_in_directory(input_images_dir) // (
input_video_info["frame_count"] / input_video_info["FPS"]
)
# Retrieve the list of image filenames
image_filenames = glob.glob(f"{input_images_dir}/*.png")
# Sort the image filenames to ensure proper ordering
image_filenames.sort()
# Read the first image to get its dimensions
first_image = cv2.imread(image_filenames[0])
height, width, _ = first_image.shape
# Define the video codec and create a VideoWriter object
fourcc = cv2.VideoWriter_fourcc(
*"mp4v"
) # Choose the appropriate codec for your desired output format
video_output = cv2.VideoWriter(output_path, fourcc, frame_rate, (width, height))
# Iterate over the image filenames and write each frame to the video
for filename in image_filenames:
frame = cv2.imread(filename)
video_output.write(frame)
# Release the VideoWriter and close any open windows
video_output.release()
return True
def create_presentation_video(
input_video: str,
high_fps_video_path: str,
output_path: str,
presentation_mode: str,
):
def create_separation_matrix(n_rows, n_cols):
separation_matrix = []
if n_rows == n_cols:
for i in range(n_rows):
row = []
for j in range(n_cols):
if i == j:
row.append(0)
else:
row.append(1)
separation_matrix.append(row)
else:
if n_cols > n_rows:
overhead = (n_cols - n_rows) // 2
start_pt, end_pt = (0, overhead), (n_rows, n_cols - overhead)
else:
overhead = (n_rows - n_cols) // 2
start_pt, end_pt = (overhead, 0), (n_rows - overhead, n_cols)
separation_matrix = [[1] * (n_cols) for _ in range(n_rows)]
x1, y1 = start_pt
x2, y2 = end_pt
dx = abs(x2 - x1)
dy = abs(y2 - y1)
x_step = 1 if x1 < x2 else -1
y_step = 1 if y1 < y2 else -1
error = dx - dy
while x1 != x2 or y1 != y2:
separation_matrix[x1][y1] = 0
double_error = 2 * error
if double_error > -dy:
error -= dy
x1 += x_step
if double_error < dx:
error += dx
y1 += y_step
# The pixels on the left of diagonal should be represented as 2
lower_than_diagonal = False # Only applicable if n_rows > n_cols
for row_idx, row in enumerate(separation_matrix):
if 0 in row:
lower_than_diagonal = True
conv_to_2 = False
for col_idx, col in enumerate(row):
if conv_to_2:
separation_matrix[row_idx][col_idx] = 2
elif col == 0:
conv_to_2 = True
else:
separation_matrix[row_idx] = [
1 if lower_than_diagonal else 2 for _ in range(len(row))
]
return np.array(separation_matrix)
def put_highlighted_texts(
input_arrey: np.ndarray,
texts: dict = {},
font: int = cv2.FONT_HERSHEY_SIMPLEX,
font_scale: float = 1.0,
color: tuple = (170, 255, 0),
thickness: int = 2,
highlight_color: tuple = (0, 0, 0),
alpha: float = 0.5,
):
# Convert the color to BGR format
color = tuple(reversed(color))
highlight_color = tuple(reversed(highlight_color))
for text, position in texts.items():
# Get the text size
(text_width, text_height), _ = cv2.getTextSize(
text, font, font_scale, thickness
)
# Calculate the bounding box coordinates
x, y = position
x2, y2 = x + text_width + 10, y - text_height - 10
# Create a transparent overlay image
overlay = input_arrey.copy()
cv2.rectangle(
overlay, (x, y), (x2 + 10, y2 - 10), highlight_color, cv2.FILLED
)
cv2.addWeighted(overlay, alpha, input_arrey, 1 - alpha, 0, input_arrey)
# Put the highlighted text on the image
cv2.putText(
input_arrey,
text,
(x + 10, y - 10),
font,
font_scale,
color,
thickness,
cv2.LINE_AA,
)
return input_arrey
# Open the videos
video1 = cv2.VideoCapture(input_video)
video2 = cv2.VideoCapture(high_fps_video_path)
# Get the properties of the videos
fps1 = video1.get(cv2.CAP_PROP_FPS)
fps2 = video2.get(cv2.CAP_PROP_FPS)
target_video_width = int(video1.get(cv2.CAP_PROP_FRAME_WIDTH))
target_video_height = int(video1.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Calculate the frame repetition ratio for the lower FPS video
repetition_ratio = int(round(fps2 / fps1))
repeat_frame = repetition_ratio
if presentation_mode.startswith("Separate"):
# If video width is greater than video height then merge videos vertically
merge_top_down = target_video_width > target_video_height
# Create a VideoWriter object to save the merged video
merged_video = cv2.VideoWriter(
output_path,
cv2.VideoWriter_fourcc(*"mp4v"),
fps2,
(target_video_width, target_video_height * 2 + 2)
if merge_top_down
else (target_video_width * 2 + 2, target_video_height),
)
ret1, frame1 = video1.read()
while True:
# Read frames from video 2
ret2, frame2 = video2.read()
if not ret2:
break
# Create a White canvas to merge the frames side by side
merged_frame = 255 * np.ones(
shape=(2 + target_video_height * 2, target_video_width, 3)
if merge_top_down
else (target_video_height, 2 + target_video_width * 2, 3),
dtype=np.uint8,
)
# Repeat frames of video 1
if repeat_frame == 0:
repeat_frame = repetition_ratio - 1
# Read frames from video 1
ret1, frame1 = video1.read()
if not ret1:
break
else:
repeat_frame = repeat_frame - 1
frame1 = put_highlighted_texts(
input_arrey=frame1, texts={f"Original: {fps1} FPS": (25, 50)}
)
frame2 = put_highlighted_texts(
input_arrey=frame2, texts={f"Enhanced: {fps2+1} FPS": (25, 50)}
)
if merge_top_down:
(
merged_frame[:target_video_height, :target_video_width],
merged_frame[target_video_height + 2 :, :target_video_width],
) = (frame1, frame2)
else:
(
merged_frame[:, :target_video_width],
merged_frame[:, target_video_width + 2 :],
) = (
frame1,
frame2,
)
# Write the merged frame to the output video
merged_video.write(merged_frame)
elif presentation_mode.startswith("Diagonal"):
separation_matrix = create_separation_matrix(
n_rows=target_video_height, n_cols=target_video_width
)
# Create a VideoWriter object to save the merged video
merged_video = cv2.VideoWriter(
output_path,
cv2.VideoWriter_fourcc(*"mp4v"),
fps2,
(target_video_width, target_video_height),
)
# Read frames from the two videos and merge them
ret1, frame1 = video1.read()
text_loc_x = (target_video_width - cv2.getTextSize(f"Enhanced: {fps2} FPS", cv2.FONT_HERSHEY_SIMPLEX, 1.0, 2)[0][0] - 25)
while True:
merged_frame = 255 * np.ones(
shape=(target_video_height, target_video_width, 3),
dtype=np.uint8,
)
# Read frames from video 2
ret2, frame2 = video2.read()
if not ret2:
break
# Repeat frames of video 1
if repeat_frame == 0:
repeat_frame = repetition_ratio - 1
# Read frames from video 1
ret1, frame1 = video1.read()
if not ret1:
break
else:
repeat_frame = repeat_frame - 1
for row_idx in range(len(separation_matrix)):
for col_idx, col in enumerate(separation_matrix[row_idx]):
merged_frame[row_idx][col_idx] = (
frame1[row_idx][col_idx]
if col == 1
else frame2[row_idx][col_idx]
if col == 2
else [255, 255, 255]
)
merged_frame = put_highlighted_texts(
input_arrey=merged_frame,
texts={
f"Original: {fps1} FPS": (25, target_video_height - 50),
f"Enhanced: {fps2} FPS": (text_loc_x, 50),
},
)
# Write the merged frame to the output video
merged_video.write(merged_frame)
else: # For Vertical or Horizontal Splits....
merged_video = cv2.VideoWriter(
output_path,
cv2.VideoWriter_fourcc(*"mp4v"),
fps2,
(
target_video_width + 1
if presentation_mode.startswith("Vertical")
else target_video_width,
target_video_height + 1
if presentation_mode.startswith("Horizontal")
else target_video_height,
),
)
ret1, frame1 = video1.read()
while True:
merged_frame = 255 * np.ones(
shape=(
target_video_height + 1
if presentation_mode.startswith("Horizontal")
else target_video_height,
target_video_width + 1
if presentation_mode.startswith("Vertical")
else target_video_width,
3,
),
dtype=np.uint8,
)
ret2, frame2 = video2.read()
if not ret2:
break
if repeat_frame == 0:
repeat_frame = repetition_ratio - 1
ret1, frame1 = video1.read()
if not ret1:
break
else:
repeat_frame -= 1
if presentation_mode.startswith("Vertical"):
(
merged_frame[:, : target_video_width // 2],
merged_frame[:, 1 + target_video_width // 2 :],
) = (
frame1[:, : target_video_width // 2],
frame2[:, target_video_width // 2 :],
)
merged_frame = put_highlighted_texts(
input_arrey=merged_frame,
texts={
f"Original: {fps1} FPS": (25, 50),
f"Enhanced: {fps2} FPS": (25 + target_video_width // 2, 50),
},
)
elif presentation_mode.startswith("Horizontal"):
(
merged_frame[: target_video_height // 2, :target_video_width],
merged_frame[
target_video_height // 2 + 1 :, :target_video_width
],
) = (
frame1[: target_video_height // 2, :target_video_width],
frame2[target_video_height // 2 :, :target_video_width],
)
merged_frame = put_highlighted_texts(
input_arrey=merged_frame,
texts={
f"Original: {fps1} FPS": (25, 50),
f"Enhanced: {fps2} FPS": (
25,
target_video_height // 2 + 50,
),
},
)
merged_video.write(merged_frame)
# Release the resources
video1.release()
video2.release()
merged_video.release()
def reset_ui():
global input_video_info
remove_directory("temp_input_frames")
remove_directory("temp_interpolated_frames")
remove_directory("temp_upscaled_frames")
return {
controle_panel: update(visible=False),
interpolated_video: update(visible=False, value=None),
info_original_fps: update(value=0),
info_new_fps: update(value=0),
interpolate_button: update(visible=False),
video_info: update(visible=False, value=[]),
}
def upload_video(input_video_path: str, num_splits: int):
def resize_input_frames():
max_width, max_height = 1080, 720
frames_name_list = glob.glob("temp_input_frames/*.png")
original_height, original_width, _ = cv2.imread(frames_name_list[0]).shape
# Check if the image is larger than the desired dimensions
if original_width > max_width or original_height > max_height:
# Calculate the aspect ratio
aspect_ratio = min(
max_width / original_width, max_height / original_height
)
# Calculate the new dimensions
new_width = int(original_width * aspect_ratio)
new_height = int(original_height * aspect_ratio)
for frame_idx in tqdm(
range(len(frames_name_list)), desc="Resizing Frame"
):
img = cv2.imread(frames_name_list[frame_idx])
img = cv2.resize(
src=img,
dsize=(new_width, new_height),
interpolation=cv2.INTER_AREA,
)
cv2.imwrite(frames_name_list[frame_idx], img)
return True
return False
input_video_info = get_video_info(input_video_path)
create_directory("temp_input_frames")
# Convert Input Video frames to PNG files
cap = cv2.VideoCapture(os.path.join(os.getcwd(), input_video_path))
frame_idx = 0
while True:
ret, frame = cap.read()
if ret:
output_filename = os.path.join(
"temp_input_frames", f"image{str(frame_idx).zfill(5)}.png"
)
frame_idx += 1
cv2.imwrite(output_filename, frame)
else:
break
# Check if the video frames are larger than 1280 x 720 pixels
# If so, resize the frames...
if resize_input_frames():
convert_png_to_mp4(
input_images_dir="temp_input_frames", output_path=input_video_path
)
return {
input_video: update(value=input_video_path),
controle_panel: update(visible=True),
info_original_fps: update(value=input_video_info["FPS"]),
info_new_fps: update(value=input_video_info["FPS"] * (num_splits + 1)),
interpolate_button: update(visible=True),
video_info: update(
visible=True,
value=[(k, str(v)) for k, v in input_video_info.items()],
),
}
def video_inflation(
input_video_path: str,
num_splits: float,
output_mode: str,
presentation_mode: str,
progress=Progress(track_tqdm=True),
):
remove_directory("temp_interpolated_frames")
create_directory("temp_interpolated_frames")
"""Invoke the Video Inflation feature"""
progress(0, desc="Frames")
engine = InterpolateEngine(
config.model, config.gpu_ids, use_time_step=config.use_time_step
)
deep_interpolater = DeepInterpolate(
interpolater=Interpolate(engine.model, log.log),
time_step=config.use_time_step,
log_fn=log.log,
)
file_list = sorted(get_files("temp_input_frames", extension="png"))
output_path = "temp_interpolated_frames"
base_filename = "if_"
count = len(file_list)
num_width = len(str(count))
offset = 1
for frame in progress.tqdm(list(range(count - offset))):
# for other than the first around, the duplicated real "before" frame is deleted for
# continuity, since it's identical to the "after" from the previous round
continued = frame > 0
# if the offset is > 1 treat this as a resynthesis of frames
# and inform the deep interpolator to not keep the real frames
resynthesis = offset > 1
before_file = file_list[frame]
after_file = file_list[frame + offset]
# if a resynthesis, start the file numbering at 1 to match the restored frame
# if an offset other than 2 is used, the frame numbers won't generally match
base_index = frame + (1 if resynthesis else 0)
filename = base_filename + "[" + str(base_index).zfill(num_width) + "]"
inner_bar_desc = f"Frame-{base_index}"
deep_interpolater.split_frames(
before_file,
after_file,
num_splits,
output_path,
filename,
progress_label=inner_bar_desc,
continued=continued,
resynthesis=resynthesis,
)
del deep_interpolater
del engine
torch.cuda.empty_cache()
# Convert Interpolated frames to video of original length
convert_png_to_mp4(
input_images_dir="temp_interpolated_frames", output_path="interpolated.mp4"
)
return_Obj = {}
if output_mode == "Inplace":
return_Obj[input_video] = update(visible=True, value="interpolated.mp4")
elif output_mode == "Separate":
return_Obj[interpolated_video] = update(
visible=True, value="interpolated.mp4"
)
else:
create_presentation_video(
input_video_path, "interpolated.mp4", "merged.mp4", presentation_mode
)
return_Obj[interpolation_presentation_video] = update(
visible=True, value="merged.mp4"
)
return_Obj[video_info] = update(
visible=True, value=[(k, str(v)) for k, v in input_video_info.items()]
)
return return_Obj
def video_upscale(input_video: str, upscale_factor: int, inplace: bool):
remove_directory("temp_upscaled_frames")
output_path = "temp_input_frames" if inplace else "temp_upscaled_frames"
create_directory(output_path)
model_name = config.realesrgan_settings["model_name"]
gpu_ips = config.gpu_ids
fp32 = config.realesrgan_settings["fp32"]
upscaler = UpscaleSeries(
model_name, gpu_ips, fp32, tiling=0, tile_pad=0, log_fn=log.log
)
file_list = get_files(path="temp_input_frames", extension="png")
output_basename = "image"
output_dict = upscaler.upscale_series(
file_list, output_path, upscale_factor, output_basename, output_type="png"
)
file_list = [key for key in output_dict.keys() if output_dict[key] == None]
if file_list:
upscaler = UpscaleSeries(
model_name,
gpu_ips,
fp32,
tiling=config.realesrgan_settings["tiling"],
tile_pad=config.realesrgan_settings["tile_pad"],
log_fn=log.log,
)
output_dict = upscaler.upscale_series(
file_list,
output_path,
upscale_factor,
output_basename,
output_type="png",
)
# Convert Interpolated frames to video of original length
convert_png_to_mp4(
input_images_dir="temp_input_frames" if inplace else "temp_upscaled_frames",
output_path=input_video if inplace else "upscaled.mp4",
)
get_video_info(input_video=input_video if inplace else "output.mp4")
return (
{
input_video: update(value=input_video),
upscaled_video: update(value="upscaled.mp4", visible=True),
}
if inplace
else {
input_video: update(value=input_video),
upscaled_video: update(value=None, visible=False),
}
)
app_header.render()
HTML(
SimpleIcons.INCREASING
+ "Increase the number of video frames to any depth and upscale them to any size",
elem_id="tabheading",
)
remove_directory("temp_input_frames")
remove_directory("temp_interpolated_frames")
remove_directory("temp_upscaled_frames")
with Column() as video_panel:
with Row():
with Column(scale=3):
input_video = PlayableVideo(label="Low FPS Input video", format="mp4")
interpolated_video = PlayableVideo(
label="Video with increased FPS", visible=False, interactive=False
)
interpolation_presentation_video = PlayableVideo(
label="Original vs Interpolated Video",
visible=False,
interactive=False,
)
upscaled_video = PlayableVideo(
label="Upscaled Video", visible=False, interactive=False
)
with Column(scale=2, visible=False) as controle_panel:
video_info = HighlightedText(
value=[(k, v) for k, v in input_video_info.items()],
label="Input video Info",
)
with Tab(label="Interpolate"):
with Column(variant="panel"):
with Row():
splits_input_slider = Slider(
value=1,
minimum=1,
maximum=10,
step=1,
label="Split Count",
info="Number of splits b/w two frames",
)
info_output_vi = Textbox(
value="1",
label="Interpolations",
max_lines=1,
interactive=False,
info="Interpolations per frame",
)
with Row():
info_original_fps = Textbox(
value="0",
label="Original FPS",
max_lines=1,
interactive=False,
info="Original FPS of the input video",
)
info_new_fps = Textbox(
value="0",
label="Updated FPS",
max_lines=1,
interactive=False,
info="FPS after interpolations",
)
with Column():
output_mode = Radio(
choices=["Inplace", "Separate", "Presentation"],
value="Inplace",
label="Output mode",
info="Result Video placement",
interactive=True,
)
presentation_mode = Radio(
choices=[
"Separate (Auto align)",
"Diagonal Split",
"Vertical Split",
"Horizontal Split",
],
value="None",
label="Presentation mode",
info="How the presentation should appear?",
interactive=True,
visible=False,
)
interpolate_button = Button(
"Interpolate Video " + SimpleIcons.ROBOT,
variant="primary",
)
with Tab(label="Upscale"):
with Column(variant="panel"):
scale_input = Slider(
value=4.0,
minimum=1.0,
maximum=8.0,
step=0.05,
label="Frame Upscale Factor",
interactive=True,
)
upscale_inplace = Checkbox(
value=False,
label="Inplace",
interactive=True,
info="Replace the original video with Upscaled video",
)
upscale_button = Button(
"Upscale Video " + SimpleIcons.ROCKET, variant="primary"
)
"""Event Handelers"""
# For any update in input video component
input_video.clear(
reset_ui,
inputs=[],
outputs=[
input_video,
controle_panel,
info_original_fps,
info_new_fps,
interpolated_video,
interpolate_button,
video_info,
],
show_progress=True,
)
input_video.upload(
upload_video,
inputs=[input_video, splits_input_slider],
outputs=[
input_video,
controle_panel,
info_original_fps,
info_new_fps,
interpolate_button,
video_info,
],
show_progress=True,
)
# If Interpolate button is clicked
interpolate_button.click(
video_inflation,
inputs=[input_video, splits_input_slider, output_mode, presentation_mode],
outputs=[
input_video,
interpolated_video,
interpolation_presentation_video,
video_info,
],
)
def process_change_in_output_mode(output_mode_title):
return {
presentation_mode: update(
visible=True if output_mode_title == "Presentation" else False
)
}
output_mode.change(
process_change_in_output_mode, inputs=[output_mode], outputs=[presentation_mode]
)
# If Upscale Button is clicked
upscale_button.click(
video_upscale,
inputs=[input_video, scale_input, upscale_inplace],
outputs=[input_video, upscaled_video],
)
# If Split Count slider is used
splits_input_slider.change(
update_splits_info,
inputs=[input_video, splits_input_slider],
outputs=[info_output_vi, info_new_fps],
show_progress=False,
)
app.launch(
inbrowser=config.auto_launch_browser,
server_name=config.server_name,
server_port=config.server_port,
prevent_thread_lock=False,
share=False,
debug=False,
enable_queue=True,
)