nouamanetazi HF staff commited on
Commit
d65aa43
1 Parent(s): aa1e949

add app.py

Browse files
Files changed (2) hide show
  1. app.py +100 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import yaml
3
+ import gradio as gr
4
+ import numpy as np
5
+
6
+ import imageio, cv2
7
+ from moviepy.editor import *
8
+ from skimage.transform import resize
9
+ from skimage import img_as_ubyte
10
+ from skimage.color import rgb2gray
11
+
12
+
13
+ from tensorflow import keras
14
+
15
+ # load model
16
+ model = keras.models.load_model('saved_model')
17
+
18
+ # Examples
19
+ samples = []
20
+ example_driving = os.listdir('asset/driving')
21
+ for video in example_driving:
22
+ samples.append([f'asset/driving/{video}', 0.5, False])
23
+
24
+
25
+ def inference(driving,
26
+ split_pred = 0.4, # predict 0.6% of video
27
+ predict_one = False, # Whether to predict a sliding one frame or all frames at once
28
+ output_name = 'output.mp4',
29
+ output_path = 'asset/output',
30
+ cpu = False,
31
+ ):
32
+
33
+ # driving
34
+ reader = imageio.get_reader(driving)
35
+ fps = reader.get_meta_data()['fps']
36
+ driving_video = []
37
+ try:
38
+ for im in reader:
39
+ driving_video.append(im)
40
+ except RuntimeError:
41
+ pass
42
+ reader.close()
43
+ driving_video = [rgb2gray(resize(frame, (64, 64)))[..., np.newaxis] for frame in driving_video]
44
+
45
+ example = np.array(driving_video)
46
+ print(example.shape)
47
+ # Pick the first/last ten frames from the example.
48
+ start_pred_id = int(split_pred * example.shape[0]) # prediction starts from frame start_pred_id
49
+ frames = example[:start_pred_id, ...]
50
+ original_frames = example[start_pred_id:, ...]
51
+ new_predictions = np.zeros(shape=(example.shape[0] - start_pred_id, *frames[0].shape))
52
+
53
+ # Predict a new set of 10 frames.
54
+ for i in range(example.shape[0] - start_pred_id):
55
+ # Extract the model's prediction and post-process it.
56
+ if predict_one:
57
+ frames = example[: start_pred_id + i + 1, ...]
58
+ else:
59
+ frames = np.concatenate((example[: start_pred_id+1 , ...], new_predictions[:i, ...]), axis=0)
60
+ new_prediction = model.predict(np.expand_dims(frames, axis=0))
61
+ new_prediction = np.squeeze(new_prediction, axis=0)
62
+ predicted_frame = np.expand_dims(new_prediction[-1, ...], axis=0)
63
+
64
+ # Extend the set of prediction frames.
65
+ new_predictions[i] = predicted_frame
66
+
67
+ # Create and save MP4s for each of the ground truth/prediction images.
68
+ def postprocess(frame_set, save_file):
69
+ # Construct a GIF from the selected video frames.
70
+ current_frames = np.squeeze(frame_set)
71
+ current_frames = current_frames[..., np.newaxis] * np.ones(3)
72
+ current_frames = (current_frames * 255).astype(np.uint8)
73
+ current_frames = list(current_frames)
74
+
75
+ print(f'{output_path}/{save_file}')
76
+ imageio.mimsave(f'{output_path}/{save_file}', current_frames, fps=fps)
77
+
78
+ # save video
79
+ os.makedirs(output_path, exist_ok=True)
80
+ postprocess(original_frames, "original.mp4")
81
+ postprocess(new_predictions, output_name)
82
+ return f'{output_path}/{output_name}', f'{output_path}/original.mp4'
83
+
84
+ iface = gr.Interface(
85
+ inference, # main function
86
+ inputs = [
87
+ gr.inputs.Video(label='Video', type='mp4'),
88
+ gr.inputs.Slider(minimum=.1, maximum=.9, default=.5, step=.001, label="prediction start"),
89
+ gr.inputs.Checkbox(label="predict one frame only", default=False),
90
+
91
+ ],
92
+ outputs = [
93
+ gr.outputs.Video(label='result'), # generated video
94
+ gr.outputs.Video(label='ground truth') # same part of original video
95
+ ],
96
+
97
+ title = 'Next-Frame Video Prediction with Convolutional LSTMs',
98
+ description = "This app is an unofficial demo web app of the Next-Frame Video Prediction with Convolutional LSTMs by Keras.",
99
+ examples = samples,
100
+ ).launch(enable_queue=True, cache_examples=True)
requirements.txt ADDED
File without changes