3

我目前正在从事一个将 OpenCVSharp 集成到 Unity 中的项目,以允许在游戏环境中进行眼动追踪。我已经设法将 OpenCVSharp 集成到 Unity 编辑器中,并且目前在游戏中可以进行眼睛检测(不是跟踪)。它可以在网络摄像头图像中找到你的眼睛,然后在我在场景中显示的纹理上显示它当前检测到它们的位置。

然而,它导致了巨大的 fps 下降,主要是因为每一帧都将网络摄像头纹理转换为 IPL 图像,以便 OpenCV 可以处理它。在完成所有眼睛检测后,它必须将其转换回 2D 纹理以显示在场景中。所以可以理解,它对 CPU 来说太多了。(据我所知,它只在我的 CPU 上使用 1 个内核)。

有没有办法在不将纹理转换为 IPL 图像的情况下进行所有的眼睛检测?或任何其他修复fps下降的方法。我尝试过的一些事情包括:

  • 限制它更新的帧。然而,这只会使它运行平稳,然后在它必须更新的框架上可怕地结结巴巴。

  • 查看线程,但据我所知,Unity 不允许这样做。据我所知,它只在我的 CPU 上使用 1 个内核,这似乎有点傻。如果有办法改变这一点,它可以解决问题吗?

  • 在相机上尝试了不同的分辨率,但游戏实际可以流畅运行的分辨率太小,眼睛无法实际检测到,更不用说跟踪了。

我已经包含了下面的代码,如果您希望在代码编辑器中查看它,这里是C# File的链接。任何建议或帮助将不胜感激!

作为参考,我使用了这里的代码(使用 opencvsharp 进行眼睛检测)

using UnityEngine;
using System.Collections;
using System;
using System.IO;
using OpenCvSharp;
//using System.Xml;

//using OpenCvSharp.Extensions;
//using System.Windows.Media;
//using System.Windows.Media.Imaging;



public class CaptureScript : MonoBehaviour
{
    public GameObject planeObj;
    public WebCamTexture webcamTexture;     //Texture retrieved from the webcam
    public Texture2D texImage;              //Texture to apply to plane
    public string deviceName;

    private int devId = 1;
    private int imWidth = 640;              //camera width
    private int imHeight = 360;             //camera height
    private string errorMsg = "No errors found!";
    static IplImage matrix;                 //Ipl image of the converted webcam texture

    CvColor[] colors = new CvColor[]
    {
        new CvColor(0,0,255),
        new CvColor(0,128,255),
        new CvColor(0,255,255),
        new CvColor(0,255,0),
        new CvColor(255,128,0),
        new CvColor(255,255,0),
        new CvColor(255,0,0),
        new CvColor(255,0,255),
    };

    const double Scale = 1.25;
    const double ScaleFactor = 2.5;
    const int MinNeighbors = 2;


// Use this for initialization
    void Start ()
    {
            //Webcam initialisation
            WebCamDevice[] devices = WebCamTexture.devices;
            Debug.Log ("num:" + devices.Length);

            for (int i=0; i<devices.Length; i++) {
                    print (devices [i].name);
                    if (devices [i].name.CompareTo (deviceName) == 1) {
                            devId = i;
                    }
            }

            if (devId >= 0) {
                    planeObj = GameObject.Find ("Plane");
                    texImage = new Texture2D (imWidth, imHeight, TextureFormat.RGB24, false);
                    webcamTexture = new WebCamTexture (devices [devId].name, imWidth, imHeight, 30);
                    webcamTexture.Play ();

                    matrix = new IplImage (imWidth, imHeight, BitDepth.U8, 3);
            }


    }

    void Update ()
    {
        if (devId >= 0)
        {
                //Convert webcam texture to iplimage
                Texture2DtoIplImage();

            /*DO IMAGE MANIPULATION HERE*/

            //do eye detection on iplimage
            EyeDetection();


            /*END IMAGE MANIPULATION*/

            if (webcamTexture.didUpdateThisFrame) 
            {
                //convert iplimage to texture
                IplImageToTexture2D();
            }

        } 
        else 
        {
            Debug.Log ("Can't find camera!");
        }

    }

    void EyeDetection()
    {

        using(IplImage smallImg = new IplImage(new CvSize(Cv.Round (imWidth/Scale), Cv.Round(imHeight/Scale)),BitDepth.U8, 1))
        {
            using(IplImage gray = new IplImage(matrix.Size, BitDepth.U8, 1))
            {
                Cv.CvtColor (matrix, gray, ColorConversion.BgrToGray);
                Cv.Resize(gray, smallImg, Interpolation.Linear);
                Cv.EqualizeHist(smallImg, smallImg);
            }


            using(CvHaarClassifierCascade cascade = CvHaarClassifierCascade.FromFile (@"C:\Users\User\Documents\opencv\sources\data\haarcascades\haarcascade_eye.xml"))
            using(CvMemStorage storage = new CvMemStorage())
            {
                storage.Clear ();
                CvSeq<CvAvgComp> eyes = Cv.HaarDetectObjects(smallImg, cascade, storage, ScaleFactor, MinNeighbors, 0, new CvSize(30, 30));
                for(int i = 0; i < eyes.Total; i++)
                {
                    CvRect r = eyes[i].Value.Rect;
                    CvPoint center = new CvPoint{ X = Cv.Round ((r.X + r.Width * 0.5) * Scale), Y = Cv.Round((r.Y + r.Height * 0.5) * Scale) };
                    int radius = Cv.Round((r.Width + r.Height) * 0.25 * Scale);
                    matrix.Circle (center, radius, colors[i % 8], 3, LineType.AntiAlias, 0);
                }
            }

        }
    }

    void OnGUI ()
    {
            GUI.Label (new Rect (200, 200, 100, 90), errorMsg);
    }

    void IplImageToTexture2D ()
    {
            int jBackwards = imHeight;

            for (int i = 0; i < imHeight; i++) {
                    for (int j = 0; j < imWidth; j++) {
                            float b = (float)matrix [i, j].Val0;
                            float g = (float)matrix [i, j].Val1;
                            float r = (float)matrix [i, j].Val2;
                            Color color = new Color (r / 255.0f, g / 255.0f, b / 255.0f);


                            jBackwards = imHeight - i - 1; // notice it is jBackward and i
                            texImage.SetPixel (j, jBackwards, color);
                    }
            }
            texImage.Apply ();
            planeObj.renderer.material.mainTexture = texImage;

    }

    void Texture2DtoIplImage ()
    {
            int jBackwards = imHeight;

            for (int v=0; v<imHeight; ++v) {
                    for (int u=0; u<imWidth; ++u) {

                            CvScalar col = new CvScalar ();
                            col.Val0 = (double)webcamTexture.GetPixel (u, v).b * 255;
                            col.Val1 = (double)webcamTexture.GetPixel (u, v).g * 255;
                            col.Val2 = (double)webcamTexture.GetPixel (u, v).r * 255;

                            jBackwards = imHeight - v - 1;

                            matrix.Set2D (jBackwards, u, col);
                            //matrix [jBackwards, u] = col;
                    }
            }
    }
}
4

2 回答 2

1

您可以将这些移出每帧更新循环:

using(CvHaarClassifierCascade cascade = CvHaarClassifierCascade.FromFile (@"C:\Users\User\Documents\opencv\sources\data\haarcascades\haarcascade_eye.xml"))
using(CvMemStorage storage = new CvMemStorage())

没有理由每帧都构建识别器图。

如果您想要真正的速度更新,线程是前进的合乎逻辑的方式,unity 本身没有线程,但如果您小心的话,您可以折叠其他线程。

在主线程上执行纹理-> ipl 图像,然后触发一个事件来触发您的线程。该线程可以完成所有 CV 工作,可能构建 tex2d 然后推回 main 进行渲染。

于 2014-05-28T21:11:14.563 回答
1

如果您使用以下方法,您还应该能够获得一些性能改进:

    Color32[] pixels;
    pixels = new Color32[webcamTexture.width * webcamTexture.height];
    webcamTexture.GetPixels32(pixels);

Unity doco 建议这比调用“GetPixels”要快很多(当然也比为每个像素调用 GetPixel 快),然后您不需要手动将每个 RGB 通道缩放到 255。

于 2015-04-04T12:47:46.747 回答