Get Informed out of Data

Full width home advertisement

Raspbian

DataScience

Post Page Advertisement [Top]

Rust - OpenCV - Raspberry Pi 4 - Face Detection

Rust - OpenCV - Raspberry Pi 4 - Face Detection

 Rust being a language which ensures memory safety it is interesting to go with raspberry pi and face detection with a high speed compared to python.

OpenCV port for Rust is not yet officical but Opensource developer comes with a ported version for both OpenCV3 and OpenCV4.


The Steps we need to undergo to activate OpenCV Rust is obviously the starting with  Raspberry pi 4 Camera.

Now make sure you enable the Camera interface on your pi following below steps. Open a terminal and type

$ sudo raspi-config     

It will open the raspberry pi configuration window with several kinds of settings. You need to navigate through the menu using arrow keys to the Interfacing Options and select it by hitting enter.


then select the Legacy Camera option and enable it.


After completing this step - kindly reboot the pi 4.

Deep down - we are going to use the wrapper of c++ version of OpenCV - so to use the we need to install the Opencv.

Run the below command,

sudo apt install libopencv-dev clang libclang-dev

as you complete installing, we are ready to go with Rust things. Let's install Rust on Pi 4 with the command,

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

press 1 to complete the installation with default configurations.

Now, let's start with new project,

cargo new opencvtest

this will create a new directory with opencvtest

cd opencvtest

now get into the directory in geny or thonny

open the Cargo.toml file and fill the dependecies,

[package]

name = "testcam"

version = "0.1.0"

edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

opencv = "0.70.0"

now move to the main.rs in the src folder.

use opencv::{

    Result,

    prelude::*,

    objdetect,

    highgui,

    imgproc,

    core,

    types,

    videoio,

};

fn main()->Result<()>{

    let mut camera = videoio::VideoCapture::new(0, videoio::CAP_ANY)?;

    // Use the following command to find the actual location of your xml files

    //sudo find / -name haarcascade_frontalface_default.xml

    //Haarcascade for eye detection

    //let xml = "/usr/share/opencv4/haarcascades/haarcascade_eye.xml";

    //Haarcascade for face detection

    let xml = "/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml";  

    let mut face_detector = objdetect::CascadeClassifier::new(xml)?;

    let mut img = Mat::default();

    let mut imag = Mat::default();

    loop{

        camera.read(&mut imag)?;

        imgproc::resize(

&imag,

&mut img,

core::Size { width: 0, height: 0 },

0.15f64,

0.15f64,

imgproc::INTER_LINEAR,

)?;

        

        let mut gray1 = Mat::default();

        imgproc::cvt_color(&img, &mut gray1, imgproc::COLOR_BGR2RGB, 0)?;

        let mut gray = Mat::default();

        imgproc::cvt_color(&gray1, &mut gray, imgproc::COLOR_RGB2GRAY, 0)?;

        let mut faces = types::VectorOfRect::new();

        face_detector.detect_multi_scale(

            &img, 

            &mut faces, 

            1.1, 

            10,

            objdetect::CASCADE_SCALE_IMAGE,

            core::Size::new(10, 10),

            core::Size::new(0, 0)

        )?;

        println!("{:?}", faces);

        if faces.len() > 0{

            for face in faces.iter(){

                imgproc::rectangle(

                    &mut img,

                    face,

                    core::Scalar::new(0f64, 255f64, 0f64, 0f64),

                    2,

                    imgproc::LINE_8,

                    0

                )?;

            }    

        }

        highgui::imshow("gray", &img)?;

        highgui::wait_key(1)?;

    }

    Ok(())

}

save the both the files and come back to terminal - directory pointed to the opencvtest folder.

Run the command to test the camera

    cargo run

This will give you and output if the face being pointed towards the camera, interesting fact of this is it handles fps at almost 75-87.


This helps largely in building camera based applications at real time when compared to python.

No comments:

Post a Comment

Bottom Ad [Post Page]