Skip to content

Instantly share code, notes, and snippets.

@rajeshpachaikani
Last active June 14, 2025 18:30
Show Gist options
  • Save rajeshpachaikani/3de2c3b4aba65e63d30dd4199ad71277 to your computer and use it in GitHub Desktop.
Save rajeshpachaikani/3de2c3b4aba65e63d30dd4199ad71277 to your computer and use it in GitHub Desktop.
Face and Eye detection using Opencv On Rustlang
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/local/share/opencv4/haarcascades/haarcascade_eye.xml";
//Haarcascade for face detection
let xml = "/usr/local/share/opencv4/haarcascades/haarcascade_frontalface_default.xml";
let mut face_detector = objdetect::CascadeClassifier::new(xml)?;
let mut img = Mat::default();
loop{
camera.read(&mut img)?;
let mut gray = Mat::default();
imgproc::cvt_color(&img, &mut gray, imgproc::COLOR_BGR2GRAY, 0)?;
let mut faces = types::VectorOfRect::new();
face_detector.detect_multi_scale(
&gray,
&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(())
}
@MuhammadHamzaChippa
Copy link

https://docs.rs/opencv/latest/opencv/imgproc/fn.put_text.html
https://docs.rs/opencv/latest/opencv/core/struct.Point_.html#structfield.x

Brother I am trying to use put_text on in my program unable to figure out how to use Point in the put_text function.
here is the snippet of code I am using in my function, it will be kind of you if you help me in this function ?

use opencv::{highgui, prelude::*, imgcodecs, imgproc, Point_  Result};

pub fn image_display_write() -> Result<()> {
    let url = String::from("../image.jpeg") ;
    let mut image_color = imgcodecs::imread(&url, imgcodecs::IMREAD_COLOR)?;
    let mut i = 0 ; 
    loop {
         highgui::imshow("image", &image_color)?;
         let mut key = highgui::wait_key(1)?;
         let font = imgproc::FONT_HERSHEY_SIMPLEX;
         let company:&str="TutorialsPoint";
         imgproc::put_text(&mut image_color, company, (140+i , 250), font, 0.5, [255,255,255], 2, imgproc::LINE_AA , true)?;
         i = i + 1 ; 
         if key == 113{
            break;
        }
    } 
    Ok(())
}

@QW1CKS
Copy link

QW1CKS commented Nov 20, 2024

If anyone is facing the same issue but on a windows 10 or 11, follow this solution.

@adarshgupta404
Copy link

adarshgupta404 commented Jun 14, 2025

@rajeshpachaikani I'm working on building a Tauri + Next.js desktop application that uses OpenCV for face detection. My goal is to integrate OpenCV into the app for real-time facial recognition features.

However, one challenge I'm facing is dependency management. Since OpenCV is an external native library, if a user does not have it properly installed on their system, the application will fail to open or function correctly.

Ideally, I want to either:

Bundle OpenCV with the app during the Tauri build process, or

Check at runtime if OpenCV is available, and show an error or guide the user through the installation if it's not.

Let me know if you have any suggestions on handling this dependency more gracefully or if there's a way to make the app fallback safely without OpenCV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment