Last active
December 21, 2024 01:40
-
-
Save rajeshpachaikani/3de2c3b4aba65e63d30dd4199ad71277 to your computer and use it in GitHub Desktop.
Face and Eye detection using Opencv On Rustlang
This file contains 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(()) | |
} |
Looks like you don't have opencv insatlled in your system. If using ubuntu run sudo apt install libopencv-dev
and run cargo clean && cargo build
Thank you so much brother problem is solved
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(())
}
If anyone is facing the same issue but on a windows 10 or 11, follow this solution.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Rajesh, whenever I try to run cargo run I got an error while installing opencv, any idea why it is happening
error: failed to run custom build command for
opencv v0.62.0
Caused by:
process didn't exit successfully:
/home/husnain/Programming/open-cv-experiment/opencv-face-detector/target/debug/build/opencv-296215c7747d2549/build-script-build
(exit status: 1)--- stdout
cargo:rerun-if-env-changed=OPENCV4_NO_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG
cargo:rerun-if-env-changed=OPENCV4_STATIC
cargo:rerun-if-env-changed=OPENCV4_DYNAMIC
cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
cargo:rerun-if-env-changed=OPENCV_NO_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG
cargo:rerun-if-env-changed=OPENCV_STATIC
cargo:rerun-if-env-changed=OPENCV_DYNAMIC
cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
--- stderr
=== Crate version: Some("0.62.0")
=== Environment configuration:
=== OPENCV_PACKAGE_NAME = None
=== OPENCV_PKGCONFIG_NAME = None
=== OPENCV_CMAKE_NAME = None
=== OPENCV_CMAKE_BIN = None
=== OPENCV_VCPKG_NAME = None
=== OPENCV_LINK_LIBS = None
=== OPENCV_LINK_PATHS = None
=== OPENCV_INCLUDE_PATHS = None
=== OPENCV_DISABLE_PROBES = None
=== CMAKE_PREFIX_PATH = None
=== OpenCV_DIR = None
=== PKG_CONFIG_PATH = None
=== VCPKG_ROOT = None
=== VCPKGRS_DYNAMIC = None
=== Enabled features:
=== ALPHAMAT
=== ARUCO
=== BARCODE
=== BGSEGM
=== BIOINSPIRED
=== CALIB3D
=== CCALIB
=== CUDAARITHM
=== CUDABGSEGM
=== CUDACODEC
=== CUDAFEATURES2D
=== CUDAFILTERS
=== CUDAIMGPROC
=== CUDAOBJDETECT
=== CUDAOPTFLOW
=== CUDASTEREO
=== CUDAWARPING
=== CVV
=== DEFAULT
=== DNN
=== DNN_SUPERRES
=== DPM
=== FACE
=== FEATURES2D
=== FLANN
=== FREETYPE
=== FUZZY
=== GAPI
=== HDF
=== HFS
=== HIGHGUI
=== IMGCODECS
=== IMGPROC
=== IMG_HASH
=== INTENSITY_TRANSFORM
=== LINE_DESCRIPTOR
=== MCC
=== ML
=== OBJDETECT
=== OPTFLOW
=== OVIS
=== PHASE_UNWRAPPING
=== PHOTO
=== PLOT
=== QUALITY
=== RAPID
=== RGBD
=== SALIENCY
=== SFM
=== SHAPE
=== STEREO
=== STITCHING
=== STRUCTURED_LIGHT
=== SUPERRES
=== SURFACE_MATCHING
=== TEXT
=== TRACKING
=== VIDEO
=== VIDEOIO
=== VIDEOSTAB
=== VIZ
=== WECHAT_QRCODE
=== XFEATURES2D
=== XIMGPROC
=== XOBJDETECT
=== XPHOTO
=== Detected probe priority based on environment vars: pkg_config: false, cmake: false, vcpkg: false
=== Probing the OpenCV library in the following order: environment, pkg_config, cmake, vcpkg_cmake, vcpkg
=== Can't probe using: environment, continuing with other methods because: Some environment variables are missing
=== Probing OpenCV library using pkg_config
=== Can't probe using: pkg_config, continuing with other methods because:
"pkg-config" "--libs" "--cflags" "opencv4"
did not exit successfully: exit status: 1error: could not find system library 'opencv4' required by the 'opencv' crate
--- stderr
Package opencv4 was not found in the pkg-config search path.
Perhaps you should add the directory containing
opencv4.pc' to the PKG_CONFIG_PATH environment variable No package 'opencv4' found ,
"pkg-config" "--libs" "--cflags" "opencv"` did not exit successfully: exit status: 1error: could not find system library 'opencv' required by the 'opencv' crate
--- stderr
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
=== Probing OpenCV library using cmake
=== cmake ninja probe command: "cmake" "-S" "/home/husnain/.cargo/registry/src/github.com-1ecc6299db9ec823/opencv-0.62.0/cmake" "-DOCVRS_PACKAGE_NAME=OpenCV" "-DCMAKE_BUILD_TYPE=Debug" "-G" "Ninja"
=== Probing with cmake ninja generator failed, will try Makefile generator, error: cmake returned an error
stdout: "-- Configuring incomplete, errors occurred!\nSee also "/home/husnain/Programming/open-cv-experiment/opencv-face-detector/target/debug/build/opencv-34eb5fa72ffd59a7/out/cmake_probe_build/CMakeFiles/CMakeOutput.log".\n"
stderr: "CMake Error: CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.\nCMake Error: CMAKE_C_COMPILER not set, after EnableLanguage\nCMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage\n"
=== cmake makefiles probe command: "cmake" "-S" "/home/husnain/.cargo/registry/src/github.com-1ecc6299db9ec823/opencv-0.62.0/cmake" "-DOCVRS_PACKAGE_NAME=OpenCV" "-DCMAKE_BUILD_TYPE=Debug" "-G" "Unix Makefiles"
=== Probing with cmake Makefile generator failed, will try deprecated find_package, error: cmake returned an error
stdout: "-- The C compiler identification is GNU 9.3.0\n-- The CXX compiler identification is GNU 9.3.0\n-- Check for working C compiler: /usr/bin/cc\n-- Check for working C compiler: /usr/bin/cc -- works\n-- Detecting C compiler ABI info\n-- Detecting C compiler ABI info - done\n-- Detecting C compile features\n-- Detecting C compile features - done\n-- Check for working CXX compiler: /usr/bin/c++\n-- Check for working CXX compiler: /usr/bin/c++ -- works\n-- Detecting CXX compiler ABI info\n-- Detecting CXX compiler ABI info - done\n-- Detecting CXX compile features\n-- Detecting CXX compile features - done\n-- Configuring incomplete, errors occurred!\nSee also "/home/husnain/Programming/open-cv-experiment/opencv-face-detector/target/debug/build/opencv-34eb5fa72ffd59a7/out/cmake_probe_build/CMakeFiles/CMakeOutput.log".\n"
stderr: "CMake Error at CMakeLists.txt:5 (find_package):\n By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has\n asked CMake to find a package configuration file provided by "OpenCV", but\n CMake did not find one.\n\n Could not find a package configuration file provided by "OpenCV" with any\n of the following names:\n\n OpenCVConfig.cmake\n opencv-config.cmake\n\n Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set\n "OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"\n provides a separate development package or SDK, be sure it has been\n installed.\n\n\n"
=== cmake find-package compile probe command: "cmake" "-S" "/home/husnain/.cargo/registry/src/github.com-1ecc6299db9ec823/opencv-0.62.0/cmake" "-DOCVRS_PACKAGE_NAME=OpenCV" "-DCMAKE_BUILD_TYPE=Debug" "--find-package" "-DCOMPILER_ID=GNU" "-DLANGUAGE=CXX" "-DMODE=COMPILE" "-DNAME=OpenCV"
=== Can't probe using: cmake, continuing with other methods because: cmake returned an error
stdout: "OpenCV not found.\n"
stderr: "CMake Error: Problem processing arguments. Aborting.\n\n"
=== Probing OpenCV library using vcpkg_cmake
=== Can't probe using: vcpkg_cmake, continuing with other methods because: Could not find Vcpkg tree: No vcpkg installation found. Set the VCPKG_ROOT environment variable or run 'vcpkg integrate install'
=== Probing OpenCV library using vcpkg
=== Can't probe using: vcpkg, continuing with other methods because: Could not find Vcpkg tree: No vcpkg installation found. Set the VCPKG_ROOT environment variable or run 'vcpkg integrate install', Could not find Vcpkg tree: No vcpkg installation found. Set the VCPKG_ROOT environment variable or run 'vcpkg integrate install'
Error: "Failed to find OpenCV package using probes: environment, pkg_config, cmake, vcpkg_cmake, vcpkg"