4.6每日总结
建立人脸数据库添加
public void addFace(String name, AFR_FSDKFace face) {
try {
//check if already registered.
boolean add = true;
for (FaceRegist frface : mRegister) {
if (frface.mName.equals(name)) {
frface.mFaceList.add(face);
add = false;
break;
}
}
if (add) { // not registered.
FaceRegist frface = new FaceRegist(name);
frface.mFaceList.add(face);
mRegister.add(frface);
}
if (!new File(mDBPath + "/face.txt").exists()) {
if (!saveInfo()) {
Log.e(TAG, "save fail!");
}
}
//save name
FileOutputStream fs = new FileOutputStream(mDBPath + "/face.txt", true);
ExtOutputStream bos = new ExtOutputStream(fs);
bos.writeString(name);
bos.close();
fs.close();
//save feature
fs = new FileOutputStream(mDBPath + "/" + name + ".data", true);
bos = new ExtOutputStream(fs);
bos.writeBytes(face.getFeatureData());
bos.close();
fs.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
使用loadFaces从文件中读取人脸
public boolean loadFaces(){
if (loadInfo()) {
try {
for (FaceRegist face : mRegister) {
Log.d(TAG, "load name:" + face.mName + "'s face feature data.");
FileInputStream fs = new FileInputStream(mDBPath + "/" + face.mName + ".data");
ExtInputStream bos = new ExtInputStream(fs);
AFR_FSDKFace afr = null;
do {
if (afr != null) {
if (mUpgrade) {
//upgrade data.
}
face.mFaceList.add(afr);
}
afr = new AFR_FSDKFace();
} while (bos.readBytes(afr.getFeatureData()));
bos.close();
fs.close();
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
if (!saveInfo()) {
Log.e(TAG, "save fail!");
}
}
return false;
}
绘出人脸框
在List<AFD_FSDKFace>中保存了检测到的人脸的位置信息和深度信息。
我们可以将检测到的人脸位置信息在图片上用一个矩形框绘制出来表示检测到的人脸信息。
Canvas canvas = mSurfaceHolder.lockCanvas();
if (canvas != null) {
Paint mPaint = new Paint();
boolean fit_horizontal = canvas.getWidth() / (float)src.width() < canvas.getHeight() / (float)src.height() ? true : false;
float scale = 1.0f;
if (fit_horizontal) {
scale = canvas.getWidth() / (float)src.width();
dst.left = 0;
dst.top = (canvas.getHeight() - (int)(src.height() * scale)) / 2;
dst.right = dst.left + canvas.getWidth();
dst.bottom = dst.top + (int)(src.height() * scale);
} else {
scale = canvas.getHeight() / (float)src.height();
dst.left = (canvas.getWidth() - (int)(src.width() * scale)) / 2;
dst.top = 0;
dst.right = dst.left + (int)(src.width() * scale);
dst.bottom = dst.top + canvas.getHeight();
}
canvas.drawBitmap(mBitmap, src, dst, mPaint);
canvas.save();
canvas.scale((float) dst.width() / (float) src.width(), (float) dst.height() / (float) src.height());
canvas.translate(dst.left / scale, dst.top / scale);
for (AFD_FSDKFace face : result) {
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(10.0f);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawRect(face.getRect(), mPaint);
}
canvas.restore();
mSurfaceHolder.unlockCanvasAndPost(canvas);
break;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用