Update src/streamlit_app.py
Browse files- src/streamlit_app.py +60 -32
src/streamlit_app.py
CHANGED
@@ -1,40 +1,68 @@
|
|
1 |
-
import altair as alt
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
.encode(
|
36 |
-
x=alt.X("x", axis=None),
|
37 |
-
y=alt.Y("y", axis=None),
|
38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
40 |
-
))
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from segments import SegmentsClient
|
3 |
+
import sys
|
4 |
+
import copy
|
5 |
+
import os
|
6 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../0_label_scripts")))
|
7 |
+
from get_labels_from_samples import export_all_sensor_frames_and_annotations
|
8 |
|
9 |
+
# ---------------- Streamlit UI ----------------
|
10 |
+
st.title("Multi-Sensor: Overwrite Target Frame Annotations with Source Frame (Cuboids)")
|
11 |
|
12 |
+
api_key = st.text_input("API Key", type="password")
|
13 |
+
source_uuid = st.text_input("Source UUID", value="")
|
14 |
+
target_uuid = st.text_input("Target UUID", value="")
|
15 |
+
source_frame_num = st.number_input("Source Frame Number (1-indexed)", min_value=1, value=50, step=1)
|
16 |
+
target_frame_num = st.number_input("Target Frame Number (1-indexed)", min_value=1, value=1, step=1)
|
17 |
|
18 |
+
if st.button("Overwrite Target Frame Annotations"):
|
19 |
+
if not api_key or not source_uuid or not target_uuid:
|
20 |
+
st.error("Please fill in all fields.")
|
21 |
+
else:
|
22 |
+
try:
|
23 |
+
client = SegmentsClient(api_key)
|
24 |
+
# Fetch source and target labels
|
25 |
+
source_label = client.get_label(source_uuid)
|
26 |
+
target_label = client.get_label(target_uuid)
|
27 |
|
28 |
+
# Extract all sensor frames/annotations
|
29 |
+
source_sensors = export_all_sensor_frames_and_annotations(source_label)
|
30 |
+
target_sensors = export_all_sensor_frames_and_annotations(target_label)
|
31 |
|
32 |
+
# Defensive copy
|
33 |
+
new_target_label = copy.deepcopy(target_label)
|
34 |
+
sensors_attr = getattr(new_target_label.attributes, "sensors", None)
|
35 |
+
if sensors_attr is None:
|
36 |
+
st.error("Target label has no sensors.")
|
37 |
+
st.stop()
|
38 |
|
39 |
+
# For each sensor in both source and target
|
40 |
+
for sensor_idx, sensor in enumerate(sensors_attr):
|
41 |
+
sensor_name = getattr(sensor, "name", None)
|
42 |
+
if not sensor_name or sensor_name not in source_sensors:
|
43 |
+
continue # skip sensors not found in source
|
44 |
+
source_frames = source_sensors[sensor_name]
|
45 |
+
target_frames = target_sensors.get(sensor_name, [])
|
46 |
+
# Frame indices are 1-indexed for user, 0-indexed in list
|
47 |
+
src_idx = source_frame_num - 1
|
48 |
+
tgt_idx = target_frame_num - 1
|
49 |
+
if src_idx >= len(source_frames) or tgt_idx >= len(target_frames):
|
50 |
+
st.warning(f"Sensor '{sensor_name}': Frame index out of range. Skipped.")
|
51 |
+
continue
|
52 |
+
# Overwrite target frame's annotations with source frame's annotations
|
53 |
+
src_anns = source_frames[src_idx]["annotations"]
|
54 |
+
tgt_frame = getattr(sensor.attributes.frames[tgt_idx], "annotations", None)
|
55 |
+
if tgt_frame is not None:
|
56 |
+
sensor.attributes.frames[tgt_idx].annotations = copy.deepcopy(src_anns)
|
57 |
+
else:
|
58 |
+
st.warning(f"Sensor '{sensor_name}': Could not access target frame annotations.")
|
59 |
|
60 |
+
# Upload the updated label to Segments.ai (only the specified frames/annotations are changed)
|
61 |
+
client.update_label(
|
62 |
+
target_uuid,
|
63 |
+
labelset="ground-truth", # Change this if you use a different labelset name
|
64 |
+
attributes=new_target_label.attributes.model_dump()
|
65 |
+
)
|
66 |
+
st.success("Target label updated and uploaded to Segments.ai!")
|
67 |
+
except Exception as e:
|
68 |
+
st.error(f"Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|