dotnet / iot

This repo includes .NET Core implementations for various IoT boards, chips, displays and PCBs.
MIT License
2.12k stars 574 forks source link

Iot.Device.Media VideoDevice Capture creates corrupt images with Raspberry PI Camera module 3 #2293

Closed chrisgilliam72 closed 2 months ago

chrisgilliam72 commented 3 months ago

I have been successfully using the code sample I found here: https://neilsnotes.net/Software/Coding/dotnetPiCam.html To capture images from my Raspberry PI with camera module v2.1

I recently bought a new camera module v3. The same code runs but when I try to view the captured image it , the image appears to be corrupt : Error interpreting jpeg image file (not a jpeg file starts with 0x00 0x00)

raffaeler commented 3 months ago

Hi @chrisgilliam72, could you please try the Camera binding instead?

The most probable reason for the VideoDevice not working anymore is because the Rasberry foundation deprecated the "legacy stack" for the newer "libcamera". You can still revert to the first one in the raspi-config, but since I created the Camera, you can work with both stacks.

chrisgilliam72 commented 3 months ago

Thanks for the response . Does the Camera device have something like the NewImageBufferReadyEvent I can hook onto ? I am wanting to stream the images to web page as per the code sample.

raffaeler commented 3 months ago

@chrisgilliam72 The Camera binding runs the official Raspberry PI libcamera-vid utility and takes the video stream back. This means that you get a continuous Stream flow that you can send to a web page. On the binding side, I could not expose that event because the stream content depends on the options you pass to the tool.

The code samples just show how to acquire the stream, but I successfully streamed the byte[] flow to the browser (which needs a video player javascript library). I did this in the past with the H264 stream because it is low-bandwidth, but I had to manually separate the individual frames because the javascript library required the video frame separation. If you don't want to mess with H264, you can change the codec to MJPEG (I believe this was the VideoDevice default codec) but I don't know if the frame separation is still needed (it should be easier to implement.

I planned to post the video frame splitting code, but it will take some time.

chrisgilliam72 commented 3 months ago

Oh I see . Thanks. Do you have any sample code I can work from ? Code that streams using libcamera-vid to a web page ?

raffaeler commented 3 months ago

Unfortunately, the code I use is far more complex and I cannot share it as-is. In the binding sample, you can see how to obtain a Stream. You can use it to feed the communication channel, typically WebSockets or SignalR (which is a .NET library wrapping either Websocket or long polling). This let you send the stream from the server to the client.

In terms of coding, you have to set up a small ASP.NET Core web application that uses straight websockets or the SignalR library. The official documentation walks through a sample sending messages from the server to the browser. This other page shows how to use streaming using SignalR.

HTH

chrisgilliam72 commented 3 months ago

Is there anyway to turn the preview window off ?

raffaeler commented 3 months ago

@chrisgilliam72 sure, you just have to specify the option matching the command line for the libcamera-vid tool which is documented on the Raspberry pi website.

HTH

Woselko commented 3 months ago

Hi @chrisgilliam72, could you please try the Camera binding instead?

The most probable reason for the VideoDevice not working anymore is because the Rasberry foundation deprecated the "legacy stack" for the newer "libcamera". You can still revert to the first one in the raspi-config, but since I created the Camera, you can work with both stacks.

I have checked those camera binding and it looks like it does not work also on RaspBerry Pi 5. Even listing cameras. I have couple of them it is working on RaspBerry Pi 4 but not on 5th.

raffaeler commented 3 months ago

I have checked those camera binding and it looks like it does not work also on RaspBerry Pi 5. Even listing cameras. I have couple of them it is working on RaspBerry Pi 4 but not on 5th.

@Woselko May I ask you to open a separate issue with detailed information? The camera binding runs a separate process with the official tool called libcamera-vid. If the libcamera-vid works from the terminal and does not work from the binding, there is either a misconfiguration or a bug. But if it does not work from the terminal, there is something wrong that has nothing to do with this repo.

Thank you

raffaeler commented 3 months ago

@chrisgilliam72 You should be able to do this adding this line:

var builder = new CommandOptionsBuilder()
// ...
    .With(CommandOptionAndValue.Create(Command.Nopreview));
Woselko commented 3 months ago

I have checked those camera binding and it looks like it does not work also on RaspBerry Pi 5. Even listing cameras. I have couple of them it is working on RaspBerry Pi 4 but not on 5th.

@Woselko May I ask you to open a separate issue with detailed information? The camera binding runs a separate process with the official tool called libcamera-vid. If the libcamera-vid works from the terminal and does not work from the binding, there is either a misconfiguration or a bug. But if it does not work from the terminal, there is something wrong that has nothing to do with this repo.

Thank you

Exactly, it works from terminal but now from the binding. I will open a separate bug.

Can someone explain me why there is so much differences between raspberry pi 4 and raspberry pi 5? Only few devices from this package works from me.

raffaeler commented 3 months ago

Exactly, it works from terminal but now from the binding. I will open a separate bug.

Thank you, please detail the specific error.

Can someone explain me why there is so much differences between raspberry pi 4 and raspberry pi 5? Only few devices from this package works from me.

The Broadcom chipset is different. Did you find a commn root for these issues? It may depend on the libgpiod vs sysfs driver usage (there are relevant issues on the Pi5 and libgpiod). In any case, please open one or more issues on this.

joperezr commented 3 months ago

@Woselko can you clarify which devices stopped working for you in the Pi5? Essentially we are trying to understand if these are devices using I2c communication or GPIO, in order to try to narrow down the issue.

Woselko commented 3 months ago

@joperezr here I made an issue: https://github.com/dotnet/iot/issues/2266 @raffaeler I did not find common root for this issue. Im implementing all of the devices on my own under this repo: https://github.com/Woselko/DotnetRaspBerryPi5Patterns

raffaeler commented 3 months ago

@Woselko

Thank you!

Woselko commented 3 months ago

@raffaeler thanks :), maybe I'll try to contribute some devices that I have that I could not find in repository. @chrisgilliam72 check my LibCamera C# wrapper in my RaspberryPi 5 repo: https://github.com/Woselko/DotnetRaspBerryPi5Patterns there is project called RaspCameraLibrary which is libcamera wrapper and I was able to stream some video locally on raspberry pi 5 server with CameraStreamServer project. image im still new to raspberry platform so it can be some mistakes :)

dotnet-policy-service[bot] commented 3 months ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.