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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Bindings to OpenCV's classes and functions that exploits GPU/Cuda. See
//! [cv::cuda](http://docs.opencv.org/3.1.0/d1/d1a/namespacecv_1_1cuda.html)

use super::core::*;
use super::errors::*;
use super::objdetect::{CSvmDetector, HogParams, ObjectDetect, SvmDetector};
use failure::Error;
use std::ffi::CString;
use std::os::raw::{c_char, c_double, c_int};
use std::path::Path;
use *;

/// Opaque data struct for C/C++ cv::cuda::GpuMat bindings
#[derive(Clone, Copy, Debug)]
pub enum CGpuMat {}

/// `GpuMat` data structure in rust, bound to an opaque type in C/C++.
#[derive(Debug)]
pub struct GpuMat {
    /// The pointer to the opaque C/C++ data structure
    pub(crate) inner: *mut CGpuMat,

    /// Number of columns
    pub cols: c_int,

    /// Number of rows
    pub rows: c_int,

    /// Depth of this mat
    pub depth: c_int,
}

extern "C" {
    fn cv_cuda_gpu_mat_default() -> *mut CGpuMat;
    fn cv_cuda_gpu_mat_drop(gpu_mat: *mut CGpuMat);
    fn cv_cuda_gpu_mat_upload(gpu_mat: *mut CGpuMat, cpu_mat: *const CMat);
    fn cv_mat_from_gpu_mat(gpu_mat: *mut CGpuMat) -> *mut CMat;
    fn cv_cuda_gpu_mat_from_mat(mat: *mut CMat) -> *mut CGpuMat;
}

impl GpuMat {
    /// Creates a default `GpuMat`.
    pub fn default() -> GpuMat {
        GpuMat {
            inner: unsafe { cv_cuda_gpu_mat_default() },
            cols: 0,
            rows: 0,
            depth: 0,
        }
    }

    /// Creates a `GpuMat` from raw pointer.
    pub(crate) fn from_raw(inner: *mut CGpuMat) -> GpuMat {
        GpuMat {
            inner: inner,
            cols: 0,
            rows: 0,
            depth: 0,
        }
    }

    /// Uploads a normal `Mat`
    pub fn upload(&mut self, mat: &Mat) {
        unsafe {
            cv_cuda_gpu_mat_upload(self.inner, mat.inner);
        }
    }
}

impl Drop for GpuMat {
    fn drop(&mut self) {
        unsafe {
            cv_cuda_gpu_mat_drop(self.inner);
        }
    }
}

impl From<GpuMat> for Mat {
    fn from(gpu_mat: GpuMat) -> Mat {
        unsafe { Mat::from_raw(cv_mat_from_gpu_mat(gpu_mat.inner)) }
    }
}

impl From<Mat> for GpuMat {
    fn from(mat: Mat) -> GpuMat {
        unsafe { GpuMat::from_raw(cv_cuda_gpu_mat_from_mat(mat.inner)) }
    }
}

/// Opaque data struct for C bindings
#[derive(Clone, Copy, Debug)]
pub enum CGpuHog {}

#[derive(Debug)]
/// Data structure that performs Histogram of Gradient (HOG).
pub struct GpuHog {
    inner: *mut CGpuHog,

    /// Hog parameters.
    pub params: HogParams,

    /// Should return detection scores
    pub return_score: bool,
}

/// We can safely send a mutable pointer to a different thread
unsafe impl Send for GpuHog {}

extern "C" {
    fn cv_cuda_hog_default() -> *mut CGpuHog;
    fn cv_cuda_hog_new(
        win_size: Size2i,
        block_size: Size2i,
        block_stride: Size2i,
        cell_size: Size2i,
        nbins: c_int,
    ) -> *mut CGpuHog;
    fn cv_cuda_hog_drop(hog: *mut CGpuHog);
    fn cv_cuda_hog_set_detector(hog: *mut CGpuHog, d: *const CSvmDetector);
    fn cv_cuda_hog_detect(hog: *mut CGpuHog, mat: *mut CGpuMat, found: *mut CVec<Rect>);
    fn cv_cuda_hog_detect_with_conf(
        hog: *mut CGpuHog,
        mat: *mut CGpuMat,
        found: *mut CVec<Rect>,
        conf: *mut CVec<c_double>,
    );

    fn cv_cuda_hog_set_gamma_correction(hog: *mut CGpuHog, gamma: bool);
    fn cv_cuda_hog_set_group_threshold(hog: *mut CGpuHog, group_threshold: c_int);
    fn cv_cuda_hog_set_hit_threshold(hog: *mut CGpuHog, hit_threshold: c_double);
    fn cv_cuda_hog_set_l2hys_threshold(hog: *mut CGpuHog, l2hys_threshold: c_double);
    fn cv_cuda_hog_set_num_levels(hog: *mut CGpuHog, num_levels: usize);
    fn cv_cuda_hog_set_scale_factor(hog: *mut CGpuHog, scale_factor: c_double);
    fn cv_cuda_hog_set_win_sigma(hog: *mut CGpuHog, win_sigma: c_double);
    fn cv_cuda_hog_set_win_stride(hog: *mut CGpuHog, win_stride: Size2i);

    fn cv_cuda_hog_get_gamma_correction(hog: *mut CGpuHog) -> bool;
    fn cv_cuda_hog_get_group_threshold(hog: *mut CGpuHog) -> c_int;
    fn cv_cuda_hog_get_hit_threshold(hog: *mut CGpuHog) -> c_double;
    fn cv_cuda_hog_get_l2hys_threshold(hog: *mut CGpuHog) -> c_double;
    fn cv_cuda_hog_get_num_levels(hog: *mut CGpuHog) -> usize;
    fn cv_cuda_hog_get_scale_factor(hog: *mut CGpuHog) -> c_double;
    fn cv_cuda_hog_get_win_sigma(hog: *mut CGpuHog) -> c_double;
    fn cv_cuda_hog_get_win_stride(hog: *mut CGpuHog) -> Size2i;
}

impl ObjectDetect for GpuHog {
    fn detect(&self, image: &Mat) -> Vec<(Rect, f64)> {
        let mut gpu_mat = GpuMat::default();
        gpu_mat.upload(image);
        if self.return_score {
            self._detect_with_confidence(&gpu_mat)
        } else {
            self._detect(&gpu_mat)
        }
    }
}

impl Default for GpuHog {
    fn default() -> GpuHog {
        let inner = unsafe { cv_cuda_hog_default() };
        let mut params = HogParams::default();
        GpuHog::update_params(inner, &mut params);
        GpuHog {
            inner: inner,
            params: params,
            return_score: false,
        }
    }
}

impl GpuHog {
    /// Creates a new GpuHog detector.
    pub fn new(win_size: Size2i, block_size: Size2i, block_stride: Size2i, cell_size: Size2i, nbins: c_int) -> GpuHog {
        let inner = unsafe { cv_cuda_hog_new(win_size, block_size, block_stride, cell_size, nbins) };
        let mut params = HogParams::default();
        GpuHog::update_params(inner, &mut params);
        GpuHog {
            inner: inner,
            params: params,
            return_score: false,
        }
    }

    /// Should or not return the detection score
    pub fn return_score(&mut self, should: bool) {
        self.return_score = should;
    }

    /// Creates a new GpuHog detector with parameters specified inside `params`.
    pub fn with_params(params: HogParams) -> GpuHog {
        let inner = unsafe {
            cv_cuda_hog_new(
                params.win_size,
                params.block_size,
                params.block_stride,
                params.cell_size,
                params.nbins,
            )
        };
        unsafe {
            cv_cuda_hog_set_gamma_correction(inner, params.gamma_correction);
            cv_cuda_hog_set_l2hys_threshold(inner, params.l2hys_threshold);
            cv_cuda_hog_set_num_levels(inner, params.nlevels);
            cv_cuda_hog_set_win_sigma(inner, params.win_sigma);

            cv_cuda_hog_set_win_stride(inner, params.win_stride);
            cv_cuda_hog_set_scale_factor(inner, params.scale);
            cv_cuda_hog_set_hit_threshold(inner, params.hit_threshold);
            cv_cuda_hog_set_group_threshold(inner, params.group_threshold);
        }
        GpuHog {
            inner: inner,
            params: params,
            return_score: false,
        }
    }

    /// Updates the parameter inside this GpuHog detector.
    fn update_params(inner: *mut CGpuHog, params: &mut HogParams) {
        params.gamma_correction = unsafe { cv_cuda_hog_get_gamma_correction(inner) };
        params.group_threshold = unsafe { cv_cuda_hog_get_group_threshold(inner) };
        params.hit_threshold = unsafe { cv_cuda_hog_get_hit_threshold(inner) };
        params.l2hys_threshold = unsafe { cv_cuda_hog_get_l2hys_threshold(inner) };
        params.nlevels = unsafe { cv_cuda_hog_get_num_levels(inner) };
        params.scale = unsafe { cv_cuda_hog_get_scale_factor(inner) };
        params.win_sigma = unsafe { cv_cuda_hog_get_win_sigma(inner) };
        params.win_stride = unsafe { cv_cuda_hog_get_win_stride(inner) };
    }

    /// Sets the SVM detector.
    pub fn set_svm_detector(&mut self, detector: SvmDetector) {
        unsafe { cv_cuda_hog_set_detector(self.inner, detector.inner) }
    }

    /// Detects according to the SVM detector specified.
    fn _detect(&self, mat: &GpuMat) -> Vec<(Rect, f64)> {
        let mut found = CVec::<Rect>::default();
        unsafe {
            cv_cuda_hog_detect(self.inner, mat.inner, &mut found);
        }
        found.unpack().into_iter().map(|r| (r, 0f64)).collect::<Vec<_>>()
    }

    /// Detects and returns the results with confidence (scores)
    fn _detect_with_confidence(&self, mat: &GpuMat) -> Vec<(Rect, f64)> {
        let mut found = CVec::<Rect>::default();
        let mut conf = CVec::<c_double>::default();
        unsafe { cv_cuda_hog_detect_with_conf(self.inner, mat.inner, &mut found, &mut conf) }

        found
            .unpack()
            .into_iter()
            .zip(conf.unpack().into_iter())
            .collect::<Vec<_>>()
    }
}

impl Drop for GpuHog {
    fn drop(&mut self) {
        unsafe { cv_cuda_hog_drop(self.inner) }
    }
}

/// Opaque data struct for C bindings
#[derive(Clone, Copy, Debug)]
pub enum CGpuCascade {}

#[derive(Debug)]
/// Data structure that performs object detection with a cascade classifier.
pub struct GpuCascade {
    inner: *mut CGpuCascade,
}

/// We can safely send a mutable pointer to a different thread
unsafe impl Send for GpuCascade {}

extern "C" {
    fn cv_cuda_cascade_new(filename: *const c_char) -> *mut CGpuCascade;
    fn cv_cuda_cascade_drop(cascade: *mut CGpuCascade);
    fn cv_cuda_cascade_detect(cascade: *mut CGpuCascade, image: *const CGpuMat, objects: *mut CVec<Rect>);

    fn cv_cuda_cascade_set_find_largest_object(cascade: *mut CGpuCascade, value: bool);
    fn cv_cuda_cascade_set_max_num_objects(cascade: *mut CGpuCascade, max: c_int);
    fn cv_cuda_cascade_set_min_neighbors(cascade: *mut CGpuCascade, min: c_int);
    fn cv_cuda_cascade_set_max_object_size(cascade: *mut CGpuCascade, max: Size2i);
    fn cv_cuda_cascade_set_min_object_size(cascade: *mut CGpuCascade, min: Size2i);
    fn cv_cuda_cascade_set_scale_factor(cascade: *mut CGpuCascade, factor: c_double);

    fn cv_cuda_cascade_get_classifier_size(cascade: *const CGpuCascade) -> Size2i;
    fn cv_cuda_cascade_get_find_largest_object(cascade: *const CGpuCascade) -> bool;
    fn cv_cuda_cascade_get_max_num_objects(cascade: *const CGpuCascade) -> c_int;
    fn cv_cuda_cascade_get_min_neighbors(cascade: *const CGpuCascade) -> c_int;
    fn cv_cuda_cascade_get_max_object_size(cascade: *const CGpuCascade) -> Size2i;
    fn cv_cuda_cascade_get_min_object_size(cascade: *const CGpuCascade) -> Size2i;
    fn cv_cuda_cascade_get_scale_factor(cascade: *const CGpuCascade) -> c_double;
}

impl GpuCascade {
    /// Loads the classifier from a file.
    ///
    /// Name of the file from which the classifier is loaded. Only the old
    /// haar classifier (trained by the haar training application) and NVIDIA's
    /// nvbin are supported for HAAR and only new type of OpenCV XML cascade
    /// supported for LBP. The working haar models can be found at
    /// opencv_folder/data/haarcascades_cuda/.
    pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
        if let Some(p) = path.as_ref().to_str() {
            let s = CString::new(p)?;
            let inner = unsafe { cv_cuda_cascade_new((&s).as_ptr()) };
            return Ok(GpuCascade { inner: inner });
        }
        Err(CvError::InvalidPath(path.as_ref().to_path_buf()).into())
    }

    /// Detects objects of different sizes in the input image.
    pub fn detect_multiscale(&self, mat: &GpuMat) -> Vec<Rect> {
        let mut found = CVec::<Rect>::default();
        unsafe {
            cv_cuda_cascade_detect(self.inner, mat.inner, &mut found);
        }
        found.unpack()
    }

    /// Sets whether or not to find the only largest object.
    pub fn set_find_largest_object(&mut self, value: bool) {
        unsafe {
            cv_cuda_cascade_set_find_largest_object(self.inner, value);
        }
    }

    /// Sets the maximum number of objects.
    pub fn set_max_num_objects(&mut self, max: c_int) {
        unsafe {
            cv_cuda_cascade_set_max_num_objects(self.inner, max);
        }
    }

    /// Sets minimal neighbors required for a detection to be valid.
    pub fn set_min_neighbors(&mut self, min: c_int) {
        unsafe {
            cv_cuda_cascade_set_min_neighbors(self.inner, min);
        }
    }

    /// Sets the maximun object size.
    pub fn set_max_object_size(&mut self, max: Size2i) {
        unsafe {
            cv_cuda_cascade_set_max_object_size(self.inner, max);
        }
    }

    /// Sets the minimal object size.
    pub fn set_min_object_size(&mut self, min: Size2i) {
        unsafe {
            cv_cuda_cascade_set_min_object_size(self.inner, min);
        }
    }

    /// Sets the scale factor used in multiscale detection.
    pub fn set_scale_factor(&mut self, factor: f64) {
        unsafe {
            cv_cuda_cascade_set_scale_factor(self.inner, factor);
        }
    }

    /// Returns the classifier size.
    pub fn get_classifier_size(&self) -> Size2i {
        unsafe { cv_cuda_cascade_get_classifier_size(self.inner) }
    }

    /// Returns if the CascadeClassifier will only return the largest object.
    pub fn get_find_largest_object_flag(&self) -> bool {
        unsafe { cv_cuda_cascade_get_find_largest_object(self.inner) }
    }

    /// Returns the allowed maximal number of detected objects.
    pub fn get_max_num_objects(&self) -> c_int {
        unsafe { cv_cuda_cascade_get_max_num_objects(self.inner) }
    }

    /// Returns the number of minimal neighbors required for a detection to be
    /// valid.
    pub fn get_min_neighbors(&self) -> c_int {
        unsafe { cv_cuda_cascade_get_min_neighbors(self.inner) }
    }

    /// Returns the maximum object size.
    pub fn get_max_object_size(&self) -> Size2i {
        unsafe { cv_cuda_cascade_get_max_object_size(self.inner) }
    }

    /// Returns the minimal object size.
    pub fn get_min_object_size(&self) -> Size2i {
        unsafe { cv_cuda_cascade_get_min_object_size(self.inner) }
    }

    /// Returns the scale factor.
    pub fn get_scale_factor(&self) -> f64 {
        unsafe { cv_cuda_cascade_get_scale_factor(self.inner) }
    }
}

impl ObjectDetect for GpuCascade {
    fn detect(&self, image: &Mat) -> Vec<(Rect, f64)> {
        let mut gpu_mat = GpuMat::default();
        gpu_mat.upload(image);
        self.detect_multiscale(&gpu_mat).into_iter().map(|r| (r, 0.0)).collect()
    }
}

impl Drop for GpuCascade {
    fn drop(&mut self) {
        unsafe { cv_cuda_cascade_drop(self.inner) }
    }
}