@luxonis/depthai-viewer-common 2.5.2 → 2.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,156 +3,8 @@
3
3
  set -euo pipefail
4
4
 
5
5
  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6
- BENCHMARKS_DIR="${SCRIPT_DIR}/src/utils/benchmarks"
7
- MAX_DOWNLOAD_ATTEMPTS="${BENCHMARK_DOWNLOAD_RETRIES:-3}"
8
-
9
- BENCHMARKS=(
10
- "bgr888i-stream"
11
- "encoded-stream"
12
- "nv12-stream"
13
- "pointcloud"
14
- )
15
-
16
- BENCHMARK_ASSETS=(
17
- "bgr888i-stream|bgr888i_stream.pkl|https://oak-viewer-releases.luxonis.com/benchmark/bgr888i-stream/bgr888i_stream.pkl"
18
- "encoded-stream|encoded_stream.pkl|https://oak-viewer-releases.luxonis.com/benchmark/encoded-stream/encoded_stream.pkl"
19
- "nv12-stream|nv12_stream.pkl|https://oak-viewer-releases.luxonis.com/benchmark/nv12-stream/nv12_stream.pkl"
20
- "pointcloud|encoded_stream.pkl|https://oak-viewer-releases.luxonis.com/benchmark/pointcloud/encoded_stream.pkl"
21
- "pointcloud|depth_stream.pkl|https://oak-viewer-releases.luxonis.com/benchmark/pointcloud/depth_stream.pkl"
22
- )
23
6
 
24
7
  trap 'exit 130' INT
25
8
  trap 'exit 143' TERM
26
9
 
27
- log() {
28
- printf '[benchmark-download] %s\n' "$1"
29
- }
30
-
31
- file_size() {
32
- local file_path=$1
33
- stat -f%z "${file_path}" 2>/dev/null || stat -c%s "${file_path}"
34
- }
35
-
36
- install_requirements() {
37
- local requirements_file="${BENCHMARKS_DIR}/requirements.txt"
38
- log "Installing Python dependencies from ${requirements_file}"
39
- python3 -m pip install --quiet -r "${requirements_file}"
40
- }
41
-
42
- get_remote_file_size() {
43
- local url=$1
44
- curl --head --silent --show-error --location --fail "${url}" |
45
- awk 'BEGIN { IGNORECASE = 1 } /^content-length:/ { gsub("\r", "", $2); print $2; exit }'
46
- }
47
-
48
- download_asset() {
49
- local benchmark_id=$1
50
- local file_name=$2
51
- local url=$3
52
- local benchmark_dir="${BENCHMARKS_DIR}/${benchmark_id}"
53
- local output_path="${benchmark_dir}/${file_name}"
54
- local partial_path="${output_path}.part"
55
- local expected_size=""
56
- local final_size=""
57
- local partial_size=""
58
- local attempt=1
59
- local curl_args=(
60
- --fail
61
- --location
62
- --progress-bar
63
- --output "${partial_path}"
64
- "${url}"
65
- )
66
-
67
- expected_size="$(get_remote_file_size "${url}" || true)"
68
-
69
- if [[ -f "${output_path}" ]]; then
70
- if [[ -n "${expected_size}" ]]; then
71
- final_size="$(file_size "${output_path}")"
72
- if [[ "${final_size}" == "${expected_size}" ]]; then
73
- log "Using existing asset ${file_name} for ${benchmark_id}"
74
- return
75
- fi
76
-
77
- log "Existing file size mismatch for ${file_name}: found ${final_size} bytes, expected ${expected_size} bytes. Preparing to resume..."
78
- else
79
- log "Using existing asset ${file_name} for ${benchmark_id}"
80
- return
81
- fi
82
-
83
- if [[ -f "${partial_path}" ]]; then
84
- partial_size="$(file_size "${partial_path}")"
85
- if [[ "${partial_size}" -lt "${final_size}" ]]; then
86
- rm -f "${partial_path}"
87
- mv "${output_path}" "${partial_path}"
88
- else
89
- rm -f "${output_path}"
90
- fi
91
- else
92
- mv "${output_path}" "${partial_path}"
93
- fi
94
- fi
95
-
96
- if [[ -f "${partial_path}" ]]; then
97
- if [[ -n "${expected_size}" ]]; then
98
- partial_size="$(file_size "${partial_path}")"
99
- if [[ "${partial_size}" == "${expected_size}" ]]; then
100
- mv "${partial_path}" "${output_path}"
101
- log "Recovered completed asset ${file_name} for ${benchmark_id}"
102
- return
103
- fi
104
-
105
- if [[ "${partial_size}" -gt "${expected_size}" ]]; then
106
- log "Partial download for ${file_name} is larger than expected. Restarting download..."
107
- rm -f "${partial_path}"
108
- else
109
- log "Resuming ${file_name} for ${benchmark_id} from ${partial_size} of ${expected_size} bytes"
110
- fi
111
- else
112
- log "Resuming ${file_name} for ${benchmark_id} from existing partial file"
113
- fi
114
-
115
- curl_args=(--continue-at - "${curl_args[@]}")
116
- else
117
- log "Downloading ${file_name} for ${benchmark_id}"
118
- fi
119
-
120
- while (( attempt <= MAX_DOWNLOAD_ATTEMPTS )); do
121
- if curl "${curl_args[@]}"; then
122
- if [[ -n "${expected_size}" ]]; then
123
- partial_size="$(file_size "${partial_path}")"
124
- if [[ "${partial_size}" != "${expected_size}" ]]; then
125
- log "Downloaded size mismatch for ${file_name}: expected ${expected_size} bytes, got ${partial_size} bytes"
126
- else
127
- mv "${partial_path}" "${output_path}"
128
- log "Download completed: ${file_name}"
129
- return
130
- fi
131
- else
132
- mv "${partial_path}" "${output_path}"
133
- log "Download completed: ${file_name}"
134
- return
135
- fi
136
- fi
137
-
138
- if (( attempt == MAX_DOWNLOAD_ATTEMPTS )); then
139
- log "Download failed for ${file_name} after ${MAX_DOWNLOAD_ATTEMPTS} attempts"
140
- return 1
141
- fi
142
-
143
- log "Retrying ${file_name} (${attempt}/${MAX_DOWNLOAD_ATTEMPTS})..."
144
- attempt=$((attempt + 1))
145
- done
146
- }
147
-
148
- log 'Upgrading pip'
149
- python3 -m pip install --quiet --upgrade pip
150
-
151
- install_requirements
152
-
153
- for benchmark_asset in "${BENCHMARK_ASSETS[@]}"; do
154
- IFS='|' read -r benchmark_id file_name url <<<"${benchmark_asset}"
155
- download_asset "${benchmark_id}" "${file_name}" "${url}"
156
- done
157
-
158
- log 'Benchmark assets are ready'
10
+ exec "${SCRIPT_DIR}/benchmark.sh" download "$@"
@@ -0,0 +1,336 @@
1
+ #!/usr/bin/env bash
2
+
3
+ BENCHMARK_GUM_ENABLED=0
4
+
5
+ BENCHMARK_DEFINITIONS=(
6
+ "bgr888i-stream|BGR888i Stream"
7
+ "encoded-stream|H.264 Stream"
8
+ "nv12-stream|NV12 Stream"
9
+ "pointcloud|Point Cloud"
10
+ )
11
+
12
+ BENCHMARK_ASSET_DEFINITIONS=(
13
+ "bgr888i-stream|bgr888i_stream.pkl|https://oak-viewer-releases.luxonis.com/benchmark/bgr888i-stream/bgr888i_stream.pkl"
14
+ "encoded-stream|encoded_stream.pkl|https://oak-viewer-releases.luxonis.com/benchmark/encoded-stream/encoded_stream.pkl"
15
+ "nv12-stream|nv12_stream.pkl|https://oak-viewer-releases.luxonis.com/benchmark/nv12-stream/nv12_stream.pkl"
16
+ "pointcloud|encoded_stream.pkl|https://oak-viewer-releases.luxonis.com/benchmark/pointcloud/encoded_stream.pkl"
17
+ "pointcloud|depth_stream.pkl|https://oak-viewer-releases.luxonis.com/benchmark/pointcloud/depth_stream.pkl"
18
+ )
19
+
20
+ BENCHMARK_SELECTED_IDS=()
21
+
22
+ benchmark_init_ui() {
23
+ local use_gum="${BENCHMARK_USE_GUM:-auto}"
24
+
25
+ BENCHMARK_GUM_ENABLED=0
26
+
27
+ case "${use_gum}" in
28
+ 0|false|FALSE|False|no|NO|No)
29
+ return
30
+ ;;
31
+ esac
32
+
33
+ if ! command -v gum >/dev/null 2>&1; then
34
+ return
35
+ fi
36
+
37
+ if [[ "${use_gum}" == 'auto' ]] && [[ ! -t 1 ]]; then
38
+ return
39
+ fi
40
+
41
+ BENCHMARK_GUM_ENABLED=1
42
+ }
43
+
44
+ benchmark_trim() {
45
+ local value=$1
46
+ value="${value#"${value%%[![:space:]]*}"}"
47
+ value="${value%"${value##*[![:space:]]}"}"
48
+ printf '%s' "${value}"
49
+ }
50
+
51
+ benchmark_join_by() {
52
+ local separator=$1
53
+ shift
54
+
55
+ if [[ $# -eq 0 ]]; then
56
+ return
57
+ fi
58
+
59
+ local result=$1
60
+ shift
61
+
62
+ for item in "$@"; do
63
+ result+="${separator}${item}"
64
+ done
65
+
66
+ printf '%s' "${result}"
67
+ }
68
+
69
+ benchmark_selected_ids_display() {
70
+ if (( ${#BENCHMARK_SELECTED_IDS[@]} == 0 )); then
71
+ return
72
+ fi
73
+
74
+ benchmark_join_by ', ' "${BENCHMARK_SELECTED_IDS[@]}"
75
+ }
76
+
77
+ benchmark_log() {
78
+ local level=$1
79
+ local message=$2
80
+ local gum_level='info'
81
+
82
+ case "${level}" in
83
+ info)
84
+ gum_level='info'
85
+ ;;
86
+ success)
87
+ gum_level='info'
88
+ ;;
89
+ warn)
90
+ gum_level='warn'
91
+ ;;
92
+ error)
93
+ gum_level='error'
94
+ ;;
95
+ esac
96
+
97
+ if (( BENCHMARK_GUM_ENABLED )) && [[ -t 1 ]]; then
98
+ case "${level}" in
99
+ success)
100
+ gum log --level "${gum_level}" "Done: ${message}"
101
+ ;;
102
+ *)
103
+ gum log --level "${gum_level}" "${message}"
104
+ ;;
105
+ esac
106
+ return
107
+ fi
108
+
109
+ printf '[benchmark] %s\n' "${message}"
110
+ }
111
+
112
+ benchmark_print_banner() {
113
+ local title=$1
114
+ local details=${2:-}
115
+
116
+ if (( BENCHMARK_GUM_ENABLED )) && [[ -t 1 ]]; then
117
+ if [[ -n "${details}" ]]; then
118
+ gum style \
119
+ --border rounded \
120
+ --border-foreground 39 \
121
+ --padding '0 1' \
122
+ --margin '1 0' \
123
+ "$(printf '%s\n%s' "${title}" "${details}")"
124
+ else
125
+ gum style \
126
+ --border rounded \
127
+ --border-foreground 39 \
128
+ --padding '0 1' \
129
+ --margin '1 0' \
130
+ "${title}"
131
+ fi
132
+ return
133
+ fi
134
+
135
+ printf '[benchmark] %s\n' "${title}"
136
+ if [[ -n "${details}" ]]; then
137
+ printf '%s\n' "${details}"
138
+ fi
139
+ }
140
+
141
+ benchmark_print_selection_summary() {
142
+ local mode=$1
143
+ local resolution=${2:-}
144
+ local selected_display
145
+ local details
146
+
147
+ selected_display="$(benchmark_selected_ids_display)"
148
+ details="$(printf 'Mode: %s\nBenchmarks: %s' "${mode}" "${selected_display}")"
149
+
150
+ if [[ -n "${resolution}" ]]; then
151
+ details+="
152
+ Resolution: ${resolution}"
153
+ fi
154
+
155
+ benchmark_print_banner 'DepthAI Benchmark CLI' "${details}"
156
+ }
157
+
158
+ benchmark_run_with_spinner() {
159
+ local title=$1
160
+ shift
161
+
162
+ if (( BENCHMARK_GUM_ENABLED )) && [[ -t 1 ]]; then
163
+ gum spin --spinner dot --title "${title}" -- "$@"
164
+ return
165
+ fi
166
+
167
+ benchmark_log info "${title}"
168
+ "$@"
169
+ }
170
+
171
+ benchmark_print_available() {
172
+ local benchmark_definition benchmark_id topic_name
173
+
174
+ if (( BENCHMARK_GUM_ENABLED )) && [[ -t 1 ]]; then
175
+ gum style --bold --foreground 39 'Available benchmarks'
176
+ for benchmark_definition in "${BENCHMARK_DEFINITIONS[@]}"; do
177
+ IFS='|' read -r benchmark_id topic_name <<<"${benchmark_definition}"
178
+ printf ' %s %s\n' "${benchmark_id}" "${topic_name}"
179
+ done
180
+ return
181
+ fi
182
+
183
+ printf 'Available benchmarks:\n'
184
+ for benchmark_definition in "${BENCHMARK_DEFINITIONS[@]}"; do
185
+ IFS='|' read -r benchmark_id topic_name <<<"${benchmark_definition}"
186
+ printf ' %s (%s)\n' "${benchmark_id}" "${topic_name}"
187
+ done
188
+ }
189
+
190
+ benchmark_selection_contains() {
191
+ local expected_id=$1
192
+ local benchmark_id
193
+
194
+ if (( ${#BENCHMARK_SELECTED_IDS[@]} == 0 )); then
195
+ return 1
196
+ fi
197
+
198
+ for benchmark_id in "${BENCHMARK_SELECTED_IDS[@]}"; do
199
+ if [[ "${benchmark_id}" == "${expected_id}" ]]; then
200
+ return 0
201
+ fi
202
+ done
203
+
204
+ return 1
205
+ }
206
+
207
+ benchmark_is_valid_id() {
208
+ local expected_id=$1
209
+ local benchmark_definition benchmark_id _
210
+
211
+ for benchmark_definition in "${BENCHMARK_DEFINITIONS[@]}"; do
212
+ IFS='|' read -r benchmark_id _ <<<"${benchmark_definition}"
213
+ if [[ "${benchmark_id}" == "${expected_id}" ]]; then
214
+ return 0
215
+ fi
216
+ done
217
+
218
+ return 1
219
+ }
220
+
221
+ benchmark_get_topic_name() {
222
+ local expected_id=$1
223
+ local benchmark_definition benchmark_id topic_name
224
+
225
+ for benchmark_definition in "${BENCHMARK_DEFINITIONS[@]}"; do
226
+ IFS='|' read -r benchmark_id topic_name <<<"${benchmark_definition}"
227
+ if [[ "${benchmark_id}" == "${expected_id}" ]]; then
228
+ printf '%s\n' "${topic_name}"
229
+ return 0
230
+ fi
231
+ done
232
+
233
+ return 1
234
+ }
235
+
236
+ benchmark_print_assets_for_id() {
237
+ local expected_id=$1
238
+ local benchmark_asset benchmark_id
239
+
240
+ for benchmark_asset in "${BENCHMARK_ASSET_DEFINITIONS[@]}"; do
241
+ IFS='|' read -r benchmark_id _ <<<"${benchmark_asset}"
242
+ if [[ "${benchmark_id}" == "${expected_id}" ]]; then
243
+ printf '%s\n' "${benchmark_asset}"
244
+ fi
245
+ done
246
+ }
247
+
248
+ benchmark_add_selected_id() {
249
+ local benchmark_id
250
+
251
+ benchmark_id="$(benchmark_trim "$1")"
252
+ if [[ -z "${benchmark_id}" ]]; then
253
+ printf 'Benchmark id cannot be empty\n' >&2
254
+ return 1
255
+ fi
256
+
257
+ if ! benchmark_is_valid_id "${benchmark_id}"; then
258
+ printf 'Unknown benchmark: %s\n' "${benchmark_id}" >&2
259
+ benchmark_print_available >&2
260
+ return 1
261
+ fi
262
+
263
+ if benchmark_selection_contains "${benchmark_id}"; then
264
+ return 0
265
+ fi
266
+
267
+ BENCHMARK_SELECTED_IDS+=("${benchmark_id}")
268
+ }
269
+
270
+ benchmark_add_selected_csv() {
271
+ local raw_ids=$1
272
+ local benchmark_ids benchmark_id
273
+
274
+ IFS=',' read -r -a benchmark_ids <<<"${raw_ids}"
275
+ for benchmark_id in "${benchmark_ids[@]}"; do
276
+ benchmark_add_selected_id "${benchmark_id}" || return 1
277
+ done
278
+ }
279
+
280
+ benchmark_select_all_if_empty() {
281
+ local benchmark_definition benchmark_id
282
+
283
+ if (( ${#BENCHMARK_SELECTED_IDS[@]} > 0 )); then
284
+ return
285
+ fi
286
+
287
+ for benchmark_definition in "${BENCHMARK_DEFINITIONS[@]}"; do
288
+ IFS='|' read -r benchmark_id _ <<<"${benchmark_definition}"
289
+ BENCHMARK_SELECTED_IDS+=("${benchmark_id}")
290
+ done
291
+ }
292
+
293
+ benchmark_parse_selection_flags() {
294
+ BENCHMARK_SELECTED_IDS=()
295
+
296
+ while [[ $# -gt 0 ]]; do
297
+ case "$1" in
298
+ --benchmark)
299
+ if [[ $# -lt 2 ]]; then
300
+ printf 'Missing value for --benchmark\n' >&2
301
+ return 1
302
+ fi
303
+ benchmark_add_selected_id "$2" || return 1
304
+ shift 2
305
+ ;;
306
+ --benchmarks)
307
+ if [[ $# -lt 2 ]]; then
308
+ printf 'Missing value for --benchmarks\n' >&2
309
+ return 1
310
+ fi
311
+ benchmark_add_selected_csv "$2" || return 1
312
+ shift 2
313
+ ;;
314
+ *)
315
+ shift
316
+ ;;
317
+ esac
318
+ done
319
+
320
+ benchmark_select_all_if_empty
321
+ }
322
+
323
+ benchmark_selected_plan_json() {
324
+ local json='['
325
+ local separator=''
326
+ local benchmark_id topic_name
327
+
328
+ for benchmark_id in "${BENCHMARK_SELECTED_IDS[@]}"; do
329
+ topic_name="$(benchmark_get_topic_name "${benchmark_id}")" || return 1
330
+ json+="${separator}{\"id\":\"${benchmark_id}\",\"topicName\":\"${topic_name}\"}"
331
+ separator=','
332
+ done
333
+
334
+ json+=']'
335
+ printf '%s\n' "${json}"
336
+ }
package/benchmark-root.sh CHANGED
@@ -4,7 +4,11 @@ set -euo pipefail
4
4
 
5
5
  BENCHMARK_VISUALIZER_LOG="${BENCHMARK_VISUALIZER_LOG:-/tmp/depthai-benchmark-visualizer.log}"
6
6
  BENCHMARK_VISUALIZER_PIDFILE="${BENCHMARK_VISUALIZER_PIDFILE:-/tmp/depthai-benchmark-visualizer.pid}"
7
+ BENCHMARK_VISUALIZER_WARMUP_SECONDS="${BENCHMARK_VISUALIZER_WARMUP_SECONDS:-5}"
7
8
  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9
+ source "${SCRIPT_DIR}/benchmark-lib.sh"
10
+ benchmark_init_ui
11
+
8
12
  REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
9
13
  VISUALIZER_DIR="${REPO_ROOT}/packages/visualizer"
10
14
  VITE_BIN="${REPO_ROOT}/node_modules/.bin/vite"
@@ -23,6 +27,12 @@ if [[ $# -eq 0 ]]; then
23
27
  exit 1
24
28
  fi
25
29
 
30
+ if ! benchmark_parse_selection_flags "$@"; then
31
+ exit 1
32
+ fi
33
+
34
+ benchmark_plan="$(benchmark_selected_plan_json)"
35
+
26
36
  cleanup() {
27
37
  if [[ -f "${BENCHMARK_VISUALIZER_PIDFILE}" ]]; then
28
38
  local visualizer_pid
@@ -56,15 +66,17 @@ cleanup
56
66
 
57
67
  (
58
68
  cd "${VISUALIZER_DIR}"
59
- BENCHMARK=true npm run build
60
- BENCHMARK=true "${VITE_BIN}" preview --host localhost --open '/'
69
+ benchmark_log info "Visualizer plan: $(benchmark_selected_ids_display)"
70
+ BENCHMARK=true BENCHMARK_PLAN="${benchmark_plan}" npm run build
71
+ BENCHMARK=true BENCHMARK_PLAN="${benchmark_plan}" "${VITE_BIN}" preview --host localhost --open '/'
61
72
  ) >"${BENCHMARK_VISUALIZER_LOG}" 2>&1 &
62
73
  visualizer_pid=$!
63
74
  printf '%s' "${visualizer_pid}" >"${BENCHMARK_VISUALIZER_PIDFILE}"
64
75
 
76
+ benchmark_log info "Starting visualizer preview. Logs: ${BENCHMARK_VISUALIZER_LOG}"
65
77
  for _ in {1..120}; do
66
78
  if ! kill -0 "${visualizer_pid}" 2>/dev/null; then
67
- echo "[benchmark] Visualizer preview exited early. See ${BENCHMARK_VISUALIZER_LOG}"
79
+ benchmark_log error "Visualizer preview exited early. See ${BENCHMARK_VISUALIZER_LOG}"
68
80
  exit 1
69
81
  fi
70
82
 
@@ -80,8 +92,13 @@ for _ in {1..120}; do
80
92
  done
81
93
 
82
94
  if ! curl -sf http://localhost:4173/ >/dev/null; then
83
- echo "[benchmark] Visualizer preview did not become ready. See ${BENCHMARK_VISUALIZER_LOG}"
95
+ benchmark_log error "Visualizer preview did not become ready. See ${BENCHMARK_VISUALIZER_LOG}"
84
96
  exit 1
85
97
  fi
86
98
 
99
+ benchmark_log success 'Visualizer preview is ready'
100
+ if [[ "${BENCHMARK_VISUALIZER_WARMUP_SECONDS}" != '0' ]]; then
101
+ benchmark_log info "Waiting ${BENCHMARK_VISUALIZER_WARMUP_SECONDS}s for the visualizer to connect before starting benchmarks"
102
+ sleep "${BENCHMARK_VISUALIZER_WARMUP_SECONDS}"
103
+ fi
87
104
  "$@"