ncoop57
commited on
Commit
•
c40e192
1
Parent(s):
fd2744e
Update app layout and change to streaming only max 2mins of video instead of downloading whole thing using ffmpeg
Browse files
app.py
CHANGED
@@ -27,38 +27,38 @@ def get_embedding(model, query, video):
|
|
27 |
|
28 |
return text_emb, img_embs
|
29 |
|
30 |
-
def
|
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 |
-
def run():
|
58 |
-
st.set_page_config(page_title="Youtube CLIFS")
|
59 |
-
# main body
|
60 |
-
model = get_model()
|
61 |
-
|
62 |
st.sidebar.markdown("### Controls:")
|
63 |
top_k = st.sidebar.slider(
|
64 |
"Top K",
|
@@ -80,10 +80,33 @@ def run():
|
|
80 |
submit_button = st.sidebar.button("Search")
|
81 |
if submit_button:
|
82 |
text = st.text("Downloading video...")
|
83 |
-
hook = lambda d: my_hook(d,
|
84 |
-
ydl_opts = {"format": "mp4[height=360]"
|
85 |
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
86 |
-
ydl.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
|
89 |
if __name__ == "__main__":
|
|
|
27 |
|
28 |
return text_emb, img_embs
|
29 |
|
30 |
+
def find_frames(url, model, desc, top_k, text):
|
31 |
+
text.text("Processing video...")
|
32 |
+
probe = ffmpeg.probe(url)
|
33 |
+
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
|
34 |
+
width = int(video_stream['width'])
|
35 |
+
height = int(video_stream['height'])
|
36 |
+
out, _ = (
|
37 |
+
ffmpeg
|
38 |
+
.input(url, t=120)
|
39 |
+
.output('pipe:', format='rawvideo', pix_fmt='rgb24')
|
40 |
+
.run(capture_stdout=True)
|
41 |
+
)
|
42 |
+
video = (
|
43 |
+
np
|
44 |
+
.frombuffer(out, np.uint8)
|
45 |
+
.reshape([-1, height, width, 3])
|
46 |
+
)[::10]
|
47 |
+
|
48 |
+
txt_embd, img_embds = get_embedding(model, desc, video)
|
49 |
+
cos_scores = np.array(util.cos_sim(txt_embd, img_embds))
|
50 |
+
ids = np.argsort(cos_scores)[0][-top_k:]
|
51 |
+
|
52 |
+
imgs = [Image.fromarray(video[i]) for i in ids]
|
53 |
+
text.empty()
|
54 |
+
st.image(imgs)
|
55 |
+
|
56 |
+
def main_page(model):
|
57 |
+
st.title("Introducing Youtube CLIFS")
|
58 |
+
|
59 |
+
def clifs_page(model):
|
60 |
+
st.title("CLIFS")
|
61 |
|
|
|
|
|
|
|
|
|
|
|
62 |
st.sidebar.markdown("### Controls:")
|
63 |
top_k = st.sidebar.slider(
|
64 |
"Top K",
|
|
|
80 |
submit_button = st.sidebar.button("Search")
|
81 |
if submit_button:
|
82 |
text = st.text("Downloading video...")
|
83 |
+
hook = lambda d: my_hook(d, )
|
84 |
+
ydl_opts = {"format": "mp4[height=360]"}
|
85 |
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
86 |
+
info_dict = ydl.extract_info(url, download=False)
|
87 |
+
video_url = info_dict.get("url", None)
|
88 |
+
find_frames(video_url, model, desc, top_k, text)
|
89 |
+
print(video_url)
|
90 |
+
# ydl.download([url])
|
91 |
+
|
92 |
+
PAGES = {
|
93 |
+
"Home": main_page,
|
94 |
+
"CLIFS": clifs_page
|
95 |
+
}
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
def run():
|
100 |
+
st.set_page_config(page_title="Youtube CLIFS")
|
101 |
+
# main body
|
102 |
+
model = get_model()
|
103 |
+
|
104 |
+
st.sidebar.title('Navigation')
|
105 |
+
selection = st.sidebar.radio("Go to", list(PAGES.keys()))
|
106 |
+
|
107 |
+
page = PAGES[selection](model)
|
108 |
+
|
109 |
+
|
110 |
|
111 |
|
112 |
if __name__ == "__main__":
|