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
134
135
136
137
138
use crate::detections::Detections;
use crate::image::Image;
use darknet_sys as sys;
use std::ffi::CString;
use std::fs;
use std::io;
use std::mem;
use std::os::raw::c_int;
use std::path::Path;
use std::ptr;
use std::slice;
use std::sync::Arc;

#[cfg(unix)]
fn path_to_bytes<P: AsRef<Path>>(path: P) -> Vec<u8> {
    use std::os::unix::ffi::OsStrExt;
    path.as_ref().as_os_str().as_bytes().to_vec()
}

#[cfg(not(unix))]
fn path_to_bytes<P: AsRef<Path>>(path: P) -> Vec<u8> {
    path.as_ref().to_string_lossy().to_string().into_bytes()
}

/// Reads file line-by-line and returns vector of strings.
/// Useful for loading object labels from file.
pub fn load_labels<P: AsRef<Path> + ?Sized>(file_name: &P) -> Result<Vec<String>, io::Error> {
    Ok(fs::read_to_string(file_name)?
        .lines()
        .map(|x| x.trim().to_string())
        .collect())
}

pub struct Network {
    net: Option<Box<sys::network>>,
    labels: Arc<Vec<String>>,
}

impl Network {
    /// Load network from config file `cfg` (under cfg/ subdir) and weights file  `weights` (can be obtained from https://pjreddie.com/darknet/, optional if training).
    /// <br>`clear` - Reset network data (used for training).
    /// <br>`labels` - vector of object labels the model was trained on (i.e. vec!["car", "bird", "dog"...]).
    pub fn load<C: AsRef<Path> + ?Sized, W: AsRef<Path> + ?Sized>(
        cfg: &C,
        weights: Option<&W>,
        clear: bool,
        labels: Vec<String>,
    ) -> Option<Network> {
        let raw_weights = match weights {
            Some(w) => CString::new(path_to_bytes(w))
                .expect("CString::new(weights_file) failed")
                .into_raw(),
            None => ptr::null_mut(),
        };
        unsafe {
            let raw_cfg = CString::new(path_to_bytes(cfg))
                .expect("CString::new(config_file) failed")
                .into_raw();
            let net = sys::load_network(raw_cfg, raw_weights, clear as c_int);
            mem::drop(CString::from_raw(raw_cfg));
            mem::drop(CString::from_raw(raw_weights));
            if net != ptr::null_mut() {
                sys::set_batch_network(net, 1);
                return Some(Network {
                    net: Some(Box::from_raw(net)),
                    labels: Arc::new(labels),
                });
            } else {
                return None;
            }
        }
    }

    /// Network input width.
    pub fn get_w(&self) -> usize {
        self.net.as_ref().unwrap().w as usize
    }

    /// Network input height.
    pub fn get_h(&self) -> usize {
        self.net.as_ref().unwrap().h as usize
    }

    /// Predict and return object bboxes (with probability > 'thresh').
    /// <br>'nms' - overlap threshold for non-maximum suppression (higher = more overlapping allowed)
    pub fn predict(&mut self, image: &mut Image, thresh: f32, nms: f32) -> Detections {
        image.resize(self.get_w(), self.get_h());
        unsafe {
            sys::network_predict(&mut (**self.net.as_mut().unwrap()), image.get_raw_data());
            let mut nboxes: c_int = 0;
            let det_ptr = sys::get_network_boxes(
                &mut (**self.net.as_mut().unwrap()),
                1,
                1,
                thresh,
                0.0,
                ptr::null_mut(),
                0,
                &mut nboxes,
            );
            if nms != 0.0 {
                sys::do_nms_sort(det_ptr, nboxes, self.labels.len() as i32, nms);
            }
            Detections::new(
                Vec::from_raw_parts(det_ptr, nboxes as usize, nboxes as usize),
                &self.labels,
                thresh,
                slice::from_raw_parts(
                    self.net.as_mut().unwrap().layers,
                    (self.net.as_mut().unwrap().n) as usize,
                )[(self.net.as_mut().unwrap().n - 1) as usize]
                    .coords as usize,
            )
        }
    }

    /// Save network weights to file
    pub fn save_weights<P: AsRef<Path> + ?Sized>(&mut self, file_name: &P) {
        let file_name =
            CString::new(path_to_bytes(file_name)).expect("CString::new(file_name) failed");
        unsafe {
            sys::save_weights(&mut (**self.net.as_mut().unwrap()), file_name.into_raw());
        }
    }

    /// Returns vector of object labels
    pub fn get_labels(&self) -> Vec<String> {
        self.labels.as_ref().clone()
    }
}

impl Drop for Network {
    fn drop(&mut self) {
        unsafe {
            sys::free_network(Box::leak(self.net.take().unwrap()));
        }
    }
}