sunana commited on
Commit
9056339
1 Parent(s): e717453

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -23
app.py CHANGED
@@ -8,22 +8,6 @@ import FFV1MT_MS
8
  import flow_tools
9
 
10
 
11
- print(f"Is CUDA available: {torch.cuda.is_available()}")
12
- # True
13
- print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
14
- # Tesla T4
15
-
16
- model = FFV1MT_MS.FFV1DNN()
17
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
18
-
19
- print('Number fo parameters: {}'.format(model.num_parameters()))
20
- model.to(device)
21
- model_dict = torch.load('Model_example.pth.tar')['state_dict']
22
- # save model
23
- model.load_state_dict(model_dict, strict=True)
24
- model.eval()
25
-
26
-
27
  def process_images(videos, x, y):
28
  # read video file
29
  cap = cv2.VideoCapture(videos)
@@ -44,8 +28,8 @@ def process_images(videos, x, y):
44
  images = [torch.from_numpy(img).permute(2, 0, 1).float().to(device).unsqueeze(0) / 255.0 for img in images]
45
  # if the max size of the image is larger than 1024, resize the image to 1024 with same ratio
46
  max_size = max(images[0].shape[2], images[0].shape[3])
47
- if max_size > 832:
48
- ratio = 832 / max_size
49
  images = [torch.nn.functional.interpolate(img, scale_factor=ratio, mode='bicubic', align_corners=True) for img
50
  in images]
51
  # transform color image to gray image
@@ -86,15 +70,32 @@ description = "## Introduction(^_^)\n" \
86
  "We also provide two sliders to adjust the location of the attention visualizer. \n" \
87
  " **Note**: The demo is running on CPU, so it may take a while to process the video. \n"
88
 
89
- # examples = [["example_1.mp4", 62, 56], ["example_2.mp4", 59, 55], ["example_3.mp4", 50, 50], ["example_4.mp4", 50, 50],
90
- # ["example_5.mp4", 39, 72]]
91
- examples = [["example_1.mp4", 62, 56]]
92
  md = "![](https://drive.google.com/uc?id=1WBqYsKRwn_78A72MJBrk643l3-gfAssP) \n" \
93
  "## Author \n" \
94
  "This project page is developed by Zitang Sun (zitangsun96 @ gmail.com)\n" \
95
  "## LICENSE \n" \
96
  "This project is licensed under the terms of the MIT license. \n"
97
- iface = gr.Interface(fn=process_images,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  inputs=[gr.Video(label="Upload video or use the example images below"),
99
  gr.Slider(0, 100, label='X location of attention visualizer'),
100
  gr.Slider(0, 100, label='Y location of attention visualizer')],
@@ -107,4 +108,6 @@ iface = gr.Interface(fn=process_images,
107
  article=md,
108
  examples=examples)
109
 
110
- iface.launch(debug=True)
 
 
 
8
  import flow_tools
9
 
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def process_images(videos, x, y):
12
  # read video file
13
  cap = cv2.VideoCapture(videos)
 
28
  images = [torch.from_numpy(img).permute(2, 0, 1).float().to(device).unsqueeze(0) / 255.0 for img in images]
29
  # if the max size of the image is larger than 1024, resize the image to 1024 with same ratio
30
  max_size = max(images[0].shape[2], images[0].shape[3])
31
+ if max_size > 768:
32
+ ratio = 768 / max_size
33
  images = [torch.nn.functional.interpolate(img, scale_factor=ratio, mode='bicubic', align_corners=True) for img
34
  in images]
35
  # transform color image to gray image
 
70
  "We also provide two sliders to adjust the location of the attention visualizer. \n" \
71
  " **Note**: The demo is running on CPU, so it may take a while to process the video. \n"
72
 
73
+ examples = [["example_1.mp4", 62, 56], ["example_2.mp4", 59, 55], ["example_3.mp4", 50, 50], ["example_4.mp4", 50, 50],
74
+ ["example_5.mp4", 39, 72]]
75
+ # examples = [["example_1.mp4", 62, 56]]
76
  md = "![](https://drive.google.com/uc?id=1WBqYsKRwn_78A72MJBrk643l3-gfAssP) \n" \
77
  "## Author \n" \
78
  "This project page is developed by Zitang Sun (zitangsun96 @ gmail.com)\n" \
79
  "## LICENSE \n" \
80
  "This project is licensed under the terms of the MIT license. \n"
81
+
82
+ if __name__ =='__main__':
83
+ print(f"Is CUDA available: {torch.cuda.is_available()}")
84
+ # True
85
+ print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
86
+ # Tesla T4
87
+
88
+ model = FFV1MT_MS.FFV1DNN()
89
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
90
+
91
+ print('Number fo parameters: {}'.format(model.num_parameters()))
92
+ model.to(device)
93
+ model_dict = torch.load('Model_example.pth.tar')['state_dict']
94
+ # save model
95
+ model.load_state_dict(model_dict, strict=True)
96
+ model.eval()
97
+
98
+ iface = gr.Interface(fn=process_images,
99
  inputs=[gr.Video(label="Upload video or use the example images below"),
100
  gr.Slider(0, 100, label='X location of attention visualizer'),
101
  gr.Slider(0, 100, label='Y location of attention visualizer')],
 
108
  article=md,
109
  examples=examples)
110
 
111
+ iface.launch(debug=True)
112
+
113
+