GithubHelp home page GithubHelp logo

Camera preview locked about usbcamera HOT 14 CLOSED

Onafets51 avatar Onafets51 commented on July 20, 2024
Camera preview locked

from usbcamera.

Comments (14)

secile avatar secile commented on July 20, 2024

Hello, Thank you for your question.

  1. As I described in comment in line 32, immediately after starting the USB camera,
    GetBitmap() fails because image buffer is not prepared yet.
    What you are saying is this situation?

  2. If not,
    I don't know if it works, but could you try the following change around line 318?
    And please let me know it work fine or not.

before change.

public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
    if (Buffer == null || Buffer.Length != BufferLen)
    {
        Buffer = new byte[BufferLen];
    }

    lock (BufferLock)
    {
        Marshal.Copy(pBuffer, Buffer, 0, BufferLen);
    }
    return 0;
}

after change.

public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
    lock (BufferLock)
    {
        if (Buffer == null || Buffer.Length != BufferLen)
        {
            Buffer = new byte[BufferLen];
        }

        Marshal.Copy(pBuffer, Buffer, 0, BufferLen);
    }
    return 0;
}

from usbcamera.

Onafets51 avatar Onafets51 commented on July 20, 2024

Thanks for your suggestion, I modified the code as shown. The defect seems less frequent, moreover it does it especially when the camera is cold and it happens also with AMCAP. With their test program IC Capture (The Imagin Source) it never happens.

from usbcamera.

secile avatar secile commented on July 20, 2024

Hello.

  1. Could you try the following change too, from line 306.

before change:

GetBitmap(int width, int height, int stride)
{
    if (Buffer == null) return EmptyBitmap(width, height);

    lock (BufferLock)
    {
        return BufferToBitmap(Buffer, width, height, stride);
    }
}

after change:

GetBitmap(int width, int height, int stride)
{
    lock (BufferLock)
    {
        if (Buffer == null) return EmptyBitmap(width, height);

        return BufferToBitmap(Buffer, width, height, stride);
    }
}
  1. If the problem is not solved yot,
    could you check VideoFormat your camera supports?
    Execute below and let me know result.
// check format.
int cameraIndex = 0;
UsbCamera.VideoFormat[] formats = UsbCamera.GetVideoFormat(cameraIndex);
for(int i=0; i<formats.Length; i++) Console.WriteLine("{0}:{1}", i, formats[i]);

from usbcamera.

secile avatar secile commented on July 20, 2024

Hello.
I found that it is necessary to return quickly in callback function.
if callback does not return quickly, it can interfere with playback.
https://docs.microsoft.com/en-us/windows/win32/directshow/isamplegrabber-setcallback

This is previous version document, it is prohibited to enter critical section.
https://docs.microsoft.com/en-us/previous-versions/ms786692(v=vs.85)

Could you try this version?
I changed do not enter critical section and do not wait another thread.
And please let me know your problem is solved.

public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
    if (Buffer == null || Buffer.Length != BufferLen)
    {
        Buffer = new byte[BufferLen];
    }

    var locked = false;
    try
    {
        System.Threading.Monitor.TryEnter(BufferLock, 0, ref locked);
        if (locked)
        {
            Marshal.Copy(pBuffer, Buffer, 0, BufferLen);
        }
    }
    finally
    {
        if (locked) System.Threading.Monitor.Exit(BufferLock);
    }
    return 0;
}

from usbcamera.

Onafets51 avatar Onafets51 commented on July 20, 2024

Hello,

I modified as follows:

public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
if (Buffer == null || Buffer.Length != BufferLen)
{
Buffer = new byte[BufferLen];
}

var locked = false;
try
{
    System.Threading.Monitor.TryEnter(BufferLock, 0, ref locked);
    if (locked)
    {
        Marshal.Copy(pBuffer, Buffer, 0, BufferLen);
    }
}
finally
{
    if (locked) System.Threading.Monitor.Exit(BufferLock);
}
return 0;

}

But the image freeze with the cold board continues; then the board warms up and does not freeze anymore. I immediately thought of a hardware problem since it freezes with the old AMCAP; but as I told you this problem never happens with their (the Imaging Source) IC Capture 2.5 program. Anyway I bought a new camera-board that will arrive next month.For the moment I put a 200 ms timer that compares the previous frame with the next one (I reduce the frames to 1/16 to speed up), when they are identical (normally there is always some flickering) I give the commands Camera.Stop(), Camera.Start(). But it's not very nice.

from usbcamera.

Onafets51 avatar Onafets51 commented on July 20, 2024

AMCAP, as UsbCamera is based on DirectShow, while IC Capture is a software based on IC Imaging Control .NET Component for C# and VB.NET projects.

from usbcamera.

Onafets51 avatar Onafets51 commented on July 20, 2024

I also tried an old version of UsbCamera where there was no callback function but there was GetBitmapMain and GetBitmapMainMain, but even with that same problem. Waiting for the new camera-board to arrive to draw conclusions.

from usbcamera.

secile avatar secile commented on July 20, 2024

Thank you for your response.

One last thing I want to check.
Please execute following code and check output message.

// check format.
int cameraIndex = 0;
UsbCamera.VideoFormat[] formats = UsbCamera.GetVideoFormat(cameraIndex);
for(int i=0; i<formats.Length; i++) Console.WriteLine("{0}:{1}", i, formats[i]);

for example, this is my result.

0:[Video], [MJPG], {Width=1280, Height=720}, 333333, [VideoInfo], ...
1:[Video], [MJPG], {Width=320, Height=180}, 333333, [VideoInfo], ...
2:[Video], [MJPG], {Width=320, Height=240}, 333333, [VideoInfo], ...
...
9:[Video], [YUY2], {Width=1280, Height=720}, 1000000, [VideoInfo], ...
10:[Video], [YUY2], {Width=320, Height=180}, 333333, [VideoInfo], ...
11:[Video], [YUY2], {Width=320, Height=240}, 333333, [VideoInfo], ...
...
18:[Video], [RGB24], {Width=1280, Height=720}, 1000000, [VideoInfo], ...
19:[Video], [RGB24], {Width=320, Height=180}, 333333, [VideoInfo], ...
20:[Video], [RGB24], {Width=320, Height=240}, 333333, [VideoInfo], ...

Now, if you are using formats[0], (that is [MJPG]),

var camera = new UsbCamera(0, formats[0]);

please use same size but another format like formats[9] (that is [YUY2]) or formats[18] (that is [RGB24])?

from usbcamera.

Onafets51 avatar Onafets51 commented on July 20, 2024

Schermata 2022-03-24 alle 17 33 41

I hope you can see

from usbcamera.

Onafets51 avatar Onafets51 commented on July 20, 2024

I use format #5

from usbcamera.

Onafets51 avatar Onafets51 commented on July 20, 2024

The camera is black and white, that's why I had chosen the Y800 1280x960 format. Now however, after setting the RGB24 1280x960 format it seems that the defect does not. I need to try longer, but it already seems significant.

from usbcamera.

secile avatar secile commented on July 20, 2024

I have one question. When you used format #5 (Y800), aside from occasional preview freeze, are you able to get correct image? (unbroken image?)

BufferToBitmap function works correctly if image memory layout is RGB24. But you are using Y800 format and it's memory layout is difference from RGB24.

from usbcamera.

Onafets51 avatar Onafets51 commented on July 20, 2024

Hi,
Yes with Y800 format #5 and also with Y16 format #1 I had a correct image. However, after the latest changes (format RGB24 format #9), on my development machine (CPU Intel Core i7 8 core 3.8 GHz) the problem did not occur anymore but, on a tablet with CPU Celeron Gemini Lake N4120 4 core 2.4 GHz instead it manifested itself. I'm waiting to receive the new camera to do more tests.

from usbcamera.

secile avatar secile commented on July 20, 2024

Thank you. I'm feeling little strangeness, but I accept the result.
I guess SampleGrabber (parts of DirectShow) converted Y16/Y800 to RGB24 appropriately.

I close this issue. If you have any question, ask me again, but I have not so much idea any more.

from usbcamera.

Related Issues (20)

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.