1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
| public class FaceUtil {
private static final String TAG = "FaceUtil";
private FaceUtil() { }
/** * 特征保存 * * @param image Mat * @param rect 人脸信息 * @return 保存是否成功 */ public static boolean saveImage(Mat image, Rect rect) { // 原图置灰 Mat grayMat = new Mat(); Imgproc.cvtColor(image, grayMat, Imgproc.COLOR_BGR2GRAY); //Imgproc.cvtColor(image, grayMat, Imgproc.COLORMAP_JET); // 把检测到的人脸重新定义大小后保存成文件 Mat sub = grayMat.submat(rect); Mat mat = new Mat(); Size size = new Size(100, 100); Imgproc.resize(sub, mat, size); return Imgcodecs.imwrite(getRecFileName(), mat); }
/** * 删除特征 * * @param context Context * @param fileName 特征文件 * @return 是否删除成功 */ public static boolean deleteImage(Context context, String fileName) { // 文件名不能为空 if (TextUtils.isEmpty(fileName)) { return false; } // 文件路径不能为空 String path = getRecFileNameList()[0]; if (path != null) { File file = new File(path); return file.exists() && file.delete(); } else { return false; } }
/** * 提取特征 * * @param * @param fileName 文件名 * @return 特征图片 */ public static Bitmap getImage(String fileName) { //String filePath = getRecFileNameList()[0]; if (TextUtils.isEmpty(fileName)) { return null; } else { return BitmapFactory.decodeFile(fileName); } }
public static double recon(Context context) { String[] dataPath = getDataFileNameList(); String recPath = getRecFileNameList()[0]; double diff = 0; for (int i = 0; i < dataPath.length; i++) { diff = compare(dataPath[i], recPath); if (diff >= 90) { break; } } return diff; }
/** * 特征对比 * * @param * @param fileName1 人脸特征 * @param fileName2 人脸特征 * @return 相似度 */ public static double compare(String fileName1, String fileName2) { try { IplImage image1 = cvLoadImage(fileName1, opencv_imgcodecs.IMREAD_GRAYSCALE); IplImage image2 = cvLoadImage(fileName2, opencv_imgcodecs.IMREAD_GRAYSCALE); if (null == image1 || null == image2) { return -1; }
int l_bins = 256; int hist_size[] = {l_bins}; float v_ranges[] = {0, 255}; float ranges[][] = {v_ranges};
IplImage imageArr1[] = {image1}; IplImage imageArr2[] = {image2}; CvHistogram Histogram1 = CvHistogram.create(1, hist_size, CV_HIST_ARRAY, ranges, 1); CvHistogram Histogram2 = CvHistogram.create(1, hist_size, CV_HIST_ARRAY, ranges, 1); cvCalcHist(imageArr1, Histogram1, 0, null); cvCalcHist(imageArr2, Histogram2, 0, null); cvNormalizeHist(Histogram1, 100.0); cvNormalizeHist(Histogram2, 100.0); // 参考:http://blog.csdn.net/nicebooks/article/details/8175002 double c1 = cvCompareHist(Histogram1, Histogram2, CV_COMP_CORREL) * 100; double c2 = cvCompareHist(Histogram1, Histogram2, CV_COMP_INTERSECT); // Log.i(TAG, "compare: ----------------------------"); // Log.i(TAG, "compare: c1 = " + c1); // Log.i(TAG, "compare: c2 = " + c2); // Log.i(TAG, "compare: 平均值 = " + ((c1 + c2) / 2)); // Log.i(TAG, "compare: ----------------------------"); return (c1 + c2) / 2; } catch (Exception e) { e.printStackTrace(); return -1; } }
/** * @return 路径 */ public static File getDataSourcePath() { File file = new File(Environment.getExternalStorageDirectory(), "/FaceDetect/sourcePic/"); if (!file.exists()) { file.mkdirs(); } return file; //return new File(Environment.getExternalStorageDirectory(),"/FaceDetect/sourcePic/"); }
public static File getRecPath() { File file = new File(Environment.getExternalStorageDirectory(), "FaceDetect"); if (!file.exists()) { file.mkdirs(); } return file; // return new File(Environment.getExternalStorageDirectory(),"FaceDetect"); }
private static String getRecFileName() { String name = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss").format(new Date()) + ".jpg"; return new File(getRecPath().getAbsolutePath(), name).getAbsolutePath(); //return getRecPath().getAbsolutePath() + new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss").format(new Date()) + ".jpg"; }
/** * @return * @description:获取识别库下的所有文件 */ public static String[] getDataFileNameList() { File pathName = getDataSourcePath(); File[] files = pathName.listFiles(); String[] fileNames = null; if (files != null) { fileNames = fileFilterEnd(files, "jpg"); Arrays.sort(fileNames, Collections.reverseOrder()); } return fileNames; }
/** * @return * @description:获取最近识别出的文件 */ public static String getRecFileAbsolutePath() { String fileName = FaceUtilNew.getRecFileNameList()[0]; return new File(getRecPath().getAbsolutePath(), fileName).getAbsolutePath(); }
public static String[] getRecFileNameList() { File pathName = getRecPath(); File[] files = pathName.listFiles(); String[] fileNames = null; if (files != null) { fileNames = fileFilterEnd(files, "jpg"); Arrays.sort(fileNames, Collections.reverseOrder()); } return fileNames; } //删除所有临时文件 public static void deleteRecFiles(){ String [] recStringFiles=getRecFileNameList(); for (String str:recStringFiles) { File file=new File(getRecPath().getAbsolutePath(),str); file.delete(); } }
private static String[] fileFilterEnd(File[] f, String end) { int count = 0; for (int i = 0; i < f.length; i++) { if (f[i].getName().endsWith(end)) count++; } String[] s1 = new String[count]; count = 0; for (int i = 0; i < f.length; i++) { if (f[i].getName().endsWith(end)) { s1[count] = f[i].getName(); count++; } } return s1; } }
|