-
-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to get camera frame on macbook #100
Comments
camera.info
's resolution is different from number of camera.frame
's slice elements
#72 (comment) |
It is likely a fault of the library improperly setting FourCC |
I found that the first problem is the NV12 checker in this line: nokhwa/nokhwa-core/src/types.rs Line 1445 in ad93e0a
I tried to change it into
Now it passes the decoding round. But the resulting decoded image is faulty. It looks like it's split in the middle, and then we put the left part on the right and the right part on the left. I feel I am close to a solution, but can't really point it out |
Does not work on Macbook Air 2014 13". Same error. |
Anyone have a work around for this? All cams in macOS are coming in as NV12 and hitting the same |
Damn, I just hit this problem, too. Until NV12 support comes in v0.11, it's possible to use a crate like DCV Color Primitives to do the necessary conversion. |
Hi, is there any workaround for this issue? |
Hi, I'm seeing this issue as well. I think the problem is that AVfoundation returns frames in Here is my current workaround to deal with this kind of data (note that this only works on Rust nightly). Alternativly, you could split the interleaved planes and use the aforementioned dcv-color-primitives crate for conversion. #![feature(portable_simd)]
use std::simd::SimdFloat;
use std::simd::f32x4;
use rayon::prelude::*;
#[inline]
pub fn uyvy_to_rgb24(in_buf: &[u8], out_buf: &mut [u8]) {
debug_assert!(out_buf.len() as f32 == in_buf.len() as f32 * 1.5);
in_buf
.par_chunks_exact(4) // FIXME: use par_array_chunks() when stabalized (https://github.com/rayon-rs/rayon/pull/789)
.zip(out_buf.par_chunks_exact_mut(6))
.for_each(|(ch, out)| {
let y1 = ch[1];
let y2 = ch[3];
let cb = ch[0];
let cr = ch[2];
let (r, g, b) = ycbcr_to_rgb(y1, cb, cr);
out[0] = r;
out[1] = g;
out[2] = b;
let (r, g, b) = ycbcr_to_rgb(y2, cb, cr);
out[3] = r;
out[4] = g;
out[5] = b;
});
}
// COLOR CONVERSION: https://stackoverflow.com/questions/28079010/rgb-to-ycbcr-using-simd-vectors-lose-some-data
#[inline]
fn ycbcr_to_rgb(y: u8, cb: u8, cr: u8) -> (u8, u8, u8) {
let ycbcr = f32x4::from_array([y as f32, cb as f32 - 128.0f32, cr as f32 - 128.0f32, 0.0]);
// rec 709: https://mymusing.co/bt-709-yuv-to-rgb-conversion-color/
let r = (ycbcr * f32x4::from_array([1.0, 0.00000, 1.5748, 0.0])).reduce_sum();
let g = (ycbcr * f32x4::from_array([1.0, -0.187324, -0.468124, 0.0])).reduce_sum();
let b = (ycbcr * f32x4::from_array([1.0, 1.8556, 0.00000, 0.0])).reduce_sum();
(clamp(r), clamp(g), clamp(b))
}
#[inline]
fn clamp(val: f32) -> u8 {
if val < 0.0 {
0
} else if val > 255.0 {
255
} else {
val.round() as u8
}
} (adapted from here: https://gist.github.com/arifd/ea820ec97265a023e67a88b66955855d) As a sidenote, it looks like recent MacOS versions do not support transparent raw access to the camera outputs anymore, i.e. you will always be offered @l1npengtul, not sure what the current status is here (I'm a bit confused with the different branches) but I might be able to help you with debugging this. Edit: Forgot to say that this is on a M2 MacBook Pro on Ventura 13.4. |
see #151 |
@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.
|
Honestly, the current avfoundation code is in such a state that Ive blanked it out of my mind. I really cant help you here, unfortunately. I'll work on making nokhwa itself use avfoundation. |
Okay, I just wanted to say that I found a way to start by replacing the code |
Just change |
Thanks @Moon1102 for the proposed workaround. It works on my MacBook for the integrated camera, with M1 CPU and macOS 14.5! Do you know whether this affects only Macs with Apple Silicon or all Macs? Or does it depend on the camera? |
@raphaelmenges Sorry, at the moment I only have a MacBook with an Apple Silicon M1 and an integrated camera, so I'm not sure if it will work properly on other types of Macs. I think the code for nokhwa's codecs may need further improvement in terms of adapting to individual devices now, perhaps will better after version 0.11. Also, I'm curious if you're experiencing error #165? i haven't found a solution yet. |
@Moon1102 using your code I still get this result: |
@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. |
Thank you for the tips @Moon1102 will investigate further! |
@Moon1102 My camera also seems to output 2vuy. Not sure what I am doing wrong? Could you give me any more hints? Totally fine if you don't want to sadly I am a bit new to rust and the video world.
|
Well for now I just patched
Now simply doing this works without any extra steps and result in a correct image:
|
@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. |
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! |
Running into the same issue and I've applied the same patch, I get the same pixel format type so I applied this function you posted here #100 (comment) to the raw buffer and saved it as jpg and got this dark image under bright light I'm on a M3 pro max btw, I'd appreciate it If anyone can help in anyway @paviro @Moon1102 @l1npengtul |
I'll see what I can do, but this will likely be fixed in 0.11 since we are switching to the avfoundation bindings crate instead of the current error prone msg send implementation. |
It also doesn't help I don't have a mac to test this with... up to this point mac support was mostly me Winging It(tm) |
Thanks for your good work 🙏🏽, Is there any temporary solution around this? It's been kicking my ass for a while now. I even tried ffmpeg to raw image conversion and still got the same result. How soon is 0.11 going to be out? I'd love to help anyway I can too |
Its probably AVFoundation backend and improperly set FrameFormat values.
Follow tracking issue #86
I suspect the blurry image is due to auto focus. |
I'll keep tracking the issue in the mean time, thanks! |
I followed the idea from @paviro above and made a PR with a fix (#193) that works on my M1 Macbook Pro. You can use it already by using this in your
|
kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange kCVPixelFormatType_420YpCbCr8BiPlanarFullRange kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange Fixes #100. This was suggested by the code from @paviro at #100 (comment).
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
Code (
main.rs
)Error
If not already resolved, I would like to contribute.
The text was updated successfully, but these errors were encountered: