Skip to content

Commit 22033e9

Browse files
committed
Port videocapture-and-opencv experimentation to media-rs-and-opencv.
1 parent 90180a2 commit 22033e9

8 files changed

Lines changed: 7160 additions & 193 deletions

File tree

experimentation/media-rs-and-opencv/Cargo.lock

Lines changed: 5620 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[package]
2+
name = "videocapture-and-opencv"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[features]
7+
opencv-410 = []
8+
opencv-411 = []
9+
default = ["opencv-411"]
10+
linux = [
11+
# required to avoid compilation error: "a `libclang` shared library is not loaded on this thread"
12+
"opencv/clang-runtime"
13+
]
14+
15+
[dependencies]
16+
# Computer Vision and Image Processing
17+
opencv = { version = "0.97.2", features = ["imgcodecs", "imgproc", "objdetect"], default-features = false }
18+
# currently need a version of media-rs with linux support (via libcamera)
19+
media = { git = "https://github.com/MakerPnP/media-rs", rev = "0db5eb9166edda92f6d6b141e2856c1f6935fd51" }
20+
#media = { path = "../../../media-rs" }
21+
image = { version = "0.25.8" }
22+
23+
# GUI Framework and User Interface
24+
egui = { version = "0.33.0", features = ["persistence"] }
25+
eframe = { version = "0.33.2", features = ["ron", "serde", "persistence"] }
26+
egui_ltreeview = { version = "0.6.0", features = ["persistence"] }
27+
28+
# Time and Date Handling
29+
chrono = { version = "0.4.42" }
30+
31+
# Logging and Debugging
32+
env_logger = { version = "0.11.8" }
33+
log = { version = "0.4.28", features = ["release_max_level_debug"] }
34+
35+
# Serialization and Deserialization
36+
serde = { version = "1.0.228", features = ["derive"] }
37+
38+
[dev-dependencies]
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<div align="center">
2+
3+
[![Discord](https://img.shields.io/discord/1255867192503832688?label=MakerPnP%20discord&color=%2332c955)](https://discord.gg/ffwj5rKZuf)
4+
[![YouTube Channel Subscribers](https://img.shields.io/youtube/channel/subscribers/UClzmlBRrChCJCXkY2h9GhBQ?style=flat&color=%2332c955)](https://www.youtube.com/channel/UClzmlBRrChCJCXkY2h9GhBQ?sub_confirmation=1)
5+
[![MakerPnP GitHub Organization's stars](https://img.shields.io/github/stars/makerpnp?style=flat&color=%2332c955)](https://github.com/MakerPnP)
6+
[![Donate via Ko-Fi](https://img.shields.io/badge/Ko--Fi-Donate-green?style=flat&color=%2332c955&logo=ko-fi)](https://ko-fi.com/dominicclifton)
7+
[![Subscribe on Patreon](https://img.shields.io/badge/Patreon-Subscribe-green?style=flat&color=%2332c955&logo=patreon)](https://www.patreon.com/MakerPnP)
8+
9+
![MakerPnP](assets/logos/makerpnp_icon_1_384x384.png)
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 ![Links](#links) section above.

0 commit comments

Comments
 (0)