Spaces:
Runtime error
Runtime error
sradc
commited on
Commit
·
4343947
1
Parent(s):
44efe1c
create .gitignore in image folder, and include base64 image in parquet, app.py use base64 image to display
Browse files- pipeline/process_videos.py +8 -2
- video_semantic_search/app.py +4 -10
pipeline/process_videos.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import pandas as pd
|
| 3 |
from PIL import Image
|
|
@@ -12,6 +14,8 @@ DATAFRAME_PATH = DATA_DIR / "dataset.parquet"
|
|
| 12 |
|
| 13 |
|
| 14 |
def process_videos() -> None:
|
|
|
|
|
|
|
| 15 |
"Runs clip on video frames, saves results to a parquet file"
|
| 16 |
clip_wrapper = ClipWrapper()
|
| 17 |
results = []
|
|
@@ -27,19 +31,21 @@ def process_videos() -> None:
|
|
| 27 |
):
|
| 28 |
image_path = extracted_images_dir / f"{frame_idx}.jpg"
|
| 29 |
image.save(image_path)
|
|
|
|
|
|
|
| 30 |
results.append(
|
| 31 |
[
|
| 32 |
video_id,
|
| 33 |
frame_idx,
|
| 34 |
timestamp_secs,
|
| 35 |
-
|
| 36 |
*clip_vector,
|
| 37 |
]
|
| 38 |
)
|
| 39 |
complete_file.touch()
|
| 40 |
df = pd.DataFrame(
|
| 41 |
results,
|
| 42 |
-
columns=["video_id", "frame_idx", "timestamp", "
|
| 43 |
+ [f"dim_{i}" for i in range(MODEL_DIM)],
|
| 44 |
)
|
| 45 |
print(f"Saving data to {DATAFRAME_PATH}")
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
|
| 3 |
import cv2
|
| 4 |
import pandas as pd
|
| 5 |
from PIL import Image
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
def process_videos() -> None:
|
| 17 |
+
IMAGES_DIR.mkdir(exist_ok=True, parents=True)
|
| 18 |
+
(IMAGES_DIR / ".gitignore").write_text("*")
|
| 19 |
"Runs clip on video frames, saves results to a parquet file"
|
| 20 |
clip_wrapper = ClipWrapper()
|
| 21 |
results = []
|
|
|
|
| 31 |
):
|
| 32 |
image_path = extracted_images_dir / f"{frame_idx}.jpg"
|
| 33 |
image.save(image_path)
|
| 34 |
+
with open(image_path, "rb") as f:
|
| 35 |
+
encoded_image = base64.b64encode(f.read()).decode()
|
| 36 |
results.append(
|
| 37 |
[
|
| 38 |
video_id,
|
| 39 |
frame_idx,
|
| 40 |
timestamp_secs,
|
| 41 |
+
encoded_image,
|
| 42 |
*clip_vector,
|
| 43 |
]
|
| 44 |
)
|
| 45 |
complete_file.touch()
|
| 46 |
df = pd.DataFrame(
|
| 47 |
results,
|
| 48 |
+
columns=["video_id", "frame_idx", "timestamp", "base64_image"]
|
| 49 |
+ [f"dim_{i}" for i in range(MODEL_DIM)],
|
| 50 |
)
|
| 51 |
print(f"Saving data to {DATAFRAME_PATH}")
|
video_semantic_search/app.py
CHANGED
|
@@ -30,6 +30,7 @@ class SemanticSearcher:
|
|
| 30 |
video_id=row["video_id"],
|
| 31 |
frame_idx=row["frame_idx"],
|
| 32 |
timestamp=row["timestamp"],
|
|
|
|
| 33 |
score=score,
|
| 34 |
)
|
| 35 |
for score, (_, row) in zip(D[0], self.metadata.iloc[I[0]].iterrows())
|
|
@@ -45,10 +46,12 @@ class SearchResult:
|
|
| 45 |
video_id: str
|
| 46 |
frame_idx: int
|
| 47 |
timestamp: float
|
|
|
|
| 48 |
score: float
|
| 49 |
|
| 50 |
|
| 51 |
def get_video_url(video_id: str, timestamp: float) -> str:
|
|
|
|
| 52 |
return f"https://www.youtube.com/watch?v={video_id}&t={int(timestamp)}"
|
| 53 |
|
| 54 |
|
|
@@ -87,19 +90,10 @@ def display_search_results(results: list[SearchResult]) -> None:
|
|
| 87 |
""",
|
| 88 |
unsafe_allow_html=True,
|
| 89 |
)
|
| 90 |
-
|
| 91 |
-
# Display the embedded YouTube video
|
| 92 |
-
# st.video(get_video_url(result.video_id), start_time=int(result.timestamp))
|
| 93 |
-
# st.image(f"data/images/{result.video_id}/{result.frame_idx}.jpg")
|
| 94 |
-
with open(
|
| 95 |
-
f"data/images/{result.video_id}/{result.frame_idx}.jpg", "rb"
|
| 96 |
-
) as f:
|
| 97 |
-
image = f.read()
|
| 98 |
-
encoded = base64.b64encode(image).decode()
|
| 99 |
st.markdown(
|
| 100 |
f"""
|
| 101 |
<a href="{get_video_url(result.video_id, result.timestamp)}">
|
| 102 |
-
<img src="data:image/jpeg;base64,{
|
| 103 |
</a>
|
| 104 |
""",
|
| 105 |
unsafe_allow_html=True,
|
|
|
|
| 30 |
video_id=row["video_id"],
|
| 31 |
frame_idx=row["frame_idx"],
|
| 32 |
timestamp=row["timestamp"],
|
| 33 |
+
base64_image=row["base64_image"],
|
| 34 |
score=score,
|
| 35 |
)
|
| 36 |
for score, (_, row) in zip(D[0], self.metadata.iloc[I[0]].iterrows())
|
|
|
|
| 46 |
video_id: str
|
| 47 |
frame_idx: int
|
| 48 |
timestamp: float
|
| 49 |
+
base64_image: str
|
| 50 |
score: float
|
| 51 |
|
| 52 |
|
| 53 |
def get_video_url(video_id: str, timestamp: float) -> str:
|
| 54 |
+
timestamp = max(0, timestamp - 3) # Show 3 seconds before the query
|
| 55 |
return f"https://www.youtube.com/watch?v={video_id}&t={int(timestamp)}"
|
| 56 |
|
| 57 |
|
|
|
|
| 90 |
""",
|
| 91 |
unsafe_allow_html=True,
|
| 92 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
st.markdown(
|
| 94 |
f"""
|
| 95 |
<a href="{get_video_url(result.video_id, result.timestamp)}">
|
| 96 |
+
<img src="data:image/jpeg;base64,{result.base64_image}" alt="frame {result.frame_idx} timestamp {int(result.timestamp)}" width="100%">
|
| 97 |
</a>
|
| 98 |
""",
|
| 99 |
unsafe_allow_html=True,
|