Object detectionΒΆ
Load YOLOX into the AI inference handler, run detection on a few camera frames, and print per-frame fetch / detect timings.
Note
load_model creates an ONNX Runtime session on the appliance and keeps it
resident until you call unload() (or exit a with /
try/finally that unloads). Expect a noticeable load delay, and the
first detect() call is often slower than
later ones (session and accelerator warmup). Reuse one loaded session across frames rather than loading per image.
"""Run YOLOX on a few camera frames and print per-frame timing."""
import time
from olo import Client
from olo.ai.yolo import YoloDetector
# @olo-playground: autofill-sim-topics
RGB_TOPIC = "wrist_rgbd/color_image"
CONFIDENCE = 0.4
FRAME_COUNT = 5
with Client() as client:
robot = client.robot()
rgb_topic = robot.core.topic(RGB_TOPIC, "sensor_msgs/msg/Image")
load_start = time.perf_counter()
with client.ai.load_model("yolox_s.onnx") as model:
load_ms = (time.perf_counter() - load_start) * 1000
detector = YoloDetector(model)
print(f"Model load: {load_ms:.1f} ms")
print()
for frame_index in range(1, FRAME_COUNT + 1):
fetch_start = time.perf_counter()
image = robot.core.get_image(rgb_topic)
fetch_ms = (time.perf_counter() - fetch_start) * 1000
detect_start = time.perf_counter()
detections = detector.detect(image, conf=CONFIDENCE)
detect_ms = (time.perf_counter() - detect_start) * 1000
print(
f"Frame {frame_index}/{FRAME_COUNT}: "
f"fetch={fetch_ms:.1f} ms "
f"detect={detect_ms:.1f} ms "
f"({image.array.shape[1]}x{image.array.shape[0]}, "
f"{len(detections)} objects @ conf>={CONFIDENCE})"
)
/** Run YOLOX on a few camera frames and print per-frame timing. */
import { YoloDetector } from "olo/ai";
import { connect } from "olo/web";
// @olo-playground: autofill-sim-topics
const RGB_TOPIC = "wrist_rgbd/color_image";
const CONFIDENCE = 0.4;
const FRAME_COUNT = 5;
const client = connect();
const robot = await client.robot();
const rgbTopic = robot.core.topic(RGB_TOPIC, "sensor_msgs/msg/Image");
const loadStart = performance.now();
const model = await client.ai.loadModel("yolox_s.onnx");
try {
const loadMs = performance.now() - loadStart;
const detector = new YoloDetector(model);
console.log(`Model load: ${loadMs.toFixed(1)} ms`);
console.log();
for (let frameIndex = 1; frameIndex <= FRAME_COUNT; frameIndex++) {
const fetchStart = performance.now();
const image = await robot.core.getImage(rgbTopic);
const fetchMs = performance.now() - fetchStart;
const detectStart = performance.now();
const detections = await detector.detect(image, { conf: CONFIDENCE });
const detectMs = performance.now() - detectStart;
console.log(
`Frame ${frameIndex}/${FRAME_COUNT}: ` +
`fetch=${fetchMs.toFixed(1)} ms ` +
`detect=${detectMs.toFixed(1)} ms ` +
`(${image.width}x${image.height}, ` +
`${detections.length} objects @ conf>=${CONFIDENCE})`,
);
}
} finally {
await model.unload();
}