AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

How do I get AForge to work in Unity?

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

How do I get AForge to work in Unity?

Postby JansenSensei » Thu Aug 01, 2019 9:15 am

Hi all.

First off, I know the title sounds as n00b as can be but I truly hope that my problem is that I am doing something incredibly n00b and that THAT is the only reason why I am struggling so much... Allow me to explain the situation

Here is what I want to do:
1. View my WebCam stream inside of Unity (Using WebCamTexture or AForge, don't care as long as I can get to the next point....)
2. Detect the shutter was pressed and then save a screenshot

That's it.

So I look at the included demo and it looks easy as can be. I run the demo and it works perfectly. The only improvement I could ask for is a better camera so the image looks better. It just works and it works great so I am excited. So I import the AForge dlls into Unity and I import the System.Windows.Forms and System.Drawing DLLs into Unity so it can stop complaining about that... and now I am ready to get started. So to start I copy paste the code from that demo that works so perfectly and... sorry, no devices found. The end. I can go no further.

Unity has said explicitly that the System.Windows.Forms DLL is NOT supported so I thought that must be it... AForge is simply not compatible with Unity... and yet I read on here that others are using it with Unity so then it means the problem must be me... If I take the working code and copy paste it into my project and it doesn't work then surely I must be doing something wrong... but what?

Can someone please point out my mistake(s) for me... please?

Here is what I am trying to do and where I am failing hard:
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Runtime.InteropServices;

public class AudioManagerTest : MonoBehaviour {

    private FilterInfoCollection videoDevices;
    private VideoCaptureDevice videoDevice;
    private VideoCapabilities[] videoCapabilities;
    private VideoCapabilities[] snapshotCapabilities;
    AForge.Controls.VideoSourcePlayer videoSourcePlayer;

    public List<string> DevicesCombo { get; private set; }
    public List<string> VideoResolutionsCombo { get; private set; }
    public List<string> SnapshotResolutionsCombo { get; private set; }

    public bool IsConnected { get; set; }
    public bool CanTakeScreenShot { get { return IsConnected && (snapshotCapabilities.Length != 0); } }
    public int SelectedVideoResolution { get; set; }
    public int SelectedCaptureResolution { get; set; }

    // Use this for initialization
    void Start () {
        DevicesCombo = new List<string>();
        VideoResolutionsCombo = new List<string>();
        SnapshotResolutionsCombo = new List<string>();

        videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        if (videoDevices.Count != 0)
        {
            // add all devices to combo
            foreach (FilterInfo device in videoDevices)
            {
                DevicesCombo.Add(device.Name);
            }
            videoSourcePlayer = new AForge.Controls.VideoSourcePlayer();
            Debug.LogError("Found " +videoDevices.Count);
        }
        else
        {
            DevicesCombo.Add("No DirectShow devices found");
            Debug.LogError("None found");
        }
    }

I am always receiving "None found".

Why?

Thanks in advance
JansenSensei
 
Posts: 7
Joined: Thu Aug 01, 2019 3:57 am

Re: How do I get AForge to work in Unity?

Postby andrew.kirillov » Thu Aug 01, 2019 10:01 am

May not be related, but one thing to check ... In which mode do you run your WinForm and Unity applications? 32-bit or 64-bit mode? Try running Unity app in the same mode as WinForms. Any difference or still reports that no devices are found?
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3453
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: How do I get AForge to work in Unity?

Postby JansenSensei » Thu Aug 01, 2019 11:01 am

I'm sorry... I don't understand what you mean... I don't use WinForms inside of Unity (not even sure you CAN or if so then HOW... Also, I think the editor is 64 bit by default so I am guessing I'm running in 64 bit... Not really sure. Unity asks you if you want to build for 64 bit or 32 bit but I don't recall ever seeing the option of specifying that option during design time. I think it's 64 bit all the time...

In any event, I know my webcam is called "USB Camera" so I tried to create a VideoCaptureDevice using a hard coded string and received no errors. I then asked Unity to provide me with a list of devices so that I could get rid of my hard coding and, again, received "USB Camera" as my device. I then decided to try and go a completely different route than my previous copy-paste example and this new route doesn't throw any errors BUT it doesn't seem to detect the shutter being pressed. I THINK I can call this progress but could you please give this code a once over and just see if you can find a reason why the shutter is not being detected, please?

Also, just as an aside, I used to run this without a VideoSourcePlayer and just added that in afterwards to see what would happen. The VideoCaptureDevice isRunning reports TRUE but the VideoSourcePlayer isRunning reports false. Does that affect the shutter code at all?

Just to be clear: I am not showing anything from the webcam on screen ATM and don't intend to. I am only interested in detecting the shutter press for now and thus the events are only meant to say "Yes, we got fired". That is all I want for now... but it's just not firing. Any idea where I am going wrong..? Hopefully this has gotten us close enough to be a simple "Yes, just add /remove this one line" answer! :D (/me knocks on wood :P )

Thanks
Code: Select all
    // Use this for initialization
    void Start () {
        DevicesCombo = new List<string>();
        VideoResolutionsCombo = new List<string>();
        SnapshotResolutionsCombo = new List<string>();

        var devices = WebCamTexture.devices;
        if (devices.Length > 0)
        {
            // add all devices to combo
            foreach (var d in devices)
                DevicesCombo.Add(d.name);

            videoDevice = new VideoCaptureDevice(devices[0].name);
            videoDevice.ProvideSnapshots = true;
            videoDevice.SnapshotFrame += new NewFrameEventHandler(videoDevice_SnapshotFrame);
            videoDevice.Start();

            videoSourcePlayer = new AForge.Controls.VideoSourcePlayer();
            videoSourcePlayer.VideoSource = videoDevice;
            videoSourcePlayer.Start();
            Debug.LogWarning("Is playing: " + videoDevice.IsRunning);
            Debug.LogWarning("and Is playing: " + videoSourcePlayer.IsRunning);
        }
        else
        {
            DevicesCombo.Add("No DirectShow devices found");
            Debug.LogError("None found");
        }
    }

    // New snapshot frame is available
    private void videoDevice_SnapshotFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Debug.LogWarning("CaptureScreenshot");
    }
JansenSensei
 
Posts: 7
Joined: Thu Aug 01, 2019 3:57 am

Re: How do I get AForge to work in Unity?

Postby andrew.kirillov » Thu Aug 01, 2019 11:12 am

JansenSensei wrote:I know my webcam is called "USB Camera" so I tried to create a VideoCaptureDevice using a hard coded string and received no errors.

This will not work. It is just human readable name for display only. It is not something which will make VideoCaptureDevice work. So no way.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3453
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: How do I get AForge to work in Unity?

Postby JansenSensei » Thu Aug 01, 2019 12:32 pm

Yeah.... I just noticed that the Moniker is used when creating the device and that moniker is nothing at all like the simple to read "USB Camera"

Unfortunately I don't know how to get the Moniker since: videoDevices = new FilterInfoCollection( FilterCategory.VideoInputDevice ) still returns no devices... :(

Which means I am now stuck again :(
JansenSensei
 
Posts: 7
Joined: Thu Aug 01, 2019 3:57 am

Re: How do I get AForge to work in Unity?

Postby JansenSensei » Thu Aug 01, 2019 5:08 pm

Seems it's not just me. This is a post for 2017 dealing with the same issue:
https://answers.unity.com/questions/583 ... forge.html

It would seem to me that the issue should be solvable if only I could find a moniker for the webcam. I noticed FilterInfo.cs makes use of MkParseDisplayName function which, basedon what I've read, is the function that generates the moniker. Some experimentation is needed now but the input parameter is currently the wildcard here... The only thing I can think to pass to it is the human displayable device name... Is THAT how it's supposed to work?

If this doesn't work then I am at my wits end... :(

UPDATE: Calling CreateFilter completely crashes Unity :(
JansenSensei
 
Posts: 7
Joined: Thu Aug 01, 2019 3:57 am




Return to AForge.NET Framework