twistedfall / opencv-rust

Rust bindings for OpenCV 3 & 4
MIT License
2k stars 160 forks source link

Unable to read frames from video input file through opencv::videioio::VideoCapture #359

Closed svenne-juul closed 1 year ago

svenne-juul commented 2 years ago

I have worked through a good example of how videoio::VideoCapture::new(..) works with live video but I don't seem to be able to get input from an existing mp4 video file to work.

The following lines contain the issue (full logic below):


`use opencv::{
    core,
    highgui,
    prelude::*,
    videoio,
};

fn run()  -> opencv::Result<()> {
    let window = "video capture";
    highgui::named_window(window, 1)?;

    //  Issue below:
    //  'from_file' by it self, and 'from_file' followed by 'open_file' both fail it letting me successfully
    // perform the subsequent 'read'
    let file_name = "Black_Butterfly.mp4";    
    let mut cam = videoio::VideoCapture::from_file(&file_name, videoio::CAP_ANY)?;
    let opened_file = videoio::VideoCapture::open_file(&mut cam, &file_name, videoio::CAP_ANY)?;
    if !opened_file {
        panic!("Unable to open video file2!");
    };
    let mut frame = core::Mat::default();
    let frame_read = videoio::VideoCapture::read(&mut cam, &mut frame)?; 
    if !frame_read {
        panic!("Unable to read from video file!");
    };
    let opened = videoio::VideoCapture::is_opened(&mut cam)?;
    println!("Opened? {}", opened);
    if !opened {
        panic!("Unable to open video file!");
    };
    //  Issue above:**_
    loop {

        videoio::VideoCapture::read(&mut cam, &mut frame)?; 
        if frame.size()?.width > 0 {
            println!("Got a frame!");
            highgui::imshow(window, &frame)?;
            let key = highgui::wait_key(2000)?;
            if key > 0 && key != 255 {
                videoio::VideoCapture::release(&mut cam)?;
                break;
            }
        }
        else {
            println!("No more frames!");
            videoio::VideoCapture::release(&mut cam)?;
            break();
        }
    }
    Ok(())

}

fn main() {
    run().unwrap()
}
`

Operating system: Edition Windows 10 Pro Version 21H2 Installed on ‎8/‎10/‎2022 OS build 19044.1889 Experience Windows Feature Experience Pack 120.2212.4180.0

Installation Installed using the approach relying on chocolatey described under 'Windows package'. I believe the install works correctly as many other opencv-related sample programs work just fine.

RUST_BACKTRACE=full cargo build -vv

PS C:\Users\svenn\rust\pxlit_opencv> cargo build -vv Fresh autocfg v1.1.0 Fresh cfg-if v1.0.0 Fresh once_cell v1.13.0 Fresh scopeguard v1.1.0 Fresh unicode-ident v1.0.3 Fresh glob v0.3.0 Fresh num_cpus v1.13.1 Fresh adler32 v1.2.0 Fresh adler v1.0.2 Fresh regex-syntax v0.6.27 Fresh jobserver v0.1.24 Fresh maplit v1.0.2 Fresh either v1.8.0 Fresh percent-encoding v2.1.0 Fresh dunce v1.0.2 Fresh vcpkg v0.2.15 Fresh futures-sink v0.3.23 Fresh weezl v0.1.7 Fresh pkg-config v0.3.25 Fresh shlex v1.1.0 Fresh bit_field v0.10.1 Fresh half v1.8.2 Fresh bitflags v1.3.2 Fresh lebe v0.5.2 Fresh rawpointer v0.2.1 Fresh smallvec v1.9.0 Fresh color_quant v1.1.0 Fresh bytemuck v1.12.1 Fresh byteorder v1.4.3 Fresh scoped_threadpool v0.1.9 Fresh getrandom v0.2.7 Fresh threadpool v1.8.1 Fresh deflate v1.0.0 Fresh inflate v0.4.5 Fresh miniz_oxide v0.5.3 Fresh cc v1.0.73 Fresh matrixmultiply v0.3.2 Fresh gif v0.11.4 Fresh crossbeam-utils v0.8.11 Fresh proc-macro2 v1.0.43 Fresh winapi v0.3.9 Fresh nanorand v0.7.0 Fresh libc v0.2.129 Fresh memchr v2.5.0 Fresh crc32fast v1.3.2 Fresh semver v1.0.13 Fresh futures-core v0.3.23 Fresh memoffset v0.6.5 Fresh num-traits v0.2.15 Fresh anyhow v1.0.62 Fresh crossbeam-channel v0.5.6 Fresh lock_api v0.4.7 Fresh quote v1.0.21 Fresh libloading v0.7.3 Fresh aho-corasick v0.7.18 Fresh flate2 v1.0.24 Fresh png v0.17.5 Fresh crossbeam-epoch v0.9.10 Fresh num-integer v0.1.45 Fresh num-complex v0.4.2 Fresh syn v1.0.99 Fresh spin v0.9.4 Fresh clang-sys v1.3.3 Fresh regex v1.6.0 Fresh crossbeam-deque v0.8.2 Fresh pin-project-internal v1.0.12 Fresh num-rational v0.4.1 Fresh ndarray v0.15.6 Fresh rayon-core v1.9.3 Fresh clang v2.0.0 Fresh pin-project v1.0.12 Fresh rayon v1.5.3 Fresh opencv-binding-generator v0.45.0 Fresh jpeg-decoder v0.2.6 Fresh flume v0.10.14 Fresh tiff v0.7.3 Fresh exr v1.4.2 Fresh image v0.24.3 Fresh opencv v0.66.0 Compiling pxlit_opencv v0.1.0 (C:\Users\svenn\rust\pxlit_opencv) Running set CARGO=\\?\C:\Users\svenn\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\cargo.exe&& set CARGO_BIN_NAME=pxlit_opencv&& set CARGO_CRATE_NAME=pxlit_opencv&& set CARGO_MANIFEST_DIR=C:\Users\svenn\rust\pxlit_opencv&& set CARGO_PKG_AUTHORS=""&& set CARGO_PKG_DESCRIPTION=""&& set CARGO_PKG_HOMEPAGE=""&& set CARGO_PKG_LICENSE=""&& set CARGO_PKG_LICENSE_FILE=""&& set CARGO_PKG_NAME=pxlit_opencv&& set CARGO_PKG_REPOSITORY=""&& set CARGO_PKG_VERSION=0.1.0&& set CARGO_PKG_VERSION_MAJOR=0&& set CARGO_PKG_VERSION_MINOR=1&& set CARGO_PKG_VERSION_PATCH=0&& set CARGO_PKG_VERSION_PRE=""&& set CARGO_PRIMARY_PACKAGE=1&& set PATH="C:\Users\svenn\rust\pxlit_opencv\target\debug\deps;C:\Users\svenn\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin;C:\Users\svenn\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin;C:\Program Files\Dell\DW WLAN Card;;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\Microsoft VS Code\bin;C:\ProgramData\chocolatey\bin;C:\Program Files\LLVM\bin;C:\Users\svenn\.cargo\bin;C:\Users\svenn\AppData\Local\Microsoft\WindowsApps;"&& rustc --crate-name pxlit_opencv --edition=2021 src\main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=a3b9fd79ddabc8b9 --out-dir C:\Users\svenn\rust\pxlit_opencv\target\debug\deps -C incremental=C:\Users\svenn\rust\pxlit_opencv\target\debug\incremental -L dependency=C:\Users\svenn\rust\pxlit_opencv\target\debug\deps --extern anyhow=C:\Users\svenn\rust\pxlit_opencv\target\debug\deps\libanyhow-93e115d4e8bb996a.rlib --extern image=C:\Users\svenn\rust\pxlit_opencv\target\debug\deps\libimage-bd9c5d2de8a0ecf7.rlib --extern ndarray=C:\Users\svenn\rust\pxlit_opencv\target\debug\deps\libndarray-6af201cef50c369f.rlib --extern opencv=C:\Users\svenn\rust\pxlit_opencv\target\debug\deps\libopencv-984bef83365d79f6.rlib -L native=C:\Users\svenn\rust\pxlit_opencv\target\debug\build\opencv-e0385181a2a64e23\out -L C:\tools\opencv\build\x64\vc15\lib Finished dev [unoptimized + debuginfo] target(s) in 1.56s

The video file I am trying to read from

https://user-images.githubusercontent.com/111534511/185486044-3144b219-87ad-4d12-85b0-51bf9194bfd5.mp4

twistedfall commented 2 years ago

Hi, what is the actual error you are getting as the result of your program?

svenne-juul commented 2 years ago

Currently I get: 'thread 'main' panicked at 'Unable to open video file!2', src\main.rs:20:9 Ultimately I am trying to get get 'let frame_read = videoio::VideoCapture::read(&mut cam, &mut frame)?; ' to return a frame from the video, but I don't get that far because I can't get ' let opened_file = videoio::VideoCapture::open_file(&mut cam, &file_name, videoio::CAP_ANY)?;' to return anything other than that 'opened_file' is false.

It is not obvious to me why we have both the 'videoio::VideoCapture::from_file' and 'videoio::VideoCapture::open_file' functions that take the same parameters. In the openvc - python integration the equivalent function only requires the file name but not the 'api_preference'.

It is entirely possible that I am using the wrong function(s), but I have also tried only using 'videoio::VideoCapture::from_file' without getting to being able to read a frame.

I very much appreciate you looking into this and I hope I am not wasting your time.

svenne-juul commented 2 years ago

Today I worked with a co-worker running apple system. The logic that failed (and still is failing) on my system worked on his system. And a similar issue I am facing when writing video also worked without problems on his system.
I think this suggests that there is something wrong on my system, but of course I don't have the foggiest idea what it is.

twistedfall commented 2 years ago

It is not obvious to me why we have both the 'videoio::VideoCapture::from_file' and 'videoio::VideoCapture::open_file' functions that take the same parameters.

The former constructs the VideoCapture instance, the latter operates on the already existing instance.

It is entirely possible that I am using the wrong function(s)

Those seem to be the right functions for what you're trying to do

python integration the equivalent function only requires the file name but not the 'api_preference'.

Rust doesn't have a notion of default values for function parameters so it's not trivial to translate that logic to the bindings.

I think this suggests that there is something wrong on my system, but of course I don't have the foggiest idea what it is.

From the looks of it it may be that your OpenCV (the C++ library) is built without the support for necessary codecs.

svenne-juul commented 2 years ago

Thank you for your comments and suggestion. I will try another full reinstall (number 3) with the hope of getting the codecs 'right'

Regards

On Sat, Oct 1, 2022, 3:33 AM twistedfall @.***> wrote:

It is not obvious to me why we have both the 'videoio::VideoCapture::from_file' and 'videoio::VideoCapture::open_file' functions that take the same parameters.

The former constructs the VideoCapture instance, the latter operates on the already existing instance.

It is entirely possible that I am using the wrong function(s)

Those seem to be the right functions for what you're trying to do

python integration the equivalent function only requires the file name but not the 'api_preference'.

Rust doesn't have a notion of default values for function parameters so it's not trivial to translate that logic to the bindings.

I think this suggests that there is something wrong on my system, but of course I don't have the foggiest idea what it is.

From the looks of it it may be that your OpenCV (the C++ library) is built without the support for necessary codecs.

— Reply to this email directly, view it on GitHub https://github.com/twistedfall/opencv-rust/issues/359#issuecomment-1264277481, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2S6DL2V3NZXIURLBHZ4N3LWA7SOBANCNFSM566N4RRA . You are receiving this because you authored the thread.Message ID: @.***>

svenne-juul commented 1 year ago

Thank you.

On Sun, Jan 15, 2023, 4:36 AM twistedfall @.***> wrote:

Closed #359 https://github.com/twistedfall/opencv-rust/issues/359 as completed.

— Reply to this email directly, view it on GitHub https://github.com/twistedfall/opencv-rust/issues/359#event-8229134496, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2S6DL4MOYKMZDWY3TNOZPTWSPAJXANCNFSM566N4RRA . You are receiving this because you authored the thread.Message ID: @.***>

wredan commented 1 year ago

Hi everyone!

I had the same issue on Windows which I didn't have on MacOs and I managed to work. As @twistedfall wrote:

From the looks of it it may be that your OpenCV (the C++ library) is built without the support for necessary codecs.

this is the main issue. These are the steps that helped me to solve it:

  1. Copied the file opencv_videoio_ffmpeg470_64.dll from C:\where_you_have_installed_opencv\opencv\build\x64\vc16\bin like I found here, do not need to rename it like they mention:

1

  1. run cargo build.

You are good to go to use opencv::videioio::VideoCapture!

I had another issue with writing with opencv::videoio::VideoWriter on Windows, here is the message:

Failed to load OpenH264 library: openh264-1.8.0-win64.dll

to solve, the same steps as before:

  1. you need to go here and download the correct dll you require;
  2. unzip it and paste it into target/debug into your Rust project;
  3. run cargo build.

Hope this can help someone make it work on Windows! And thank you @twistedfall for the amazing work.

All the best

svenne-juul commented 1 year ago

Awesome Danilo and twistedfall,

I will try this out. I ended up getting a mac for my rust/opencv development for this reason.

Regards Svenne

On Fri, May 12, 2023 at 5:16 AM Danilo Santitto @.***> wrote:

Hi everyone!

I had the same issue on Windows which I didn't have on MacOs and I managed to work. As @twistedfall https://github.com/twistedfall wrote:

From the looks of it it may be that your OpenCV (the C++ library) is built without the support for necessary codecs.

this is the main issue. These are the steps that helped me to solve it:

  1. Copied the file opencv_videoio_ffmpeg470_64.dll from C:\where_you_have_installed_opencv\opencv\build\x64\vc16\bin like I found here https://stackoverflow.com/questions/11699298/opencv-2-4-videocapture-not-working-on-windows, do not need to rename it like they mention:

[image: 1] https://user-images.githubusercontent.com/32953565/237921059-7f495574-cbfc-456f-97a0-d82806ab2d1a.png

  1. run cargo build.

You are good to go to use opencv::videioio::VideoCapture!

I had another issue with writing with opencv::videoio::VideoWriter on Windows, here is the message:

Failed to load OpenH264 library: openh264-1.8.0-win64.dll

to solve, the same steps as before:

  1. you need to go here https://github.com/cisco/openh264/releases and download the correct dll you require;
  2. unzip it and paste it into target/debug into your Rust project;
  3. run cargo build.

Hope this can help someone make it work on Windows! And thank you @twistedfall https://github.com/twistedfall for the amazing work.

All the best

— Reply to this email directly, view it on GitHub https://github.com/twistedfall/opencv-rust/issues/359#issuecomment-1545438136, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2S6DL7SC6DL64MIJBH6NRDXFX5YHANCNFSM566N4RRA . You are receiving this because you authored the thread.Message ID: @.***>

programORdie2 commented 1 week ago

Hi everyone!

I had the same issue on Windows which I didn't have on MacOs and I managed to work. As @twistedfall wrote:

From the looks of it it may be that your OpenCV (the C++ library) is built without the support for necessary codecs.

this is the main issue. These are the steps that helped me to solve it:

1. Copied the file `opencv_videoio_ffmpeg470_64.dll` from `C:\where_you_have_installed_opencv\opencv\build\x64\vc16\bin` like I found [here](https://stackoverflow.com/questions/11699298/opencv-2-4-videocapture-not-working-on-windows), do not need to rename it like they mention:

1

2. run `cargo build`.

You are good to go to use opencv::videioio::VideoCapture!

I had another issue with writing with opencv::videoio::VideoWriter on Windows, here is the message:

Failed to load OpenH264 library: openh264-1.8.0-win64.dll

to solve, the same steps as before:

1. you need to go [here](https://github.com/cisco/openh264/releases) and download the correct dll you require;

2. unzip it and paste it into `target/debug` into your Rust project;

3. run `cargo build`.

Hope this can help someone make it work on Windows! And thank you @twistedfall for the amazing work.

All the best

Thank you very much, this worked for me!