您的位置:首页 > 编程语言 > Java开发

Java之Opencv人脸识别-yellowcong

2018-01-06 12:59 399 查看
通过opencv来识别浙大的美女,是不是爽歪歪啊,其中我们只要有越丰富的人脸模型,我们的识别就可以更加的精确,这个模型不是很精确,有的人脸没有识别上

项目地址

#码云地址 https://gitee.com/yellowcong/opencv 
#github https://github.com/yellowcong/opencv[/code] 

案例



识别后的美女



目录结构

pics这个目录用来放需要识别的图片

cascade_storage.xml 这个是人脸识别的模型

这个jar包在中央仓库配有所以 自己直接放到项目了



代码

package com.yellowcong.face;

import java.io.File;

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;

/**
* 创建日期:2018年1月6日<br/>
* 创建时间:上午11:59:19<br/>
* 创建者    :yellowcong<br/>
* 机能概要:
*/
public class FaceDemo {

public static void main(String[] args) {

//图片地址
String inputImagePath = FaceDemo.class.getClassLoader().getResource("pics/demo.jpg").getFile();
// 指定读出的图片路径和输出的文件
String outputImageFile = "D:/demo_identificate.png";

//加载lib,这个lib的名称
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

//读取图像的 mat信息
Mat image = Highgui.imread(new File(inputImagePath).getAbsolutePath());

//初始化人脸识别类
MatOfRect faceDetections = new MatOfRect();
//添加识别模型
String xmlPath = FaceDemo.class.getClassLoader().getResource("cascade_storage.xml").getPath().substring(1);
CascadeClassifier faceDetector = new CascadeClassifier(xmlPath);
//识别脸
faceDetector.detectMultiScale(image, faceDetections);

// 画出脸的位置
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, 0, 255), 2);
}

// 写入到文件
Highgui.imwrite(outputImageFile, image);

System.out.print("图片已经识别");
}
}


环境搭建

添加lib到maven的路径中

注意,直接添加到jar包的路径上,是没有用的,只能添加到maven这个上面



jar 添加pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>yellowcong</groupId>
<artifactId>day01_06</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>day01_06</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>opencv</groupId>
<artifactId>opencv-2413</artifactId>
<version>opencv-2413</version>
<scope>system</scope>
<optional>true</optional>
<systemPath>${project.basedir}/src/main/lib/opencv/opencv-2413.jar</systemPath>
</dependency>
</dependencies>
</project>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: