Search code examples
opencvrustface-recognitionffidlib

opencv face recognition LBPH issue


I have been struggling for two day to find an appropriate way of initializing PtrOfLBPHFaceRecognizer in rust using opencv.

let mut void_pointer = libc::malloc(mem::size_of::<c_void>() as libc::size_t) as *mut c_void;
if void_pointer.is_null() {
    panic!("failed to allocate memory");
}

let mut model = PtrOfLBPHFaceRecognizer::from_raw(void_pointer);
let ptr = PtrOfLBPHFaceRecognizer::init(model);
let mut model = PtrOfLBPHFaceRecognizer::deref_mut(ptr);
model.train(&images,&labels);

result is panic with invalid memory reference.

any ideas are appreciated.


Solution

  • You can do this by calling the create method. The default parameters used in C++ to initialize it are also described in the docs.

    use opencv::face::prelude::*;
    
    // According to docs, these values are the defaults used in C++
    let mut model: PtrOfLBPHFaceRecognizer = <dyn LBPHFaceRecognizer>::create(1, 8, 8, 8, DBL_MAX).unwrap();