awacke1 commited on
Commit
9ab5bd1
1 Parent(s): 4856f18

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +281 -0
app.py ADDED
@@ -0,0 +1,281 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import time
5
+ import random
6
+ from PIL import Image
7
+
8
+ # Additional imports for CoTracker3 Demo
9
+ import torch
10
+ import imageio.v3 as iio
11
+ import matplotlib.pyplot as plt
12
+ import colorsys
13
+ import tempfile
14
+ import cv2
15
+ import os
16
+
17
+ # Set up the page configuration
18
+ st.set_page_config(
19
+ page_title="Streamlit Super Fun Guide 🎈",
20
+ page_icon="🎈",
21
+ layout="wide",
22
+ initial_sidebar_state="expanded",
23
+ )
24
+
25
+ # Add a header with an emoji
26
+ st.title("Welcome to the Streamlit Super Fun Guide 🎉")
27
+
28
+ # Add a limerick
29
+ st.write("""
30
+ There once was a coder so bright,
31
+ Who coded with all of their might.
32
+ With Streamlit's delight,
33
+ They coded all night,
34
+ And apps popped up left and right!
35
+ """)
36
+
37
+ # Add a wise quote
38
+ st.write("> “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler")
39
+
40
+ # Sidebar navigation
41
+ st.sidebar.title("Navigation 🧭")
42
+ options = st.sidebar.radio("Go to", [
43
+ "Introduction 🚀",
44
+ "Upload Media 📁",
45
+ "Animated Charts 📊",
46
+ "Caching Demo 🗃️",
47
+ "Query Parameters 🔍",
48
+ "Character Gallery 🐾",
49
+ "CoTracker3 Demo 🕵️‍♂️"
50
+ ])
51
+
52
+ # Introduction Page
53
+ if options == "Introduction 🚀":
54
+ st.header("Introduction 🚀")
55
+ st.write("Hello there 👋")
56
+ st.write("Thanks for stopping by! Let's embark on a fun journey to learn Streamlit together!")
57
+ st.write("Here's a joke to start with:")
58
+ st.write("> Why did the programmer quit his job? Because he didn't get arrays! 😂")
59
+ st.image("https://media.giphy.com/media/3oEjI6SIIHBdRxXI40/giphy.gif")
60
+
61
+ # Upload Media Page
62
+ elif options == "Upload Media 📁":
63
+ st.header("Upload Media Files 📁")
64
+ st.write("You can upload images, videos, and audio files here, and we'll display them in galleries!")
65
+ uploaded_files = st.file_uploader("Choose media files", accept_multiple_files=True, type=["png", "jpg", "jpeg", "mp4", "mp3", "wav"])
66
+
67
+ if uploaded_files:
68
+ images = []
69
+ videos = []
70
+ audios = []
71
+ for uploaded_file in uploaded_files:
72
+ if uploaded_file.type.startswith("image/"):
73
+ images.append(uploaded_file)
74
+ elif uploaded_file.type.startswith("video/"):
75
+ videos.append(uploaded_file)
76
+ elif uploaded_file.type.startswith("audio/"):
77
+ audios.append(uploaded_file)
78
+
79
+ if images:
80
+ st.subheader("Image Gallery 🖼️")
81
+ cols = st.columns(3)
82
+ for idx, img in enumerate(images):
83
+ with cols[idx % 3]:
84
+ st.image(img, use_column_width=True)
85
+
86
+ if videos:
87
+ st.subheader("Video Gallery 🎥")
88
+ for vid in videos:
89
+ st.video(vid)
90
+
91
+ if audios:
92
+ st.subheader("Audio Gallery 🎵")
93
+ for aud in audios:
94
+ st.audio(aud)
95
+
96
+ # Animated Charts Page
97
+ elif options == "Animated Charts 📊":
98
+ st.header("Animated Charts 📊")
99
+ st.write("Watch the data come alive with live-updating charts!")
100
+
101
+ # Live-updating line chart
102
+ progress_bar = st.progress(0)
103
+ status_text = st.empty()
104
+ chart = st.line_chart(np.random.randn(10, 2))
105
+
106
+ for i in range(100):
107
+ new_rows = np.random.randn(1, 2)
108
+ status_text.text(f"Iteration {i+1}")
109
+ chart.add_rows(new_rows)
110
+ progress_bar.progress((i + 1) % 100)
111
+ time.sleep(0.1)
112
+ st.success("Animation Complete! 🎉")
113
+
114
+ # Caching Demo Page
115
+ elif options == "Caching Demo 🗃️":
116
+ st.header("Caching Demo 🗃️")
117
+ st.write("This demonstrates how caching can speed up your app.")
118
+
119
+ @st.cache_data
120
+ def expensive_computation(n):
121
+ time.sleep(2) # Simulate a long computation
122
+ return np.random.randn(n)
123
+
124
+ st.write("Click the button to compute some data.")
125
+ if st.button("Compute Data 🔄"):
126
+ with st.spinner("Computing..."):
127
+ data = expensive_computation(1000)
128
+ st.success("Computation Complete! 🎉")
129
+ st.line_chart(data)
130
+ else:
131
+ st.write("Awaiting your command...")
132
+
133
+ # Query Parameters Page
134
+ elif options == "Query Parameters 🔍":
135
+ st.header("Query Parameters 🔍")
136
+ st.write("You can control the app via URL query parameters!")
137
+ st.write("Try adding `?name=YourName` to the URL and see what happens.")
138
+
139
+ name = st.experimental_get_query_params().get("name", ["Stranger"])[0]
140
+ st.write(f"Hello, {name}! 👋")
141
+ st.write("Change the 'name' parameter in the URL to personalize this message.")
142
+
143
+ # Character Gallery Page
144
+ elif options == "Character Gallery 🐾":
145
+ st.header("Character Gallery 🐾")
146
+ st.write("Meet our delightful characters!")
147
+
148
+ characters = [
149
+ {"name": "Leo", "emoji": "🦁"},
150
+ {"name": "Mia", "emoji": "🐱"},
151
+ {"name": "Max", "emoji": "🐶"},
152
+ {"name": "Zoe", "emoji": "🦊"},
153
+ {"name": "Sam", "emoji": "🐵"},
154
+ {"name": "Lily", "emoji": "🐰"},
155
+ {"name": "Oscar", "emoji": "🐼"},
156
+ {"name": "Ella", "emoji": "🐨"},
157
+ {"name": "Jack", "emoji": "🐸"},
158
+ {"name": "Nina", "emoji": "🐙"},
159
+ {"name": "Charlie", "emoji": "🐵"},
160
+ {"name": "Daisy", "emoji": "🐷"},
161
+ {"name": "Felix", "emoji": "🐧"},
162
+ {"name": "Grace", "emoji": "🐮"},
163
+ {"name": "Henry", "emoji": "🐴"},
164
+ ]
165
+
166
+ random.shuffle(characters)
167
+ cols = st.columns(5)
168
+ for idx, character in enumerate(characters):
169
+ with cols[idx % 5]:
170
+ st.write(f"{character['emoji']} **{character['name']}**")
171
+
172
+ # CoTracker3 Demo Page
173
+ elif options == "CoTracker3 Demo 🕵️‍♂️":
174
+ st.header("CoTracker3 Demo 🕵️‍♂️")
175
+ st.write("This demo showcases point tracking using [CoTracker3](https://cotracker3.github.io/).")
176
+ st.write("Upload a video or select one of the example videos, then click **Track Points** to see CoTracker3 in action!")
177
+
178
+ # Example videos
179
+ example_videos = {
180
+ "Apple": "https://github.com/facebookresearch/co-tracker/raw/refs/heads/main/assets/apple.mp4",
181
+ "Bear": "https://github.com/facebookresearch/co-tracker/raw/refs/heads/main/assets/bear.mp4",
182
+ "Paragliding Launch": "https://github.com/facebookresearch/co-tracker/raw/refs/heads/main/assets/paragliding-launch.mp4",
183
+ "Paragliding": "https://github.com/facebookresearch/co-tracker/raw/refs/heads/main/assets/paragliding.mp4",
184
+ "Cat": "https://github.com/facebookresearch/co-tracker/raw/refs/heads/main/assets/cat.mp4",
185
+ "Pillow": "https://github.com/facebookresearch/co-tracker/raw/refs/heads/main/assets/pillow.mp4",
186
+ "Teddy": "https://github.com/facebookresearch/co-tracker/raw/refs/heads/main/assets/teddy.mp4",
187
+ "Backpack": "https://github.com/facebookresearch/co-tracker/raw/refs/heads/main/assets/backpack.mp4",
188
+ }
189
+
190
+ # Video uploader
191
+ uploaded_video = st.file_uploader("Upload a video (mp4 format)", type=["mp4"])
192
+
193
+ # Option to select example videos
194
+ selected_example = st.selectbox("Or select an example video", ["None"] + list(example_videos.keys()))
195
+
196
+ # Tracking button
197
+ if st.button("Track Points"):
198
+ with st.spinner("Processing..."):
199
+ # Load the video
200
+ if uploaded_video is not None:
201
+ # Process the uploaded video
202
+ video_bytes = uploaded_video.read()
203
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_file:
204
+ tmp_file.write(video_bytes)
205
+ tmp_video_path = tmp_file.name
206
+ frames = iio.imread(tmp_video_path, plugin="pyav")
207
+ elif selected_example != "None":
208
+ # Download and read the example video
209
+ video_url = example_videos[selected_example]
210
+ frames = iio.imread(video_url, plugin="pyav")
211
+ else:
212
+ st.warning("Please upload a video or select an example video.")
213
+ st.stop()
214
+
215
+ # Check if video is too long
216
+ FRAME_LIMIT = 300
217
+ if frames.shape[0] > FRAME_LIMIT:
218
+ frames = frames[:FRAME_LIMIT]
219
+ st.warning(f"Video is too long. Only the first {FRAME_LIMIT} frames will be processed.")
220
+
221
+ # Process with CoTracker3
222
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
223
+ grid_size = 10
224
+ video = torch.tensor(frames).permute(0, 3, 1, 2)[None].float().to(device) # B T C H W
225
+
226
+ # Run Offline CoTracker
227
+ cotracker = torch.hub.load("facebookresearch/co-tracker", "cotracker3_offline").to(device)
228
+ pred_tracks, pred_visibility = cotracker(video, grid_size=grid_size) # B T N 2, B T N 1
229
+
230
+ # Visualize the results
231
+ def get_colors(num_colors):
232
+ colors = []
233
+ for i in np.arange(0.0, 360.0, 360.0 / num_colors):
234
+ hue = i / 360.0
235
+ lightness = (50 + np.random.rand() * 10) / 100.0
236
+ saturation = (90 + np.random.rand() * 10) / 100.0
237
+ color = colorsys.hls_to_rgb(hue, lightness, saturation)
238
+ colors.append(
239
+ (int(color[0] * 255), int(color[1] * 255), int(color[2] * 255))
240
+ )
241
+ random.shuffle(colors)
242
+ return colors
243
+
244
+ def paint_point_track(frames, point_tracks, visibles, colormap=None):
245
+ num_points, num_frames = point_tracks.shape[0:2]
246
+ if colormap is None:
247
+ colormap = get_colors(num_colors=num_points)
248
+ height, width = frames.shape[1:3]
249
+ radius = int(round(min(height, width) * 0.015))
250
+ video = frames.copy()
251
+ for t in range(num_frames):
252
+ image = video[t]
253
+ for i in range(num_points):
254
+ x, y = point_tracks[i, t, :]
255
+ x = int(np.clip(x, 0, width - 1))
256
+ y = int(np.clip(y, 0, height - 1))
257
+ if visibles[i, t]:
258
+ cv2.circle(image, (x, y), radius, colormap[i], -1)
259
+ video[t] = image
260
+ return video
261
+
262
+ # Prepare data for visualization
263
+ pred_tracks = pred_tracks[0].cpu().numpy().transpose(1, 0, 2) # N x T x 2
264
+ pred_visibility = pred_visibility[0].cpu().numpy().transpose(1, 0) # N x T
265
+
266
+ output_frames = paint_point_track(frames, pred_tracks, pred_visibility)
267
+
268
+ # Save the output video
269
+ output_video_path = os.path.join(tempfile.gettempdir(), "output_video.mp4")
270
+ writer = imageio.get_writer(output_video_path, fps=25)
271
+ for frame in output_frames:
272
+ writer.append_data(frame)
273
+ writer.close()
274
+
275
+ # Display the output video
276
+ st.video(output_video_path)
277
+
278
+ st.success("Tracking Complete! 🎉")
279
+
280
+ # Footer with self-deprecating humor
281
+ st.write("If you find any bugs, remember they're just features in disguise! 🐞")