File size: 5,415 Bytes
b98ffbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//! Demonstrates the most barebone usage of the Rerun SDK.

use std::env::VarError;

use dora_node_api::{
    arrow::array::{Float32Array, StringArray, UInt8Array},
    DoraNode, Event,
};
use eyre::{eyre, Context, Result};
use rerun::{
    external::re_types::ArrowBuffer, SpawnOptions, TensorBuffer, TensorData, TensorDimension,
};

fn main() -> Result<()> {
    // rerun `serve()` requires to have a running Tokio runtime in the current context.
    let rt = tokio::runtime::Runtime::new().expect("Failed to create tokio runtime");
    let _guard = rt.enter();

    let (_node, mut events) =
        DoraNode::init_from_env().context("Could not initialize dora node")?;

    // Limit memory usage
    let mut options = SpawnOptions::default();

    let memory_limit = match std::env::var("RERUN_MEMORY_LIMIT") {
        Ok(memory_limit) => memory_limit
            .parse::<String>()
            .context("Could not parse RERUN_MEMORY_LIMIT value")?,
        Err(VarError::NotUnicode(_)) => {
            return Err(eyre!("RERUN_MEMORY_LIMIT env variable is not unicode"));
        }
        Err(VarError::NotPresent) => "25%".to_string(),
    };

    options.memory_limit = memory_limit;

    let rec = rerun::RecordingStreamBuilder::new("dora-rerun")
        .spawn_opts(&options, None)
        .context("Could not spawn rerun visualization")?;

    while let Some(event) = events.recv() {
        if let Event::Input {
            id,
            data,
            metadata: _,
        } = event
        {
            if id.as_str().contains("image") {
                let shape = vec![
                    TensorDimension {
                        name: Some("height".into()),
                        size: std::env::var(format!("{}_HEIGHT", id.as_str().to_uppercase()))
                            .context(format!(
                                "Could not read {}_HEIGHT env variable for parsing the image",
                                id.as_str().to_uppercase()
                            ))?
                            .parse()
                            .context(format!(
                                "Could not parse env {}_HEIGHT",
                                id.as_str().to_uppercase()
                            ))?,
                    },
                    TensorDimension {
                        name: Some("width".into()),
                        size: std::env::var(format!("{}_WIDTH", id.as_str().to_uppercase()))
                            .context(format!(
                                "Could not read {}_WIDTH env variable for parsing the image",
                                id.as_str().to_uppercase()
                            ))?
                            .parse()
                            .context(format!(
                                "Could not parse env {}_WIDTH",
                                id.as_str().to_uppercase()
                            ))?,
                    },
                    TensorDimension {
                        name: Some("depth".into()),
                        size: std::env::var(format!("{}_DEPTH", id.as_str().to_uppercase()))
                            .context(format!(
                                "Could not read {}_DEPTH env variable for parsing the image",
                                id.as_str().to_uppercase()
                            ))?
                            .parse()
                            .context(format!(
                                "Could not parse env {}_DEPTH",
                                id.as_str().to_uppercase()
                            ))?,
                    },
                ];

                let buffer: UInt8Array = data.to_data().into();
                let buffer: &[u8] = buffer.values();
                let buffer = TensorBuffer::U8(ArrowBuffer::from(buffer));
                let tensordata = TensorData::new(shape.clone(), buffer);
                let image = rerun::Image::new(tensordata);

                rec.log(id.as_str(), &image)
                    .context("could not log image")?;
            } else if id.as_str().contains("textlog") {
                let buffer: StringArray = data.to_data().into();
                buffer.iter().try_for_each(|string| -> Result<()> {
                    if let Some(str) = string {
                        rec.log(id.as_str(), &rerun::TextLog::new(str))
                            .wrap_err("Could not log text")
                    } else {
                        Ok(())
                    }
                })?;
            } else if id.as_str().contains("boxes2d") {
                let buffer: Float32Array = data.to_data().into();
                let buffer: &[f32] = buffer.values();
                let mut centers = vec![];
                let mut sizes = vec![];
                let mut classes = vec![];
                buffer.chunks(6).for_each(|block| {
                    if let [x, y, w, h, _conf, cls] = block {
                        centers.push((*x, *y));
                        sizes.push((*w, *h));
                        classes.push(*cls as u16);
                    }
                });
                rec.log(
                    id.as_str(),
                    &rerun::Boxes2D::from_centers_and_sizes(centers, sizes).with_class_ids(classes),
                )
                .wrap_err("Could not log Boxes2D")?;
            }
        }
    }

    Ok(())
}