This commit is contained in:
137
jnpf-ftb/jnpf-ftb-biz/src/main/java/jnpf/util/SeetaFaceUtil.java
Normal file
137
jnpf-ftb/jnpf-ftb-biz/src/main/java/jnpf/util/SeetaFaceUtil.java
Normal file
@@ -0,0 +1,137 @@
|
||||
//package jnpf.util;
|
||||
//
|
||||
//import com.seeta.sdk.*;
|
||||
//import com.seeta.sdk.util.SeetafaceUtil;
|
||||
//import jnpf.exception.HandleException;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.springframework.web.multipart.MultipartFile;
|
||||
//
|
||||
//import javax.imageio.ImageIO;
|
||||
//import java.awt.image.BufferedImage;
|
||||
//import java.io.InputStream;
|
||||
//import java.net.HttpURLConnection;
|
||||
//import java.net.URL;
|
||||
//
|
||||
///**
|
||||
// * 人脸识别工具类
|
||||
// *
|
||||
// * @author yanwenfu
|
||||
// * @create 2025-04-08
|
||||
// */
|
||||
//@Slf4j
|
||||
//public class SeetaFaceUtil {
|
||||
//
|
||||
// //人脸检测器
|
||||
// private static final FaceDetector detector;
|
||||
// //关键点定位器 5点
|
||||
// private static final FaceLandmarker faceLandmarker;
|
||||
// //人脸向量特征提取和对比器
|
||||
// private static final FaceRecognizer faceRecognizer;
|
||||
// static {
|
||||
// try {
|
||||
// detector = new FaceDetector(new SeetaModelSetting(SeetaConstant.face_detector, SeetaDevice.SEETA_DEVICE_AUTO));
|
||||
// faceLandmarker = new FaceLandmarker(new SeetaModelSetting(SeetaConstant.face_landmarker_pts5, SeetaDevice.SEETA_DEVICE_AUTO));
|
||||
// faceRecognizer = new FaceRecognizer(new SeetaModelSetting(SeetaConstant.face_recognizer, SeetaDevice.SEETA_DEVICE_AUTO));
|
||||
// } catch (Exception e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 资源释放方法
|
||||
// public static synchronized void releaseModels() {
|
||||
// try {
|
||||
// if (detector != null) {
|
||||
// detector.dispose(); // 实际方法名以SDK为准
|
||||
// }
|
||||
// if (faceLandmarker != null) {
|
||||
// faceLandmarker.dispose();
|
||||
// }
|
||||
// if (faceRecognizer != null) {
|
||||
// faceRecognizer.dispose();
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// log.error("模型资源释放异常", e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取照片特征(参数必须传递一个)
|
||||
// * @param file 文件
|
||||
// * @param url 远程路径
|
||||
// * @return float[]
|
||||
// */
|
||||
// public static float[] analyzeEcognizer(MultipartFile file, String url) throws HandleException {
|
||||
// float[] features1 = null;
|
||||
// try {
|
||||
// //第1张照片
|
||||
// SeetaImageData image1;
|
||||
// if (null != file) {
|
||||
// image1 = SeetafaceUtil.toSeetaImageData(imageAsBufferedImage(file));
|
||||
// } else {
|
||||
// image1 = SeetafaceUtil.toSeetaImageData(downloadImageAsBufferedImage(url));
|
||||
// }
|
||||
// //第一张照片人脸识别
|
||||
// SeetaRect[] detects1 = detector.Detect(image1);
|
||||
// if (null == detects1 || detects1.length == 0) {
|
||||
// throw new HandleException("未识别到人脸,请重试");
|
||||
// }
|
||||
// SeetaPointF[] pointFS1 = new SeetaPointF[faceRecognizer.GetExtractFeatureSize()];
|
||||
// int[] masks1 = new int[faceRecognizer.GetExtractFeatureSize()];
|
||||
// //第一张图片,第一个人脸关键点定位,有多个人脸的情况下,只取第一个人脸(这是测试,先这样写)
|
||||
// faceLandmarker.mark(image1, detects1[0], pointFS1, masks1);
|
||||
// //第一张图片,第一个人脸向量特征提取features1
|
||||
// features1 = new float[faceRecognizer.GetExtractFeatureSize()];
|
||||
// faceRecognizer.Extract(image1, pointFS1, features1);
|
||||
// } catch (Exception e) {
|
||||
// throw new HandleException(e.getMessage());
|
||||
// }
|
||||
// return features1;
|
||||
// }
|
||||
//
|
||||
// public static Boolean compareEigenvalue(float[] features1, float[] features2) {
|
||||
// try {
|
||||
// //人脸向量特征提取和对比器
|
||||
// // FaceRecognizer faceRecognizer = new FaceRecognizer(new SeetaModelSetting(SeetaConstant.face_recognizer, SeetaDevice.SEETA_DEVICE_AUTO));
|
||||
// if (features1 != null && features2 != null ) {
|
||||
// float calculateSimilarity = faceRecognizer.CalculateSimilarity(features1, features2);
|
||||
//
|
||||
// if (calculateSimilarity > 0.75) {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// log.error(e.getMessage());
|
||||
// return false;
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// public static BufferedImage imageAsBufferedImage(MultipartFile file) throws Exception {
|
||||
// // 获取输入流
|
||||
// InputStream inputStream = file.getInputStream();
|
||||
// // 使用ImageIO读取InputStream到BufferedImage
|
||||
// BufferedImage bufferedImage = ImageIO.read(inputStream);
|
||||
// // 关闭输入流
|
||||
// inputStream.close();
|
||||
// return bufferedImage;
|
||||
// }
|
||||
//
|
||||
// public static BufferedImage downloadImageAsBufferedImage(String imageUrl) throws Exception {
|
||||
// // 创建URL对象
|
||||
// URL url = new URL(imageUrl);
|
||||
// // 打开连接
|
||||
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
// connection.setRequestMethod("GET");
|
||||
// // 连接到资源
|
||||
// connection.connect();
|
||||
// // 获取输入流
|
||||
// InputStream inputStream = connection.getInputStream();
|
||||
// // 使用ImageIO读取InputStream到BufferedImage
|
||||
// BufferedImage bufferedImage = ImageIO.read(inputStream);
|
||||
// // 关闭输入流
|
||||
// inputStream.close();
|
||||
// // 断开连接
|
||||
// connection.disconnect();
|
||||
// return bufferedImage;
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user