143 lines
4.5 KiB
C++
143 lines
4.5 KiB
C++
#pragma once
|
||
#include <vector>
|
||
#include <map>
|
||
#include <string>
|
||
#include <mutex>
|
||
#include <sqlite3/sqlite3.h>
|
||
|
||
#define MOBILE_FACE_FEATURE_FLOAT_LEN 512
|
||
#define MOBILE_FACE_FEATURE_BYTE_LEN (MOBILE_FACE_FEATURE_FLOAT_LEN*sizeof(float))
|
||
|
||
#define MAX_FEATURES_FOR_ONE_NAME 1 //一个人限制录入1条数据
|
||
#define MAX_FEATURE_FOR_GROUP 2000 //每组限制5000条数据
|
||
#define MAX_GROUP_NUM 500 //最多只能添加500组
|
||
|
||
#define FS_DB_QUERY_SIZE_BY_NAME "select count(*) from table_features where Name=?;"
|
||
#define FS_DB_QUERY_SIZE_OF_NAMES "select count(distinct Name) from table_features;"
|
||
#define FS_DB_QUERY_SIZE_INALL "select count(*) from table_features;"
|
||
|
||
enum DB_ERR_CODE
|
||
{
|
||
DB_ERR_BIND_SQL_DATA = -100,//绑定sql数据错误
|
||
DB_ERR_SQL, //sql错误
|
||
DB_ERR_SQL_EXE, //执行SQL错误
|
||
DB_ERR_FEATURE_LEN, //人脸特征长度错误
|
||
DB_ERR_NAME_FEATURE_EXIST, //该名字已经注册特征
|
||
DB_ERR_GROUP_FEATURE_FULL, //分组内注册人脸数达到上限
|
||
DB_ERR_GROUP_FULL, //创建的分组达到上限
|
||
DB_ERR_ADD_FACE_FEATURE, //录入人脸特征失败
|
||
DB_ERR_NON_GROUP, //组名不存在
|
||
DB_ERR_GROUP_REPEAT, //分组名字重复
|
||
DB_ERR_ADD_GROUP, //添加分组数据失败
|
||
DB_ERR_NO_INIT, //人脸数据库未初始化
|
||
|
||
DB_ERR_NO_DATA = -1, //未查询到数据
|
||
DB_ERR_OK = 0 //无错误
|
||
|
||
};
|
||
|
||
struct DB_GROUP_OBJ
|
||
{
|
||
int group_id;
|
||
std::string group_name;
|
||
std::string create_time;
|
||
std::string remarks; //分组备注信息
|
||
};
|
||
struct DB_FACE_OBJ
|
||
{
|
||
DB_GROUP_OBJ group;
|
||
|
||
std::string vip_id;
|
||
std::string name;
|
||
std::string enter_time;
|
||
unsigned char byteFeature[MOBILE_FACE_FEATURE_BYTE_LEN];
|
||
};
|
||
|
||
struct SEARCH_FACE_RESULT {
|
||
DB_FACE_OBJ face_obj;
|
||
float score;
|
||
};
|
||
|
||
|
||
class FaceDbMgr
|
||
{
|
||
class AutoDeleteStmt
|
||
{
|
||
public:
|
||
explicit AutoDeleteStmt(sqlite3_stmt * pstmt)
|
||
{
|
||
m_pStmt = pstmt;
|
||
}
|
||
~AutoDeleteStmt()
|
||
{
|
||
if (m_pStmt)
|
||
{
|
||
sqlite3_finalize(m_pStmt);
|
||
}
|
||
}
|
||
|
||
private:
|
||
sqlite3_stmt * m_pStmt;
|
||
};
|
||
|
||
private:
|
||
explicit FaceDbMgr(char * pcDbName = "DeepCamFaceService.db");
|
||
~FaceDbMgr();
|
||
|
||
public:
|
||
static FaceDbMgr* GetInstance();
|
||
/*********************** 业务接口 *****************************/
|
||
bool IsDbInit();//获取数据库是否初始化成功的状态
|
||
|
||
std::string GetErrInfo(DB_ERR_CODE);
|
||
DB_ERR_CODE EnterFace(const std::vector<float>& feature, std::string face_name, std::string vid, std::string group_name, std::vector<unsigned char> & imgData);
|
||
DB_ERR_CODE FaceSearch(const std::vector<float> & feature, const std::string group_name, DB_FACE_OBJ& face_obj, std::vector<uint8_t>& img_data, float & fSimilarity, float fThreshold);
|
||
DB_ERR_CODE DeleteFaceByName(std::string faceName, std::string group_name);
|
||
DB_ERR_CODE DeleteAllFace(std::string group_name);
|
||
DB_ERR_CODE AddGroup(std::string group_name, std::string remarks = "");
|
||
DB_ERR_CODE DeleteGroup(std::string group_name);
|
||
//查询所有的组数据
|
||
DB_ERR_CODE QueryGroupDatas(std::vector<DB_GROUP_OBJ>& objs);
|
||
public:
|
||
/*********************** 基础API *****************************/
|
||
float FaceDbMgr::FeatureCompare(const float* feature1, const float* feature2);
|
||
|
||
int FeatureDBMgrExec(const char * strSql);//执行一些不需要返回表数据的语句、只需要返回是否执行成功的结果。
|
||
|
||
//返回查询表数据条数,大于0才表示有数据
|
||
int QueryTableSize(const char * strSql);
|
||
//返回查询表数据条数,大于0才表示有数据
|
||
int QueryTableSizeByName(const char * strSql, const char * strPersonName);
|
||
//查询所有的人脸特征数量
|
||
int QueryAllFeatureSize();
|
||
//查询分组内所有的人脸特征数量
|
||
int QueryGroupAllFeatureSize(int group_id);
|
||
//通过名字查询组内注册人脸特征数量
|
||
int QueryNameFeatureInGroupSize(std::string groupName, std::string faceName);
|
||
//查询所有分组数量
|
||
int QueryAllGroupSize();
|
||
//通过组名字查询组ID
|
||
int QueryGroupIdByName(std::string groupName);
|
||
int QueryGroupByName(std::string groupName, std::vector<DB_GROUP_OBJ>& objs);
|
||
int QueryGroupByID(int group_id, std::vector<DB_GROUP_OBJ>& objs);
|
||
|
||
//查询组内所有的特征数据
|
||
int QueryFeatrueByGroup(DB_GROUP_OBJ& group, std::vector<DB_FACE_OBJ>& face_db);
|
||
//通过姓名查询人脸特征库
|
||
int QueryFeatureByName(std::string face_name, std::string group_name, std::vector<DB_FACE_OBJ>& face_db);
|
||
//通过姓名查询人脸图片
|
||
int QueryImageByName(std::string face_name, int group_id, std::vector<uint8_t>& img_data);
|
||
|
||
private:
|
||
sqlite3 * m_db;
|
||
bool m_bDbInit;
|
||
std::mutex m_db_lock;
|
||
std::map<DB_ERR_CODE, std::string> m_err_info; //错误代码映射表
|
||
//static std::map<std::string, int> m_groups; //组ID <=> 组名 映射
|
||
static std::vector<DB_GROUP_OBJ> m_groups;
|
||
static std::map<int, std::vector<DB_FACE_OBJ>> m_GroupDb; //人脸数据集 组ID <=> 单组数据集
|
||
|
||
static FaceDbMgr* m_instance;
|
||
};
|
||
|