nokhwa: Unable to get camera frame on macbook
Thank you for creating such a great repository! I have a issue when using Mabbook pro 14inc (M1 max). Below are the steps to reproduce the error.
Ventura 13.1 machine MacBook Pro 14-inch 2021 Apple M1 Max rust version 1.66.0 Cargo.toml
name = "show_image"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
show-image="0.13.1"
image = { version="0.24.5" }
nokhwa = { version = "0.10.3", features=["input-native", "output-threaded"] }
Code (main.rs)
use nokhwa::{
nokhwa_initialize,
pixel_format::{RgbAFormat, RgbFormat},
query,
utils::{ApiBackend, RequestedFormat, RequestedFormatType},
threaded::CallbackCamera
};
fn main() {
// only needs to be run on OSX
nokhwa_initialize(|granted| {
println!("User said {}", granted);
});
let cameras = query(ApiBackend::Auto).unwrap();
cameras.iter().for_each(|cam| println!("{:?}", cam));
let format = RequestedFormat::new::<RgbFormat>(RequestedFormatType::AbsoluteHighestFrameRate);
let first_camera = cameras.first().unwrap();
let mut threaded = CallbackCamera::new(first_camera.index().clone(), format, |buffer| {
let image = buffer.decode_image::<RgbAFormat>().unwrap();
println!("{}x{} {}", image.width(), image.height(), image.len());
})
.unwrap();
threaded.open_stream().unwrap();
#[allow(clippy::empty_loop)] // keep it running
loop {
let frame = threaded.poll_frame().unwrap();
let image = frame.decode_image::<RgbAFormat>().unwrap();
println!(
"{}x{} {} naripoggers",
image.width(),
image.height(),
image.len()
);
}
}
Error
User said true
CameraInfo { human_name: "FaceTime HD Camera", description: "Apple Inc.: FaceTime HD Camera - AVCaptureDeviceTypeBuiltInWideAngleCamera, Unspecified f0", misc: "47B4B64B-7067-4B9C-AD2B-AE273A71F4B5", index: Index(0) }
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ProcessFrameError { src: NV12, destination: "RGB", error: "bad input buffer size" }', src/main.rs:63:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: ProcessFrameError { src: NV12, destination: "RGB", error: "bad input buffer size" }', src/main.rs:55:57
If not already resolved, I would like to contribute.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 12
- Comments: 30 (7 by maintainers)
Commits related to this issue
- macos: use YUYV for pixel types (#193) kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange kCVPixelFormatType_420YpCbCr8BiPlanarFullRange kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange Fixes #100.... — committed to l1npengtul/nokhwa by astraw 2 years ago
- macos: use YUYV for pixel types kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange kCVPixelFormatType_420YpCbCr8BiPlanarFullRange kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange Fixes l1npengtul#100. ... — committed to repotea-workspace/nokhwa by fewensa 2 years ago
@paviro Hi. My camera captures video frames of type kCVPixelFormatType_422YpCbCr8 aka “2vuy”. There are various types of them, which you can refer to it to determine the type of your own video frames. And this example can be used to analyze and determine your video frame type.
Well for now I just patched
raw_fcc_to_frameformatinnokhwa-bindings-macos/src/lib.rsto returnFrameFormat::YUYVfor 875704438 instead ofFrameFormat::NV12.Now simply doing this works without any extra steps and result in a correct image:
I don’t fully understand as you can see the av-foundation binding does return 846624121 but somehow the same camera in nokhwa seems to return 875704438. Anyhow glad this works for now! Thank you for your support!
@paviro Sorry, I just saw the message and found that you’ve already solved the problem in a simpler and better way. That’s awesome. I debugged it and got a format value of 846624121, which is different from yours. If your value is 875704438, that corresponds to kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange and it doesn’t matter anymore.
Thank you for the tips @Moon1102 will investigate further!
Just change
let [v, y2, u, y1] = [pixel[0] as f32, pixel[1] as f32, pixel[2] as f32, pixel[3] as f32];tolet [u, y1, v, y2] = [pixel[0] as f32, pixel[1] as f32, pixel[2] as f32, pixel[3] as f32];so you can get the image with the correct color.Okay, I just wanted to say that I found a way to start by replacing the code
let image = buffer.decode_image::<RgbAFormat>().unwrap();temporarily with my code, so that I can successfully start using nokhwa, otherwise I would have to accept theNV12: bad input buffer sizeerror. I can’t say I’ve managed to solve the problem completely, but it’s going in a good direction for me~ I’ll take another look at how to fix the bluish pictures sometime soon.@l1npengtul Could you take a look at the code below when you have a moment? This is a test case I wrote where I was able to convert a camera data frame into an image and save it, on Rust v1.79, MacOS 13.5.2 (Apple M1). The only problem now is that the color is bluish, I don’t know why yet, this code works well and the color is correct when I use the av-foundation library.