OpenCVForUnity 识别后,在做人脸对比时误差太大,不知道哪里错了,有经验的帮忙看下

weixin_35953077 2016-08-25 11:07:46

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

using OpenCVForUnity;
using System.Text;

public class CamDetectFace : MonoBehaviour
{

public UILabel lable;
public UILabel lable2;
public UITexture _Texture;
// <summary>
/// The web cam texture.
/// </summary>
protected WebCamTexture webCamTexture;
/// <summary>
/// The web cam device.
/// </summary>
protected WebCamDevice webCamDevice;

/// <summary>
/// The colors.
/// </summary>
Color32[] colors;

/// <summary>
/// The is front facing.
/// </summary>
public bool isFrontFacing = false;

/// <summary>
/// The width.
/// </summary>
int width =640 ;

/// <summary>
/// The height.
/// </summary>
int height =480;

/// <summary>
/// The rgba mat.
/// </summary>
Mat rgbaMat;

/// <summary>
/// The gray mat.
/// </summary>
Mat grayMat;

/// <summary>
/// The texture.
/// </summary>
Texture2D texture;

/// <summary>
/// The cascade.
/// </summary>
CascadeClassifier cascade;

/// <summary>
/// The faces.
/// </summary>
MatOfRect faces;

/// <summary>
/// The init done.
/// </summary>
bool initDone = false;

// Use this for initialization
void 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);

Debug.Log("width " + webCamTexture.width + " height " + webCamTexture.height + " fps " + webCamTexture.requestedFPS);
// Starts the camera
webCamTexture.Play();
while (true)
{
if (webCamTexture.didUpdateThisFrame)
{
//Debug.Log("width " + webCamTexture.width + " height " + webCamTexture.height + " fps " + webCamTexture.requestedFPS);
//Debug.Log("videoRotationAngle " + webCamTexture.videoRotationAngle + " videoVerticallyMirrored " + webCamTexture.videoVerticallyMirrored + " isFrongFacing " + webCamDevice.isFrontFacing);
colors = new Color32[webCamTexture.width * webCamTexture.height];
rgbaMat = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC4);
grayMat = new Mat(webCamTexture.height, webCamTexture.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.56f,1.92f, 1);

// cascade = new CascadeClassifier(Utils.getFilePath("H:/app/3d/SMARTPV/Assets/OpenCVForUnity/StreamingAssets/lbpcascade_frontalface.xml"));
cascade = new CascadeClassifier(Utils.getFilePath("H:/app/3d/SMARTPV/Assets/OpenCVForUnity/StreamingAssets/haarcascade_frontalface_alt.xml"));
faces = new MatOfRect();
gameObject.GetComponent<Renderer>().material.mainTexture = texture;
initDone = true;
break;
}
else
{
yield return 0;
}
}
}

// Update is called once per frame
void 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
new Size(50,50), new Size());// new Size(webCamTexture.height * 0.2, webCamTexture.height * 0.2)
OpenCVForUnity.Rect[] rects = faces.toArray();

MatOfInt labels = new MatOfInt();
List<int> labelsList = new List<int>();
List<Mat> images = new List<Mat>();
images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/dddd_1.png", 0));
labelsList.Add(0);
images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/dddd_2.png", 0));
labelsList.Add(0);
images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/dddd_3.png", 0));
labelsList.Add(0);
images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/dddd_4.png", 0));
labelsList.Add(0);
images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/dddd_5.png", 0));
labelsList.Add(0);
images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/dddd_6.png", 0));
labelsList.Add(0);

//images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/Test/0.jpg", 0));
//labelsList.Add(1);
//images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/Test/1.jpg", 0));
//labelsList.Add(1);
//images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/Test/2.jpg", 0));
//labelsList.Add(1);
//images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/Test/3.jpg", 0));
//labelsList.Add(1);
//images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/Test/4.jpg", 0));
//labelsList.Add(2);
//images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/Test/5.jpg", 0));
//labelsList.Add(2);
//images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/Test/6.jpg", 0));
//labelsList.Add(2);
//images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/Test/7.jpg", 0));
//labelsList.Add(3);
//images.Add(Highgui.imread("H:/app/3d/SMARTPV/Assets/Resources/Texture/Test/8.jpg", 0));
//labelsList.Add(3);
labels.fromList(labelsList);

int[] predictedLabel = new int[1];
double[] predictedConfidence = new double[1];

FaceRecognizer faceRecognizer = FaceRecognizer.createEigenFaceRecognizer();
faceRecognizer.train(images, labels);

string strSubstring = "";
for (int i = 0; i < rects.Length; i++)
{
Size dsize = new Size(64, 64);
Mat testSampleMat = new Mat(dsize, Imgproc.COLOR_RGBA2GRAY); //Imgproc.COLOR_RGBA2GRAY
Imgproc.resize(grayMat.submat(rects[i]), testSampleMat, dsize);
faceRecognizer.predict(testSampleMat, predictedLabel, predictedConfidence);

Texture2D texturess = new Texture2D(testSampleMat.cols(), testSampleMat.rows(), TextureFormat.RGBA32, false);//
Utils.matToTexture2D(testSampleMat, texturess);
_Texture.mainTexture = texturess;

string aaa = predictedConfidence[0] <=3000 ? "乔磊" : "不认识";
Scalar s = predictedConfidence[0] <= 3000 ? new Scalar(255, 0, 0, 255) : new Scalar(9, 255, 0, 255);
Core.putText(rgbaMat, predictedLabel[0].ToString(), new Point(rects[i].x + 10, rects[i].y + 20), Core.FONT_HERSHEY_COMPLEX, 0.4, s);
Core.rectangle(rgbaMat, new Point(rects[i].x, rects[i].y), new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), s, 2);

strSubstring += i + "#人脸[" + aaa + "] 误差值:" + predictedConfidence[0] + " |||" + predictedLabel[0] + "\n\t";
}
lable.text = "检测到人脸数量:" + rects.Length;
lable2.text = strSubstring;
//rgbaMat
Utils.matToTexture2D(rgbaMat, texture, colors);
}
}
void OnDisable()
{
webCamTexture.Stop();
}

}



...全文
3036 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
telepath1st 2018-01-04
  • 打赏
  • 举报
回复
这个高级了,研究了
wswtfxd 2018-01-04
  • 打赏
  • 举报
回复
图片怎么截取的????
于子潇 2017-08-17
  • 打赏
  • 举报
回复
大神 , 怎么做训练啊?
weixin_35953077 2016-08-25
  • 打赏
  • 举报
回复
这是训练的图片
生活在他方 2016-08-25
  • 打赏
  • 举报
回复
没用过啊。。。。。。。。。。

2,543

社区成员

发帖
与我相关
我的任务
社区描述
Unity3D相关内容讨论专区
游戏unity 技术论坛(原bbs)
社区管理员
  • Unity3D
  • 芝麻粒儿
  • 「已注销」
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

Unity3D社区公告:

  1. 社区致力于解决各种Unity3D相关的“疑难杂症”。
  2. 社区不允许发布与Unity3D或相关技术无关内容。
  3. 社区版主邀请各位一道为打造优秀社区不懈努力。

试试用AI创作助手写篇文章吧