Last active
August 13, 2025 07:28
-
-
Save rajeshpachaikani/3de2c3b4aba65e63d30dd4199ad71277 to your computer and use it in GitHub Desktop.
Face and Eye detection using Opencv On Rustlang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(()) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
twistedfall/opencv-rust#364 (comment)
I guess this link have the steps to statically linking opencv with a rust binary. I haven't tried it so try your luck and share the results here.