人脸识别
package com.acts.opencv.demo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.acts.opencv.common.utils.Constants;
import com.acts.opencv.common.web.BaseController;
/**
* OpenCV人脸识别demo
* 创建者 Songer
* 创建时间 2018年3月9日
*/
@Controller
@RequestMapping(value = "demo")
public class DemoController extends BaseController {
private static final Logger logger = LoggerFactory.getLogger(DemoController.class);
@RequestMapping(value = "detectFace")
public void detectFace(HttpServletResponse response, HttpServletRequest request, String url) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.out.println("===========java.library.path:" + System.getProperty("java.library.path"));
logger.info("\nRunning DetectFaceDemo");
String resourcePath = getClass().getResource("/lbpcascade_frontalface.xml").getPath().substring(1);
logger.info("resourcePath============" + resourcePath);
CascadeClassifier faceDetector = new CascadeClassifier(resourcePath);
logger.info("url==============" + Constants.PATH + url);
Mat image = Highgui.imread(Constants.PATH + url);
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
logger.info(String.format("Detected %s faces", faceDetections.toArray().length));
// Draw a bounding box around each face.
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));
}
// Save the visualized detection.
String filename = url.substring(url.lastIndexOf("/"), url.length());
System.out.println(String.format("Writing %s", Constants.PATH + Constants.DEST_IMAGE_PATH + filename));
Highgui.imwrite(Constants.PATH + Constants.DEST_IMAGE_PATH + filename, image);
renderString(response, Constants.SUCCESS);
}
public static void main(String[] args) {
System.out.println("Hello, OpenCV");
// Load the native library.
System.loadLibrary("opencv_java2413");
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.out.println("===========java.library.path:" + System.getProperty("java.library.path"));
}
}
静态类
package com.acts.opencv.common.utils;
import org.springframework.web.context.ContextLoader;
/**
* 常量 创建者 Songer 创建时间 2018年3月09日
*
*/
public class Constants {
public static final String CURRENT_USER = "UserInfo";
public static final String WECHAT_USER = "weChatUserInfo";
public static final String REFERENCE_CODE = "referenceCode";
public static final String SUCCESS = "success";
public static final String ERROR = "error";
public static final String SF_FILE_SEPARATOR = System.getProperty("file.separator");// 文件分隔符
public static final String SF_LINE_SEPARATOR = System.getProperty("line.separator");// 行分隔符
public static final String SF_PATH_SEPARATOR = System.getProperty("path.separator");// 路径分隔符
public static final String PATH = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/");
/**
* 文件
*/
public static final String SOURCE_IMAGE_PATH = Constants.SF_FILE_SEPARATOR + "statics"
+ Constants.SF_FILE_SEPARATOR + "sourceimage" + Constants.SF_FILE_SEPARATOR;// 图片原地址
public static final String DEST_IMAGE_PATH = Constants.SF_FILE_SEPARATOR + "statics" + Constants.SF_FILE_SEPARATOR
+ "destimage" + Constants.SF_FILE_SEPARATOR;// 图片生成地址
/**
* 返回参数规范
*/
/** 区分类型 1 -- 无错误,Code重复 */
public static final String CODE_DUPLICATE = "1";
/** 区分类型 2 -- 无错误,名称重复 */
public static final String NAME_DUPLICATE = "2";
/** 区分类型 3 -- 数量超出 */
public static final String NUMBER_OVER = "3";
/** 区分类型 0 -- 无错误,程序正常执行 */
public static final String NO_ERROR = "0";
/** 区分类型 -1 -- 无错误,返回结果为空 */
public static final String NULL_POINTER = "-1";
/** 区分类型 -2 -- 错误,参数不正确 */
public static final String INCORRECT_PARAMETER = "-2";
/** 区分类型 -3 -- 错误,程序执行错误 */
public static final String PROGRAM_EXECUTION_ERROR = "-3";
/** 区分类型 -5 -- 错误,数据已删除 */
public static final String DATA_DELETED = "-5";
/** 区分类型 -6 -- 错误,参数不一致(验证码) */
public static final String DATA_NOT_SAME = "-6";
/**json文件缺失 */
public static final String NO_JSON_FILE = "-7";
/**
* 分页中可能用到的常量
*/
public static final Integer PAGE_SIZE=10;//一页共有十条内容
}
|