Giter VIP home page Giter VIP logo

v4l2.net's Introduction

V4L2.NET

V4L2 (Video for Linux version 2) implemented by .NET Core. The required minimum version of .NET Core is 2.1. Theoretically support all Linux devices running .NET Core (.NET Core JIT depends on ARMv7 instructions).

dotnet/iot#664

Install Dependencies

  1. Install v4l-utils.
    sudo apt-get install v4l-utils
    
  2. Install System.Drawing native dependencies.
    sudo apt-get install libc6-dev libgdiplus libx11-dev
    

Getting Started

  1. Create a VideoConnectionSettings and set the parameters for capture.
    VideoConnectionSettings settings = new VideoConnectionSettings(busId: 0)
    {
        CaptureSize = (2560, 1920),
        PixelFormat = PixelFormat.JPEG,
        ExposureType = ExposureType.Auto
    };
  2. Create a communications channel to a video device.
    using VideoDevice device = VideoDevice.Create(settings);
  3. Capture static image
    // Capture static image
    device.Capture("/home/pi/jpg_direct_output.jpg");
    
    // Change capture setting
    device.Settings.PixelFormat = PixelFormat.YUV420;
    
    // Get image stream, convert pixel format and save to file
    MemoryStream ms = device.Capture();
    Color[] colors = VideoDevice.Yv12ToRgb(ms, settings.CaptureSize);
    Bitmap bitmap = VideoDevice.RgbToBitmap(settings.CaptureSize, colors);
    bitmap.Save("/home/pi/yuyv_to_jpg.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
  4. Other methods
    // Get the supported formats of the device
    IEnumerable<PixelFormat> formats = device.GetSupportedPixelFormats();
    // Get the resolutions of the format
    IEnumerable<(uint Width, uint Height)> resolutions = device.GetPixelFormatResolutions(PixelFormat.YUYV));
    // Query v4l2 controls default and current value
    VideoDeviceValue value = device.GetVideoDeviceValue(VideoDeviceValueType.Rotate);

Run the sample

cd V4l2.Samples
dotnet publish -c release -r linux-arm -o YOUR_FOLDER
sudo dotnet YOUR_FOLDER/V4l2.Samples.dll

Run the sample with Docker

Before build docker image, you need to modify SDK, runtime and apt sources(in China) to adapt to the corresponding Linux platform.

docker build -t v4l2-sample -f Dockerfile .
docker run --rm -it --device /dev/video0 -v /home/pi/images:/home/pi/images v4l2-sample

v4l2.net's People

Contributors

zhanggaoxing avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

v4l2.net's Issues

Not capturing an Image

So to begin with, here is my code:

using System;
using System.Collections.Generic;
using Iot.Device.Media;

namespace FaceMod
{
    class Program
    {
        static void Main(string[] args)
        {
            VideoConnectionSettings settings = new VideoConnectionSettings(busId: 0)
            {
                CaptureSize = (640, 480), ExposureType = ExposureType.Auto, PixelFormat = PixelFormat.MJPEG
            };
            using VideoDevice device = VideoDevice.Create(settings);
            IEnumerable<PixelFormat> formats = device.GetSupportedPixelFormats();
            foreach (var f in formats)
            {
                Console.WriteLine(f);
            }
            IEnumerable<(uint Width, uint Height)> resolutions = device.GetPixelFormatResolutions(PixelFormat.YUYV);
            foreach (var res in resolutions)
            {
                Console.WriteLine(res.Width + "x" + res.Height);
            }
            //Manually Verified that all settings are valid and my webcam is capable. Or at least claims to be.
            var cap = device.Capture();
            Console.WriteLine(cap.Length);
            //cap.Length is 0 - Nothing is ever captured.
            Console.ReadKey();
            
        } 
    }
}

The problem I am having is that I cannot get it to capture an image - Writing it to disk directly with the built in writes a 0 byte file, and the MemoryStream is always empty. I know that it does at least get as far as turning on the webcam, because it does manage to do that. It just never captures an image.

Any ideas?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.