Home > other >  OpenCVForUnity recognition, error when doing face contrast is too big, I don't know where is wr
OpenCVForUnity recognition, error when doing face contrast is too big, I don't know where is wr

Time:09-20

 
Using UnityEngine;
Using System. The Collections;
using System.Collections.Generic;

Using OpenCVForUnity;
using System.Text;

Public class CamDetectFace: MonoBehaviour
{

Public UILabel lable.
Public UILabel lable2;
Public UITexture _Texture;
//& lt; Summary>
///The web CAM texture.
///& lt;/summary>
Protected WebCamTexture WebCamTexture;
///& lt; Summary>
///The web CAM device.
///& lt;/summary>
Protected WebCamDevice WebCamDevice;

///& lt; Summary>
///The colors.
///& lt;/summary>
Color32 [] colors;

///& lt; Summary>
///The is front facing.
///& lt;/summary>
Public bool isFrontFacing=false;

///& lt; Summary>
///The width.
///& lt;/summary>
Int width=640;

///& lt; Summary>
///The height.
///& lt;/summary>
Int height=480;

///& lt; Summary>
///The rgba mat.
///& lt;/summary>
Mat rgbaMat;

///& lt; Summary>
///The gray mat.
///& lt;/summary>
Mat grayMat;

///& lt; Summary>
///The texture.
///& lt;/summary>
Texture2D texture;

///& lt; Summary>
///The cascade.
///& lt;/summary>
CascadeClassifier cascade;

///& lt; Summary>
///The faces.
///& lt;/summary>
MatOfRect faces;

///& lt; Summary>
///The init done.
///& lt;/summary>
Bool initDone=false;

//Use this for initialization
Void the Start ()
{

StartCoroutine (init ());
}
Public string webcamName;
Private IEnumerator init ()
{
Yield return Application. RequestUserAuthorization (UserAuthorization. WebCam);
WebCamDevice=WebCamTexture. Devices [0];
WebCamTexture=new webCamTexture (webCamDevice. Name, width, height);

The Debug Log (" width "+ webCamTexture. Width +" height "+ webCamTexture. Height +" FPS "+ webCamTexture. RequestedFPS);
//Starts the camera
WebCamTexture. Play ();
While (true)
{
If (webCamTexture. DidUpdateThisFrame)
{
//the Debug Log (" width "+ webCamTexture. Width +" height "+ webCamTexture. Height +" FPS "+ webCamTexture. RequestedFPS);
//the Debug Log (" videoRotationAngle "+ webCamTexture. VideoRotationAngle +" videoVerticallyMirrored "+ webCamTexture. VideoVerticallyMirrored +" isFrongFacing "+ webCamDevice. IsFrontFacing);
Colors=new Color32 [webCamTexture. Width * webCamTexture. Height].
RgbaMat=new Mat (webCamTexture webCamTexture. Height, width, CvType. CV_8UC4);
GrayMat=new Mat (webCamTexture webCamTexture. Height, width, CvType. CV_8UC1);
Texture=new Texture2D (webCamTexture. Width, webCamTexture. Height, TextureFormat RGBA32, false);
GameObject. Transform. EulerAngles=new Vector3 (0, 0, 0);
GameObject. Transform. LocalScale=new Vector3 (2.56 f, 1.92 f, 1);

//cascade=new CascadeClassifier (Utils getFilePath (" H:/app/3 d/SMARTPV/Assets/OpenCVForUnity StreamingAssets/lbpcascade_frontalface XML "));
Cascade=new CascadeClassifier (Utils. GetFilePath (" H:/app/3 d/SMARTPV/Assets/OpenCVForUnity StreamingAssets/haarcascade_frontalface_alt XML "));
Faces=new MatOfRect ();
GameObject. GetComponent (a). The material. MainTexture=texture;
InitDone=true;
break;
}
The else
{
Yield return 0;
}
}
}

//Update is called once per frame
Void the Update ()
{
if (! InitDone) return;
If (webCamTexture. DidUpdateThisFrame)
{
Utils. WebCamTextureToMat (webCamTexture rgbaMat, colors);
If (webCamDevice. IsFrontFacing)
{
If (webCamTexture. VideoRotationAngle==0)
{
Core. Flip (rgbaMat rgbaMat, 1);
}
Else if (webCamTexture videoRotationAngle==90)
{
Core. Flip (rgbaMat rgbaMat, 0).
}
Else if (webCamTexture videoRotationAngle==270)
{
Core. Flip (rgbaMat rgbaMat, 1);
}
}//Imgproc COLOR_RGBA2GRAY
Imgproc. CvtColor (rgbaMat, grayMat, Imgproc COLOR_RGBA2GRAY);
Imgproc. EqualizeHist (grayMat grayMat);
If (cascade!=null)
Cascade. DetectMultiScale (grayMat, faces, 1.1, 2, 2,//TODO: objdetect. CV_HAAR_SCALE_IMAGE
The new Size (50, 50), the new Size ());//the new Size (webCamTexture. Height * 0.2, webCamTexture height * 0.2)
OpenCVForUnity. The Rect [] rects=faces. ToArray ();

MatOfInt labels=new MatOfInt ();
List LabelsList=new List (a);
List Images=new List (a);
Images. The Add (Highgui. Imread (" H:/app/3 d/SMARTPV/Assets/Resources/Texture/dddd_1. PNG ", 0));
LabelsList. Add (0);
Images. The Add (Highgui. Imread (" H:/app/3 d/SMARTPV/Assets/Resources/Texture/dddd_2. PNG ", 0));
LabelsList. Add (0);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related