简体   繁体   English

如何从OpenCv中的图像识别人

[英]How to recognize a person from an Image in OpenCv

I made a program that detects a face from an Image: 我编写了一个程序,可以从图像中检测到人脸:

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;

class DetectFaceDemo {
public void run() {
System.out.println("\nRunning DetectFaceDemo");

CascadeClassifier faceDetector = new CascadeClassifier("C:\\Users\\HM\\Documents\\NetBeansProjects\\vision\\src\\lbpcascade_frontalface.xml");
Mat image = Highgui.imread("C:\\Users\\HM\\Downloads\\john-lennon.jpg");

MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);

System.out.println(String.format("Faces detected: %s ", faceDetections.toArray().length));

for (Rect rect : faceDetections.toArray()) {
    Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0), 3);
}

String filename = "detcSuccessful.png";
Highgui.imwrite(filename, image);
    }
}

public class image {
public static void main(String[] args) {
// Load the native library.
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new DetectFaceDemo().run();
    }
}

And i got this: John lennon picture 我得到了: 约翰·列侬图片

Wanna to recognize the guy who is in the Image by the program and show his name after. 想要通过程序识别出图像中的那个人,并以他的名字显示。

Something like: Computer: this guy is John. 像: 计算机:这个人是约翰。

package com.test8;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

class FaceDetection {
    // Load the native library.
    static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME);}

    public static void main(String[] args) {

        System.out.println("\nRunning DetectFaceDemo");

        CascadeClassifier faceDetector = new CascadeClassifier("D:\\AntonKONG\\OpenCV\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
        Mat image = Imgcodecs.imread("src//data//hkid2.png");

        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image, faceDetections);

        System.out.println(String.format("Faces detected: %s ", faceDetections.toArray().length));

        for (Rect rect : faceDetections.toArray()) {
            Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0), 3);
        }

        String filename = "detcSuccessful.png";
        Imgcodecs.imwrite(filename, image);

    }
}

Here is the code I have previously wrote in opencv 2.4 in C++. 这是我以前在C ++中的opencv 2.4中编写的代码。 As I see, you are using JAVA-OpenCV wrapper, so you can easily find JAVA equivalents. 如我所见,您正在使用JAVA-OpenCV包装器,因此您可以轻松找到JAVA等效项。 In the following code, image is vector of images and label is vector of labels. 在下面的代码中, image图像的矢量,而label是标签的矢量。 For instace, consider your search space is limited to three persons: 对于实例,请考虑将您的搜索空间限制为三个人:

0 : John Lenon 
    john1.png  // 100x90 px
    john2.png  // 100x90 px
    john3.png  // 100x90 px
1 : Robert DeNiro
    robert1.png      // 100x90 px
    robert2.png      // 100x90 px
    robert3.png      // 100x90 px
2 : AlPacino
    al1.png    // 100x90 px
    al2.png    // 100x90 px
    al3.png    // 100x90 px

0,1,2 are labels and you can see there are 3 faces correspond to each label. 0,1,2是标签,您可以看到每个标签对应3个面。 Read each image and store it in a Mat object. 读取每个图像并将其存储在Mat对象中。 as below: 如下:

Mat j1,j2,j3; // John Lennon's faces  :: label 0
Mat r1,r2,r3; // Robert Deniro's faces :: label 1
Mat a1,a2,a3; // Al Pacino's faces :: label 2

Create your image vector and label vector as below: 创建您的图像矢量和标签矢量,如下所示:

images:{j1,j2,j3,r1,r2,r3,a1,a2,a3}
labels:{0,0,0,1,1,1,2,2,2}

then, pass them to the following piece of code. 然后,将它们传递给以下代码。

cv::Ptr<cv::FaceRecognizer> model = createEigenFaceRecognizer();
    model->train(images, labels);
    int prediction;
    double confidence;
    model->predict(Frame,prediction,confidence);

Sorry for answering in C++ but I'm sure that there are similar. 很抱歉用C ++回答,但我确定有相似之处。 In addition, make sure which version of opencv you are using. 另外,请确保您使用的是哪个版本的opencv。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM