Skip to content

Instantly share code, notes, and snippets.

@rajeshpachaikani
Created November 14, 2021 11:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajeshpachaikani/949652fe9fac4b246d68fd3f301019df to your computer and use it in GitHub Desktop.
Save rajeshpachaikani/949652fe9fac4b246d68fd3f301019df to your computer and use it in GitHub Desktop.
Rust opencv demo
/*
Author: Rajesh Pachaikani
Contact: rajesh@makeitnow.in
*/
use opencv::{highgui, prelude::*, videoio, Result};
fn main() -> Result<()> {
let mut cam = videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
highgui::named_window("window", highgui::WINDOW_FULLSCREEN)?;
let mut frame = Mat::default();
loop{
cam.read(&mut frame)?;
highgui::imshow("window", &frame)?;
let key = highgui::wait_key(1)?;
if key == 113{
break;
}
}
Ok(())
}
@flixstn
Copy link

flixstn commented Nov 24, 2021

Saw your post on reddit and you asked for feedback. Nice getting started with OpenCV and Rust btw!
Regarding your code snippet and possible suggestions I would consider adapting the following:

use opencv::{highgui, prelude::*, videoio, Result};

fn main() -> Result<()> {
    let mut cam = videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
    let mut frame = Mat::default();
    
    // moving wait_key(1) in the loop header allows for easy loop breaking if a condition is met (0 corresponds to 'ESC', 113 would be 'Q'
    while highgui::wait_key(1)? < 0 {
        cam.read(&mut frame)?;

        // check whether VideoCapture still has frames to capture
        if !cam.grab()? {
            println!("Video processing finished");
            break
        }

        highgui::imshow("window", &frame)?;
    }

    Ok(())
}

@rajeshpachaikani
Copy link
Author

I am very new to Rust. I didn't knew I can use while loop like that in Rust. Thank you for your inputs.

@cardboardcode
Copy link

cardboardcode commented Jan 12, 2023

Thanks @rajeshpachaikani. ๐Ÿ˜„

Just getting started with Rust and your script helped out.

Instructions

Hi, for anyone trying to run the script above, you can refer to what I did to get the above script running after having installed Rust on my work station.

Environment ๐Ÿ“–

Ubuntu 22.04

  1. Set up cargo workspace
cd $HOME
cargo new --bin rust-opencv

Build ๐Ÿ”จ

  1. Include opencv dependencies under newly generated Cargo.toml:
cd $HOME
cd rust-opencv

Your Cargo.toml should look what is shown below:

[package]
name = "rust-opencv"
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.60.0" # <---

Run ๐Ÿƒโ€โ™‚๏ธ

  1. Build cargo workspace:
cd ~/rust-opencv
cargo run

The web camera that you have connected should now be displayed using the OpenCV GUI.

Issues ๐Ÿ›

If you encountered an error similar to what is shown below:

warning: could not execute `llvm-config` one or more times, if the LLVM_CONFIG_PATH environment variable is set to a full path to valid `llvm-config` executable it will be used to try to find an instance of `libclang` on your system: "couldn't execute `llvm-config --prefix` (path=llvm-config) (error: No such file or directory (os error 2))"

error: failed to run custom build command for `clang-sys v1.4.0`

You can resolve this issue by running the command below:

sudo apt-get install clang libclang-dev

@MuneebHoda
Copy link

failed to run custom build command for opencv v0.78.0
-- Error while running this code

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