|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | +[](https://discord.gg/ffwj5rKZuf) |
| 4 | +[](https://www.youtube.com/channel/UClzmlBRrChCJCXkY2h9GhBQ?sub_confirmation=1) |
| 5 | +[](https://github.com/MakerPnP) |
| 6 | +[](https://ko-fi.com/dominicclifton) |
| 7 | +[](https://www.patreon.com/MakerPnP) |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +</div> |
| 12 | + |
| 13 | +# Video Capture and OpenCV |
| 14 | + |
| 15 | +A cross-platform tool to display video streams concurrently with [OpenCV](https://docs.opencv.org/4.x/index.html) visualizations. |
| 16 | + |
| 17 | +Video capture is done using the [`media-rs`](https://github.com/rust-media/media-rs) crate. |
| 18 | + |
| 19 | +Recent screenshot: |
| 20 | +[<img src="assets/screenshots/TODO.png" width="800" alt="VideoCapture + OpenCV">](assets/screenshots/TODO.png) |
| 21 | + |
| 22 | +## Background |
| 23 | + |
| 24 | +This was written as an experiment to see how to use the [`media-rs`](https://github.com/libark/video-capture) crate with a minimal set of `OpenCV` dependencies. |
| 25 | + |
| 26 | +As of OpenCV 4.x there is no way to enumerate cameras with OpenCV itself, the API doesn't allow you to specify a camera id, |
| 27 | +device path, serial number or other unique identifier when opening a video capture device. This makes it unsuitable for |
| 28 | +use when you want your program to always use the same cameras, regardless of which port it may be connected to. Additionally, |
| 29 | +OpenCV doesn't have an API for discovering available camera resolutions, frame rates or video formats (YUYV, NV12, MJPEG, etc), |
| 30 | +this is usually required for a camera application. It's highly recommend to use uncompressed formats like YUYV or NV12 |
| 31 | + when doing machine vision work due to the compression artifacts that can be introduced by MJPEG, or similar compressed formats. |
| 32 | + |
| 33 | +Thus, a solution was needed to capture video frames from a specific camera, using known-good resolution, frame rate and |
| 34 | +video format and then feed the frames into OpenCV for visualizations. |
| 35 | + |
| 36 | +There was also a desire to avoid the [`videoio`](https://github.com/opencv/opencv/blob/4.x/modules/videoio/doc/videoio_overview.markdown) feature in the OpenCV library since it uses much more C code and additional |
| 37 | +unsafe C baggage. |
| 38 | + |
| 39 | +Since this is an experimentation project, the code is not optimized for performance. There may be more optimal ways to |
| 40 | +get the camera images into OpenCV that what is shown here, currently the code converts the images into BGR `Mat`s for |
| 41 | +processing by OpenCV. |
| 42 | + |
| 43 | +Other related experiments: |
| 44 | +* `videocapture-and-opencv` (this supercedes it, since `video-capture` is older than `media-rs` and doesn't support capturing compressed video frames) |
| 45 | +* `camera-enumeration-windows` |
| 46 | + |
| 47 | +## Building |
| 48 | + |
| 49 | +Requires OpenCV to be installed, follow the instructions here: https://github.com/twistedfall/opencv-rust and ensure the |
| 50 | +pre-requisites are met, here: https://github.com/twistedfall/opencv-rust/blob/master/INSTALL.md |
| 51 | + |
| 52 | +Currently only the following OpenCV modules are used: "imgcodecs", "imgproc", "objdetect", you may be able to build |
| 53 | +opencv with only those modules if you prefer. See this crate's `Cargo.toml` for the latest list of OpenCV modules used. |
| 54 | + |
| 55 | +normally, build with: |
| 56 | + |
| 57 | +``` |
| 58 | +cargo build --release |
| 59 | +``` |
| 60 | + |
| 61 | +### Windows/MSYS2/UCRT64 |
| 62 | + |
| 63 | +``` |
| 64 | +cargo build --target x86_64-pc-windows-gnu --release |
| 65 | +``` |
| 66 | +or |
| 67 | +``` |
| 68 | +rustup run stable-x86_64-pc-windows-gnu cargo build --target x86_64-pc-windows-gnu --release |
| 69 | +``` |
| 70 | + |
| 71 | +### Windows |
| 72 | + |
| 73 | +Tested on |
| 74 | +a) Windows 11, with OpenCV built using vcpkg, using the `x86_64-windows-pc-msvc` toolchain. |
| 75 | +b) Windows 11/MSYS2/UCRT64, with OpenCV installed using `pacman` using the `x86_64-pc-windows-gnu` target. |
| 76 | + |
| 77 | +## Running |
| 78 | + |
| 79 | +OpenCV libraries must be available on the system path. |
| 80 | + |
| 81 | +Linux/macOS: |
| 82 | +``` |
| 83 | +$ ./target/release/videocapture-and-opencv |
| 84 | +``` |
| 85 | + |
| 86 | +### Windows |
| 87 | +``` |
| 88 | +> .\target\release\videocapture-and-opencv.exe |
| 89 | +``` |
| 90 | + |
| 91 | +### Windows/MSYS2/UCRT64 |
| 92 | + |
| 93 | +``` |
| 94 | +$ ./target/x86_64-pc-windows-gnu/release/videocapture-and-opencv.exe |
| 95 | +``` |
| 96 | + |
| 97 | +## OpenCV data files |
| 98 | + |
| 99 | +For face detection, the OpenCV data files are required. |
| 100 | + |
| 101 | +https://github.com/opencv/opencv/tree/4.x/data |
| 102 | + |
| 103 | +Common paths below, the OpenCV path in the UI needs to be set to the same path as the data files, which should contain a `haarcascades` folder. |
| 104 | + |
| 105 | +### Windows/MSYS2/UCRT64 |
| 106 | + |
| 107 | +``` |
| 108 | +C:\msys64\ucrt64\share\opencv4 |
| 109 | +``` |
| 110 | + |
| 111 | +### Windows/VCPKG |
| 112 | + |
| 113 | +This depends on the exact version of OpenCV that was installed by vcpkg when you built OpenCV. |
| 114 | + |
| 115 | +e.g.: |
| 116 | +``` |
| 117 | +D:\Programs\vcpkg\buildtrees\opencv4\src\4.11.0-0357908e41.clean\data |
| 118 | +``` |
| 119 | + |
| 120 | + |
| 121 | +## Donations |
| 122 | + |
| 123 | +If you find this project useful, please consider making a donation via Ko-Fi or Patreon. |
| 124 | + |
| 125 | +* Ko-fi: https://ko-fi.com/dominicclifton |
| 126 | +* Patreon: https://www.patreon.com/MakerPnP |
| 127 | + |
| 128 | +## Links |
| 129 | + |
| 130 | +Please subscribe to be notified of live-stream events so you can follow further developments. |
| 131 | + |
| 132 | +* Patreon: https://www.patreon.com/MakerPnP |
| 133 | +* Source: https://github.com/MakerPnP |
| 134 | +* Discord: https://discord.gg/ffwj5rKZuf |
| 135 | +* YouTube: https://www.youtube.com/@MakerPnP |
| 136 | +* X/Twitter: https://x.com/MakerPicknPlace |
| 137 | + |
| 138 | +## Authors |
| 139 | + |
| 140 | +* Dominic Clifton - Project founder and primary maintainer. |
| 141 | + |
| 142 | +## License |
| 143 | + |
| 144 | +Dual-licensed under Apache or MIT, at your option. |
| 145 | + |
| 146 | +## Contributing |
| 147 | + |
| 148 | +If you'd like to contribute, please raise an issue or a PR on the github issue tracker, work-in-progress PRs are fine |
| 149 | +to let us know you're working on something, and/or visit the discord server. See the  section above. |
0 commit comments