public static String exportResource(Context context, int resourceId, String dirname) { String fullname = context.getResources().getString(resourceId); String resName = fullname.substring(fullname.lastIndexOf("/") + 1); try { InputStream is = context.getResources().openRawResource(resourceId); File resDir = context.getDir(dirname, Context.MODE_PRIVATE); File resFile = new File(resDir, resName); FileOutputStream os = new FileOutputStream(resFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } is.close(); os.close(); return resFile.getAbsolutePath(); } catch (IOException e) { e.printStackTrace(); throw new CvException("Failed to export resource " + resName + ". Exception thrown: " + e); } }
/** * * @Title: reconstructFace * @Description: 从输入的预处理图像在人脸模型中重构人脸 * @param model 包含预处理的人脸模型 * @param preprocessedFace 输入的预处理过的图像 * @return * Mat * @throws */ public static Mat reconstructFace(BasicFaceRecognizer model, Mat preprocessedFace){ try { // 获取每个人脸的特征值 Mat eigenvectors = model.getEigenVectors(); // 获取平均人脸 Mat averageFaceRow = model.getMean(); int faceHeight = preprocessedFace.rows(); // subspaceProject将人脸图像投影到特征空间 Mat projection = subspaceProject(eigenvectors, averageFaceRow, preprocessedFace.reshape(1, 1)); // subspaceReconstruct从特征空间重构图像 Mat reconstructionRow = subspaceReconstruct(eigenvectors, averageFaceRow, projection); Mat reconstructionMat = reconstructionRow.reshape(1, faceHeight); Mat reconstructedFace = new Mat(reconstructionMat.size(), CvType.CV_8U); reconstructionMat.convertTo(reconstructedFace, CvType.CV_8U, 1, 0); return reconstructedFace; } catch(CvException e) { e.printStackTrace(); } return null; }