Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +67 -120
src/streamlit_app.py
CHANGED
|
@@ -1,11 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import io
|
| 3 |
import csv
|
| 4 |
-
import concurrent.futures
|
| 5 |
-
from segments import SegmentsClient
|
| 6 |
from datetime import datetime
|
| 7 |
-
import
|
| 8 |
-
import os
|
| 9 |
from get_labels_from_samples import (
|
| 10 |
get_samples as get_samples_objects,
|
| 11 |
export_frames_and_annotations,
|
|
@@ -47,95 +44,6 @@ def parse_classes(input_str: str) -> list:
|
|
| 47 |
return sorted(set(classes))
|
| 48 |
|
| 49 |
|
| 50 |
-
def _count_from_frames(frames, target_set):
|
| 51 |
-
"""Helper to count frames, total annotations, and matching annotations directly."""
|
| 52 |
-
if not frames:
|
| 53 |
-
return 0, 0, 0
|
| 54 |
-
num_frames = len(frames)
|
| 55 |
-
total_annotations = 0
|
| 56 |
-
matching_annotations = 0
|
| 57 |
-
for f in frames:
|
| 58 |
-
anns = getattr(f, 'annotations', [])
|
| 59 |
-
total_annotations += len(anns)
|
| 60 |
-
if target_set:
|
| 61 |
-
for ann in anns:
|
| 62 |
-
if getattr(ann, 'category_id', None) in target_set:
|
| 63 |
-
matching_annotations += 1
|
| 64 |
-
return num_frames, total_annotations, matching_annotations
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
def compute_metrics_for_sample(sample, api_key, target_set, is_multisensor, sensor_select):
|
| 68 |
-
"""
|
| 69 |
-
Fetch label for a single sample and compute metrics.
|
| 70 |
-
Returns a list of metric dicts (one per sensor if 'All sensors', otherwise one).
|
| 71 |
-
"""
|
| 72 |
-
try:
|
| 73 |
-
client = init_client(api_key)
|
| 74 |
-
label = client.get_label(sample.uuid)
|
| 75 |
-
labelset = getattr(label, 'labelset', '') or ''
|
| 76 |
-
labeled_by = getattr(label, 'created_by', '') or ''
|
| 77 |
-
reviewed_by = getattr(label, 'reviewed_by', '') or ''
|
| 78 |
-
|
| 79 |
-
metrics_rows = []
|
| 80 |
-
|
| 81 |
-
if is_multisensor:
|
| 82 |
-
sensors = getattr(getattr(label, 'attributes', None), 'sensors', None) or []
|
| 83 |
-
if sensor_select and sensor_select != 'All sensors':
|
| 84 |
-
# single sensor
|
| 85 |
-
for sensor in sensors:
|
| 86 |
-
if getattr(sensor, 'name', None) == sensor_select:
|
| 87 |
-
frames = getattr(getattr(sensor, 'attributes', None), 'frames', [])
|
| 88 |
-
num_frames, total_annotations, matching_annotations = _count_from_frames(frames, target_set)
|
| 89 |
-
metrics_rows.append({
|
| 90 |
-
'name': getattr(sample, 'name', sample.uuid),
|
| 91 |
-
'uuid': sample.uuid,
|
| 92 |
-
'labelset': labelset,
|
| 93 |
-
'sensor': sensor_select,
|
| 94 |
-
'num_frames': num_frames,
|
| 95 |
-
'total_annotations': total_annotations,
|
| 96 |
-
'matching_annotations': matching_annotations,
|
| 97 |
-
'labeled_by': labeled_by,
|
| 98 |
-
'reviewed_by': reviewed_by
|
| 99 |
-
})
|
| 100 |
-
break
|
| 101 |
-
else:
|
| 102 |
-
# all sensors
|
| 103 |
-
for sensor in sensors:
|
| 104 |
-
sensor_name = getattr(sensor, 'name', 'Unknown')
|
| 105 |
-
frames = getattr(getattr(sensor, 'attributes', None), 'frames', [])
|
| 106 |
-
num_frames, total_annotations, matching_annotations = _count_from_frames(frames, target_set)
|
| 107 |
-
metrics_rows.append({
|
| 108 |
-
'name': getattr(sample, 'name', sample.uuid),
|
| 109 |
-
'uuid': sample.uuid,
|
| 110 |
-
'labelset': labelset,
|
| 111 |
-
'sensor': sensor_name,
|
| 112 |
-
'num_frames': num_frames,
|
| 113 |
-
'total_annotations': total_annotations,
|
| 114 |
-
'matching_annotations': matching_annotations,
|
| 115 |
-
'labeled_by': labeled_by,
|
| 116 |
-
'reviewed_by': reviewed_by
|
| 117 |
-
})
|
| 118 |
-
else:
|
| 119 |
-
# single-sensor dataset
|
| 120 |
-
frames = getattr(getattr(label, 'attributes', None), 'frames', [])
|
| 121 |
-
num_frames, total_annotations, matching_annotations = _count_from_frames(frames, target_set)
|
| 122 |
-
metrics_rows.append({
|
| 123 |
-
'name': getattr(sample, 'name', sample.uuid),
|
| 124 |
-
'uuid': sample.uuid,
|
| 125 |
-
'labelset': labelset,
|
| 126 |
-
'sensor': '',
|
| 127 |
-
'num_frames': num_frames,
|
| 128 |
-
'total_annotations': total_annotations,
|
| 129 |
-
'matching_annotations': matching_annotations,
|
| 130 |
-
'labeled_by': labeled_by,
|
| 131 |
-
'reviewed_by': reviewed_by
|
| 132 |
-
})
|
| 133 |
-
|
| 134 |
-
return metrics_rows
|
| 135 |
-
except Exception:
|
| 136 |
-
return []
|
| 137 |
-
|
| 138 |
-
|
| 139 |
def generate_csv(metrics: list, dataset_identifier: str) -> str:
|
| 140 |
"""
|
| 141 |
Generate CSV content from list of per-sample metrics.
|
|
@@ -194,9 +102,6 @@ if api_key and dataset_identifier:
|
|
| 194 |
if is_multisensor:
|
| 195 |
sensor_select = st.selectbox("Choose sensor (optional)", options=['All sensors'] + sensor_names)
|
| 196 |
|
| 197 |
-
# Concurrency control
|
| 198 |
-
parallel_workers = st.slider("Parallel requests", min_value=1, max_value=32, value=8, help="Increase to speed up processing; lower if you hit API limits.")
|
| 199 |
-
|
| 200 |
if run_button:
|
| 201 |
st.session_state.csv_content = None
|
| 202 |
st.session_state.error = None
|
|
@@ -217,33 +122,75 @@ if run_button:
|
|
| 217 |
st.info("Checking dataset type...")
|
| 218 |
try:
|
| 219 |
target_classes = parse_classes(classes_input)
|
| 220 |
-
|
| 221 |
metrics = []
|
| 222 |
# Update loader after dataset type check
|
| 223 |
if status_ctx is not None:
|
| 224 |
status_ctx.update(label="Dataset type checked. Processing samples...", state="running")
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
if not metrics:
|
| 248 |
st.session_state.error = "No metrics could be generated for the dataset."
|
| 249 |
else:
|
|
@@ -266,4 +213,4 @@ if st.session_state.csv_content:
|
|
| 266 |
data=st.session_state.csv_content,
|
| 267 |
file_name=filename,
|
| 268 |
mime="text/csv"
|
| 269 |
-
)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import io
|
| 3 |
import csv
|
|
|
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
+
from segments import SegmentsClient
|
|
|
|
| 6 |
from get_labels_from_samples import (
|
| 7 |
get_samples as get_samples_objects,
|
| 8 |
export_frames_and_annotations,
|
|
|
|
| 44 |
return sorted(set(classes))
|
| 45 |
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def generate_csv(metrics: list, dataset_identifier: str) -> str:
|
| 48 |
"""
|
| 49 |
Generate CSV content from list of per-sample metrics.
|
|
|
|
| 102 |
if is_multisensor:
|
| 103 |
sensor_select = st.selectbox("Choose sensor (optional)", options=['All sensors'] + sensor_names)
|
| 104 |
|
|
|
|
|
|
|
|
|
|
| 105 |
if run_button:
|
| 106 |
st.session_state.csv_content = None
|
| 107 |
st.session_state.error = None
|
|
|
|
| 122 |
st.info("Checking dataset type...")
|
| 123 |
try:
|
| 124 |
target_classes = parse_classes(classes_input)
|
| 125 |
+
client = init_client(api_key)
|
| 126 |
metrics = []
|
| 127 |
# Update loader after dataset type check
|
| 128 |
if status_ctx is not None:
|
| 129 |
status_ctx.update(label="Dataset type checked. Processing samples...", state="running")
|
| 130 |
+
for sample in samples_objects:
|
| 131 |
+
try:
|
| 132 |
+
label = client.get_label(sample.uuid)
|
| 133 |
+
labelset = getattr(label, 'labelset', '') or ''
|
| 134 |
+
labeled_by = getattr(label, 'created_by', '') or ''
|
| 135 |
+
reviewed_by = getattr(label, 'reviewed_by', '') or ''
|
| 136 |
+
if is_multisensor and sensor_select and sensor_select != 'All sensors':
|
| 137 |
+
frames_list = export_sensor_frames_and_annotations(label, sensor_select)
|
| 138 |
+
sensor_val = sensor_select
|
| 139 |
+
num_frames = len(frames_list)
|
| 140 |
+
total_annotations = sum(len(f['annotations']) for f in frames_list)
|
| 141 |
+
matching_annotations = sum(
|
| 142 |
+
1
|
| 143 |
+
for f in frames_list
|
| 144 |
+
for ann in f['annotations']
|
| 145 |
+
if getattr(ann, 'category_id', None) in target_classes
|
| 146 |
+
)
|
| 147 |
+
elif is_multisensor and (not sensor_select or sensor_select == 'All sensors'):
|
| 148 |
+
all_sensor_frames = export_all_sensor_frames_and_annotations(label)
|
| 149 |
+
for sensor_name, frames_list in all_sensor_frames.items():
|
| 150 |
+
num_frames = len(frames_list)
|
| 151 |
+
total_annotations = sum(len(f['annotations']) for f in frames_list)
|
| 152 |
+
matching_annotations = sum(
|
| 153 |
+
1
|
| 154 |
+
for f in frames_list
|
| 155 |
+
for ann in f['annotations']
|
| 156 |
+
if getattr(ann, 'category_id', None) in target_classes
|
| 157 |
+
)
|
| 158 |
+
metrics.append({
|
| 159 |
+
'name': getattr(sample, 'name', sample.uuid),
|
| 160 |
+
'uuid': sample.uuid,
|
| 161 |
+
'labelset': labelset,
|
| 162 |
+
'sensor': sensor_name,
|
| 163 |
+
'num_frames': num_frames,
|
| 164 |
+
'total_annotations': total_annotations,
|
| 165 |
+
'matching_annotations': matching_annotations,
|
| 166 |
+
'labeled_by': labeled_by,
|
| 167 |
+
'reviewed_by': reviewed_by
|
| 168 |
+
})
|
| 169 |
+
continue
|
| 170 |
+
else:
|
| 171 |
+
frames_list = export_frames_and_annotations(label)
|
| 172 |
+
sensor_val = ''
|
| 173 |
+
num_frames = len(frames_list)
|
| 174 |
+
total_annotations = sum(len(f['annotations']) for f in frames_list)
|
| 175 |
+
matching_annotations = sum(
|
| 176 |
+
1
|
| 177 |
+
for f in frames_list
|
| 178 |
+
for ann in f['annotations']
|
| 179 |
+
if getattr(ann, 'category_id', None) in target_classes
|
| 180 |
+
)
|
| 181 |
+
metrics.append({
|
| 182 |
+
'name': getattr(sample, 'name', sample.uuid),
|
| 183 |
+
'uuid': sample.uuid,
|
| 184 |
+
'labelset': labelset,
|
| 185 |
+
'sensor': sensor_val if is_multisensor else '',
|
| 186 |
+
'num_frames': num_frames,
|
| 187 |
+
'total_annotations': total_annotations,
|
| 188 |
+
'matching_annotations': matching_annotations,
|
| 189 |
+
'labeled_by': labeled_by,
|
| 190 |
+
'reviewed_by': reviewed_by
|
| 191 |
+
})
|
| 192 |
+
except Exception as e:
|
| 193 |
+
continue
|
| 194 |
if not metrics:
|
| 195 |
st.session_state.error = "No metrics could be generated for the dataset."
|
| 196 |
else:
|
|
|
|
| 213 |
data=st.session_state.csv_content,
|
| 214 |
file_name=filename,
|
| 215 |
mime="text/csv"
|
| 216 |
+
)
|