From 7bcd2613bb116eff33d6e2358bd8c737bacc3cc1 Mon Sep 17 00:00:00 2001
From: pans <pans@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期三, 04 一月 2017 16:33:26 +0800
Subject: [PATCH] 

---
 RtspFace/demo/src/tools.h        |    5 
 RtspFace/demo/src/test.cpp       |   14 ++-
 RtspFace/demo/src/DBuntil.cpp    |   97 ++++++++++++++++++++++++
 RtspFace/demo/src/DBuntil.h      |   41 ++++++++++
 RtspFace/demo/src/Makefile       |   11 ++
 RtspFace/demo/src/db/DBuntil.cpp |    2 
 RtspFace/demo/src/tools.cpp      |   18 +++-
 7 files changed, 172 insertions(+), 16 deletions(-)

diff --git a/RtspFace/demo/src/DBuntil.cpp b/RtspFace/demo/src/DBuntil.cpp
new file mode 100644
index 0000000..b9c4d94
--- /dev/null
+++ b/RtspFace/demo/src/DBuntil.cpp
@@ -0,0 +1,97 @@
+#include "DBuntil.h"
+#include <mysql.h>
+#include <cstdio>
+#include <iostream>
+
+
+MYSQL myCont;
+MYSQL_RES *result;
+MYSQL_ROW sql_row;
+
+DBuntil::DBuntil(){}
+
+DBuntil::DBuntil(my_db mydb)
+{
+	if(mysql_init(&myCont)!=NULL)
+	{
+		std::cout<<"init succeed"<<std::endl;
+	}
+	else
+		std::cout<<"init failed"<<std::endl;
+
+	if(mysql_real_connect(&myCont, mydb.host, mydb.user, mydb.pswd, mydb.db, mydb.port, NULL, 0) != NULL)
+	{
+		std::cout<<"mysql_real_connect succeed"<<std::endl;
+	}
+	else
+		std::cout<<"mysql_real_connect failed"<<std::endl;
+}
+
+DBuntil::~DBuntil()
+{
+	if (result != NULL)
+		mysql_free_result(result);
+
+	mysql_close(&myCont);
+}
+
+person DBuntil::db_rearch(int f_id)
+{
+	person p= {0,""};
+	db_select(f_id,&p);
+	//瀵圭粨鏋勪綋璧嬪��
+	return p;
+}
+
+bool DBuntil::db_select(int f_id,person* per)
+{
+
+	sprintf( sql, "select a.p_id,b.`name`,b.img from face_person a,user_info b where a.face_id = %d AND a.p_id = b.pid " , f_id );
+
+	mysql_query(&myCont, "SET NAMES utf8"); //璁剧疆缂栫爜鏍煎紡
+	res = mysql_query(&myCont,sql);//鏌ヨ
+	if (!res)
+	{
+		result = mysql_store_result(&myCont);
+		if (result)
+		{
+			while (sql_row = mysql_fetch_row(result))
+			{
+				//鑾峰彇鍏蜂綋鐨勬暟鎹�
+				per->p_id = atoi( sql_row[0]);
+				per->name = sql_row[1];
+			}
+		}
+	}
+	else
+	{
+		std::cout<<"query sql failed!"<<std::endl;
+		return false;
+	}
+	return true;
+}
+
+bool DBuntil::db_add(int f_id,person *per)
+{
+	//
+	sprintf( sql, "INSERT INTO user_info(NAME, img) VALUES('%s', NULL)" , per->name );
+	res = mysql_query(&myCont,sql);
+	if(!res)
+	{
+		sprintf( sql, "INSERT INTO face_person(p_id, face_id) VALUES (LAST_INSERT_ID(), %d)" , f_id );
+		res = mysql_query(&myCont,sql);
+		if(!res)
+		{
+			return true;
+		}
+	}
+	return false;
+}
+
+bool DBuntil::db_register(int f_id,person *per)
+{
+	if(db_add(f_id,per)){
+		return true;
+	}
+	return false;
+}
\ No newline at end of file
diff --git a/RtspFace/demo/src/DBuntil.h b/RtspFace/demo/src/DBuntil.h
new file mode 100644
index 0000000..337b371
--- /dev/null
+++ b/RtspFace/demo/src/DBuntil.h
@@ -0,0 +1,41 @@
+#ifndef _DBUNTIL_H_
+#define _DBUNTIL_H_
+
+struct person {
+	int p_id;
+	char *name;
+	int f_id;
+	//图片
+};
+
+struct my_db {
+	char user[25];
+	char pswd[25];
+	char host[25];
+	char db[25];
+	unsigned int port;
+};
+
+class DBuntil
+{
+
+public:
+	DBuntil();
+	DBuntil(my_db mydb);
+	~DBuntil();
+	person db_rearch(int f_id);
+
+	bool db_register(int f_id,person *p);
+
+private:
+
+	bool db_add(int f_id,person *per);
+	int db_update();
+	bool db_select(int f_id,person *per);
+
+	char sql[1024];
+	int res;
+};
+
+
+#endif
\ No newline at end of file
diff --git a/RtspFace/demo/src/Makefile b/RtspFace/demo/src/Makefile
index c8ca8cc..d3036c1 100644
--- a/RtspFace/demo/src/Makefile
+++ b/RtspFace/demo/src/Makefile
@@ -1,18 +1,23 @@
 锘緾XX=g++
 CXXFLAGS:=-I../include
+CXXFLAGS+=/usr/include/mysql
 CXXFLAGS+=$(shell pkg-config --cflags opencv)
 
-LDFLAGS+=-L../libs/lib_dummy
+LDFLAGS+=-L../libs/lib_dummy /usr/lib64/mysql
 
-LIBS:=-lcvface_api -lpthread
+LIBS:=-lcvface_api -lpthread -lmysqlclient
 LIBS+=$(shell pkg-config --libs opencv)
 
-OBJ = faceDB.o faceAPI.o main.o 
+OBJ = faceAPI.o db.o tools.o main.o 
 
 demo: $(OBJ)
 	$(CXX) $(CXXFLAGS) $(LDFLAGS) -o demo $(OBJ) $(LIBS)
 main.o : test.cpp
 	$(CXX) $(CXXFLAGS) $(LDFLAGS) -c test.cpp -o main.o 
+tools.o : tools.h
+	$(CXX) $(CXXFLAGS) $(LDFLAGS) -c tools.cpp -o tools.o
+db.o : DBuntil.h
+	$(CXX) $(CXXFLAGS) -o db.o -c  DBuntil.cpp $(LDFLAGS) $(LIBS)
 faceAPI.o : faceAPI.h
 	$(CXX) $(CXXFLAGS) $(LDFLAGS) -c faceAPI.cpp -o faceAPI.o
 #faceDB.o : faceDB.cpp
diff --git a/RtspFace/demo/src/db/DBuntil.cpp b/RtspFace/demo/src/db/DBuntil.cpp
index 8f9f108..b9c4d94 100644
--- a/RtspFace/demo/src/db/DBuntil.cpp
+++ b/RtspFace/demo/src/db/DBuntil.cpp
@@ -43,7 +43,7 @@
 	return p;
 }
 
-int DBuntil::db_select(int f_id,person* per)
+bool DBuntil::db_select(int f_id,person* per)
 {
 
 	sprintf( sql, "select a.p_id,b.`name`,b.img from face_person a,user_info b where a.face_id = %d AND a.p_id = b.pid " , f_id );
diff --git a/RtspFace/demo/src/test.cpp b/RtspFace/demo/src/test.cpp
index f626a03..05eab0d 100644
--- a/RtspFace/demo/src/test.cpp
+++ b/RtspFace/demo/src/test.cpp
@@ -2,7 +2,7 @@
 #include <vector>
 #include <stdio.h>
 
-#include "faceAPI.h"
+#include "tools.h"
 
 using namespace std;
 
@@ -13,6 +13,10 @@
 	char *db_path = "./out.db";
 	char *image_path = argv[1];
 	char *image_list = "../test_image/imglist";
+	
+	
+	person p={0,"axsdcc"};
+	
 	cv::Mat bgr_image = cv::imread(image_path);
 	if(bgr_image.data != NULL) {
 		cout<<image_path<<endl;
@@ -20,10 +24,12 @@
 		cout<<"image is null"<<endl;
 		cout<<image_path<<endl;
 	}
+	
 	int idx = -11;
-	faceAPI face;
-	idx = face.do_reasch(bgr_image);
+	tools tool=tools();
+	idx=tool->register(bgr_image,p);
 	cout<<"idx="<<idx<<endl;
-
+	
+	
 	return 0;
 }
diff --git a/RtspFace/demo/src/tools.cpp b/RtspFace/demo/src/tools.cpp
index b64f807..42c1515 100644
--- a/RtspFace/demo/src/tools.cpp
+++ b/RtspFace/demo/src/tools.cpp
@@ -1,6 +1,10 @@
 #include "tools.h"
 
-tools::tools() {}
+tools::tools() {
+	my_db mydb={"root","Basic@2017","localhost","demo",3306};
+	
+	dbu = DBuntil(mydb);
+}
 
 tools::~tools() {}
 
@@ -9,7 +13,7 @@
 	idx = f_api->do_reasch(image);
 	if(idx<0)
 	{
-		p = f_db->db_rearch(int);
+		p = dbu->db_rearch(int);
 		if(p != NULL)
 		{
 			return 0;
@@ -34,13 +38,16 @@
 
 int tools::register(cv::Mat image,person *p)
 {
-	do_register(image,p);
+	if(do_register(image,p) == 0){
+		return 0;
+	}
+	return -1;
 }
 
 person tools::do_search(int idx)
 {
 	person p;
-	p = f_db->db_rearch(int);
+	p = dbu->db_rearch(int);
 	return p;
 }
 
@@ -49,7 +56,8 @@
 	idx = f_api->do_register(image);
 	if(idx<0)
 	{
-		idx = f_db->do_register(idx,p);
+		p->f_id = idx;
+		idx = dbu->do_register(idx,p);
 		if(idx)
 		{
 			return 0;
diff --git a/RtspFace/demo/src/tools.h b/RtspFace/demo/src/tools.h
index a4bc5db..e69692f 100644
--- a/RtspFace/demo/src/tools.h
+++ b/RtspFace/demo/src/tools.h
@@ -4,9 +4,6 @@
 #include "DBuntil.h"
 #include "faceAPI.h"
 
-class faceAPI f_api;
-class DButil f_db;
-
 class tools
 {
 public:
@@ -19,6 +16,8 @@
 	int do_register(cv::Mat image,person *p);
 	person do_search(int idx);
 	int idx;
+	DBuntil dbu;
+	faceAPI f_api;
 }
 
 #endif
\ No newline at end of file

--
Gitblit v1.8.0