-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
167 lines (129 loc) · 4.79 KB
/
Copy pathapp.py
File metadata and controls
167 lines (129 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import sys
import threading
import time
from pathlib import Path
import streamlit as st
from PIL import Image
from config import (
DEFAULT_DATA_DIR,
DEFAULT_FPS,
DEFAULT_SIMILARITY_THRESHOLD,
DEFAULT_VIDEO_PATH,
IMAGE_PATH_KEY,
IMAGES_DIR_NAME,
QDRANT_STORAGE_DIR_NAME,
SEARCH_LIMIT,
)
from glasses_x_edge.capture import VideoCapture
from glasses_x_edge.embedding import CrossModalEncoder
from glasses_x_edge.storage import VisionStorage
HIDDEN_CONTROLS_CSS = """
<style>
video { pointer-events: none; }
</style>
"""
class SystemState:
def __init__(self):
self.is_running = True
self.images_dir = Path(DEFAULT_DATA_DIR) / IMAGES_DIR_NAME
self.images_dir.mkdir(parents=True, exist_ok=True)
self.storage = VisionStorage(Path(DEFAULT_DATA_DIR) / QDRANT_STORAGE_DIR_NAME)
self.storage.initialize()
self.encoder = CrossModalEncoder()
self.encoder.load_models()
threading.Thread(target=self.index_video_background, daemon=True).start()
def index_video_background(self):
cap = VideoCapture(str(DEFAULT_VIDEO_PATH), fps=DEFAULT_FPS)
last_frame = None
with cap:
for frame in cap.capture_continuous():
if not self.is_running:
break
# Skip similar frames to save space
if last_frame is not None:
if (
VideoCapture.calculate_similarity(frame, last_frame)
> DEFAULT_SIMILARITY_THRESHOLD
):
continue
last_frame = frame
timestamp = int(time.time() * 1000)
image_path = self.images_dir / f"frame_{timestamp}.jpg"
cap.save_frame(frame, image_path)
embedding = self.encoder.encode_image(image_path)
self.storage.store_image(image_path, embedding)
@st.cache_resource
def get_system_state():
return SystemState()
@st.fragment
def render_search_interface(system):
st.header("Smart 🕶️ X Qdrant Edge")
query = st.text_input("Search for what you saw")
if query:
text_embedding = system.encoder.encode_text(query)
results = system.storage.search_similar(text_embedding, limit=SEARCH_LIMIT)
if not results:
st.info("No matching frames found.")
return
for result in results:
image_path = Path(result[IMAGE_PATH_KEY])
st.image(
Image.open(image_path),
width="stretch",
)
@st.fragment(run_every=2)
def render_sync_status(storage):
q_size = storage.upload_queue.size
st.metric("Upload Queue", q_size)
@st.fragment
def render_snapshot_restore(storage):
if st.button("🌜 Incremental Sync", use_container_width=True):
try:
with st.spinner("Syncing..."):
storage.sync_from_server()
st.success("Synced!")
except Exception as e:
st.error(f"{e}")
if st.button("🌕 Full Sync", use_container_width=True):
try:
with st.spinner("Syncing..."):
storage.full_sync_from_server()
st.success("Synced!")
except Exception as e:
st.error(f"{e}")
with st.expander("What is this?"):
st.markdown(
"Initially, the glasses store vectors unindexed to save CPU. "
"The server builds the HNSW index for fast search.\n\n"
"Syncing downloads the indexed snapshot from the server.\n\n"
"🌜 Incremental Sync\n\n"
"Downloads a partial snapshot with only the new updates. Requires a prior full sync.\n\n"
"🌕 Full Sync\n\n"
"Downloads the complete indexed snapshot from the server."
)
def main():
st.set_page_config(page_title="Qdrant Edge Demo", page_icon="👓", layout="wide")
st.markdown(HIDDEN_CONTROLS_CSS, unsafe_allow_html=True)
system = get_system_state()
with st.sidebar:
st.header("Server Sync Status")
render_sync_status(system.storage)
st.header("Server Sync")
render_snapshot_restore(system.storage)
col_left, col_right = st.columns([1, 1])
with col_right:
st.subheader("You are seeing this")
if Path(DEFAULT_VIDEO_PATH).exists():
st.video(str(DEFAULT_VIDEO_PATH), autoplay=True, muted=True)
st.info("This video is being indexed in real-time.")
else:
st.error(f"Video file not found: {DEFAULT_VIDEO_PATH}")
with col_left:
render_search_interface(system)
if __name__ == "__main__":
from streamlit.web import cli as stcli
if st.runtime.exists():
main()
else:
sys.argv = ["streamlit", "run", sys.argv[0]]
sys.exit(stcli.main())