gpt4 book ai didi

android - 如何在Android上使用FaceDetector.Face进行人脸识别

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:05 24 4
gpt4 key购买 nike

这是我在这里的第一篇文章,如果我的问题不清楚或没有提供足够的信息,我很抱歉。

我目前正在开发一个可以从图片中识别人脸的 Android 应用程序。

我的第一个方法是使用 JavaCV,一切正常,除了面部检测需要太多时间才能完成!

之后,我尝试使用 FaceDetector.Face 检测人脸。然后我使用检测到的人脸来训练我的人脸识别器模型。目前没有发现错误。

我的问题是我的模型无法识别 FaceDetector.Face 提供的任何检测到的人脸。我总是从预测函数中得到 -1。谁能告诉我可能出了什么问题?提前致谢!

这是我在检测后裁剪人脸的方式:

    for(int count=0;count<NUMBER_OF_FACE_DETECTED;count++)
{
Face face=detectedFaces[count];
PointF midPoint=new PointF();
face.getMidPoint(midPoint);
eyeDistance=face.eyesDistance();

left = midPoint.x - (float)(1.4 * eyeDistance);
top = midPoint.y - (float)(1.8 * eyeDistance);

bmFace = Bitmap.createBitmap(origiImage, (int) left, (int) top, (int) (2.8 * eyeDistance), (int) (3.6 * eyeDistance));
bmFaces.add(bmFace);
}

这是训练模型的主要部分。

    MatVector images = new MatVector(imageFiles.length);            
int[] labels = new int[imageFiles.length];

IplImage img;
IplImage grayImage;
FaceRecognizer faceRecognizer = createLBPHFaceRecognizer(1, 8, 8, 8, binaryTreshold);
try
{
FileInputStream fstream = new FileInputStream(working_Dir.getAbsolutePath()+"/csv.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String imgInfo;

for (int i = 0; (imgInfo = br.readLine()) != null; i++)
{
String info[] = imgInfo.split(";");

String imagePath = info[0];
img = cvLoadImage(imagePath);
grayImage = IplImage.create(img.width(),img.height(), IPL_DEPTH_8U, 1);
cvCvtColor(img, grayImage, CV_BGR2GRAY);
images.put(i, grayImage);
labels[i] = Integer.parseInt(info[1]);;
}
in.close();

//train the FaceRecognizer model
faceRecognizer.train(images, labels);
}catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}

最后我用下面的代码识别了人脸:

    public static String identifyFace(IplImage grayImg)
{
String predictedName = "";

//identify face from the image
int predictedLabel = faceRecognizer.predict(grayImg);

if(predictedLabel != -1 )
{
predictedName = new String(idToName.get(predictedLabel));
}
return predictedName;
}

最佳答案

这只有在您没有正确设置阈值时才会发生,请参阅文档:

创建LBPHFaceRecognizer 的方法是:

Ptr<FaceRecognizer> createLBPHFaceRecognizer(int radius=1, int neighbors=8, int grid_x=8, int grid_y=8, double threshold=DBL_MAX)

,其中:

  • threshold – 预测中应用的阈值。如果到最近邻居的距离大于阈值,则此方法返回-1。

因此,在上面的方法签名中,您会看到阈值默认设置为 DBL_MAX。因此,如果您只是将阈值排除在外,那么它永远不会产生 -1。另一方面,如果您将阈值设置得太低,FaceRecognizer 总是会产生 -1。也就是说,检查您在代码中将 binaryTreshold 设置为什么。为您的数据找到合适的决策阈值是一个经典的优化问题,您必须根据给定标准(例如,基于错误接受率/错误拒绝率)优化最佳阈值。

关于android - 如何在Android上使用FaceDetector.Face进行人脸识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14207485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com