Commit a7faf8dc by guanghui.cui

演示程序,新增socket转发

parent 440cb3a8
No preview for this file type
[SYS] [SYS]
#serach URL #serach URL
serachURL=http://139.224.206.167:8788/facesearch/search4File serachURL=http://139.224.206.167:8788/facesearch/search4File
\ No newline at end of file #精确度,该值越小,代表需要识别的面部精确度越高
accuracy=0.10
\ No newline at end of file
...@@ -58,21 +58,24 @@ ...@@ -58,21 +58,24 @@
// FaceTrackingVid.cpp : Defines the entry point for the console application for tracking faces in videos. // FaceTrackingVid.cpp : Defines the entry point for the console application for tracking faces in videos.
// Libraries for landmark detection (includes CLNF and CLM modules) // Libraries for landmark detection (includes CLNF and CLM modules)
#include "LandmarkCoreIncludes.h"
#include "GazeEstimation.h"
#include "jsonAnalysis.h"
#include "../curl/curl.h"
#include "utility/utilCommonAPI.h"
#include "utility/iniOperation.h"
#ifdef WIN32 #ifdef WIN32
#include <WinSock2.h>
#include <process.h> #include <process.h>
#include <Windows.h> #include <Windows.h>
#include "IShowDetail.h" #include "IShowDetail.h"
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "ShowDetail.lib") #pragma comment(lib, "ShowDetail.lib")
#endif #endif
#include "LandmarkCoreIncludes.h"
#include "GazeEstimation.h"
#include "jsonAnalysis.h"
#include "../curl/curl.h"
#include "utility/utilCommonAPI.h"
#include "utility/iniOperation.h"
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
...@@ -108,6 +111,7 @@ printErrorAndAbort( std::string( "Fatal error: " ) + stream ) ...@@ -108,6 +111,7 @@ printErrorAndAbort( std::string( "Fatal error: " ) + stream )
using namespace std; using namespace std;
string postResponseStr; string postResponseStr;
double accuracy = 0.00;
vector<string> get_arguments(int argc, char **argv) vector<string> get_arguments(int argc, char **argv)
{ {
...@@ -148,7 +152,7 @@ bool visualise_tracking(cv::Mat& captured_image, cv::Mat_<float>& depth_image, c ...@@ -148,7 +152,7 @@ bool visualise_tracking(cv::Mat& captured_image, cv::Mat_<float>& depth_image, c
vis_certainty = -1; vis_certainty = -1;
vis_certainty = (vis_certainty + 1) / (visualisation_boundary + 1); vis_certainty = (vis_certainty + 1) / (visualisation_boundary + 1);
if (vis_certainty < 0.06 && takephoto) { if (vis_certainty < accuracy && takephoto) {
INFO_STREAM("+++++++++++++++: " << vis_certainty); INFO_STREAM("+++++++++++++++: " << vis_certainty);
flag = true; flag = true;
} }
...@@ -303,20 +307,203 @@ unsigned __stdcall ThreadFunction(void *pThread) ...@@ -303,20 +307,203 @@ unsigned __stdcall ThreadFunction(void *pThread)
} }
#endif #endif
#define BUFFER_SIZE 1024 //缓冲区大小
//Socket server
DWORD WINAPI FunSocketServer(LPVOID lpParamter)
{
WORD sockVersion = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(sockVersion, &wsaData) != 0)
{
return 0;
}
//创建套接字
SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (slisten == INVALID_SOCKET)
{
printf("socket error !");
return 0;
}
//绑定IP和端口
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8888);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
bind(slisten, (SOCKADDR*)&sin, sizeof(sin));
//开始监听
if (listen(slisten, 5) == SOCKET_ERROR)
{
DWORD k = GetLastError();
printf("listen error !");
return 0;
}
//循环接收数据
SOCKET sClient;
sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);
char revData[255];
while (true)
{
printf("等待连接...\n");
sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
if (sClient == INVALID_SOCKET)
{
printf("accept error !");
continue;
}
//printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
//接收数据
int ret = recv(sClient, revData, 255, 0);
if (ret > 0)
{
revData[ret] = 0x00;
printf(revData);
}
//发送数据
const char * sendData = "你好,TCP客户端!\n";
send(sClient, sendData, strlen(sendData), 0);
closesocket(sClient);
}
closesocket(slisten);
WSACleanup();
//SOCKET sockTakewayServer;
//SOCKET sockTakewayClient;
//SOCKADDR_IN addrServer;
//SOCKADDR_IN addrClient;
//sockTakewayServer = socket(AF_INET, SOCK_STREAM, 0);
//addrServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//INADDR_ANY表示任何IP
//addrServer.sin_family = AF_INET;
//addrServer.sin_port = htons(24000);//绑定端口
//bind(sockTakewayServer, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
////Listen监听端
//listen(sockTakewayServer, 5);//5为等待连接数目
//int len = sizeof(SOCKADDR);
//char* takeWayRecvBuf = new char[BUFFER_SIZE * 10]; //外卖数据接收区
//int nErrCode = 0;
//while (true)
//{
// sockTakewayClient = accept(sockTakewayServer, (SOCKADDR*)&addrClient, &len);
// if (sockTakewayClient == SOCKET_ERROR)
// {
// continue;
// }
// //接收并打印客户端数据
// int rlt = recv(sockTakewayClient, takeWayRecvBuf, BUFFER_SIZE * 10, 0);
// if (rlt > 0)
// {
// takeWayRecvBuf[rlt] = '\0';
// const char * sendData = "你好,TCP客户端!\n";
// send(sockTakewayClient, sendData, strlen(sendData), 0);
// }
// closesocket(sockTakewayClient);
//}
//delete[] takeWayRecvBuf;
//closesocket(sockTakewayServer);
return 0;
}
bool bTakePic = false; //是否开始拍照
#include <dlib/sockets.h>
#include <dlib/server.h>
//#pragma comment(lib, "newlib/dlib.lib")
HANDLE g_hSemaphore = NULL;
string socketBackJson;
class serv : public dlib::server
{
void on_connect(
dlib::connection& con
)
{
char tmpJson[1000];
int n = con.read(tmpJson, 1000);
tmpJson[n] = '\0';
cout << "recived data:" << tmpJson << endl;
int fm_cmd = SocketRequestJson(tmpJson);
if (2002 == fm_cmd) {
bTakePic = true;
//信号量
g_hSemaphore = CreateSemaphore(NULL //信号量的安全特性
, 1 //设置信号量的初始计数。可设置零到最大值之间的一个值
, 1 //设置信号量的最大计数
, NULL //指定信号量对象的名称
);
if (NULL == g_hSemaphore)
{
cout << "create hSemaphore failed! error_code:" << GetLastError() << endl;
return;
}
WaitForSingleObject(g_hSemaphore, INFINITE);
//-----------
con.write(socketBackJson.data(), socketBackJson.length());
}
con.shutdown();
}
};
serv our_server;
void dlibServer()
{
try
{
// set up the server object we have made
our_server.set_listening_port(24777);
// Tell the server to begin accepting connections.
our_server.set_max_connections(10000);
our_server.start_async();
cout << "Press enter to end this program" << endl;
//cin.get();
}
catch (exception& e)
{
cout << e.what() << endl;
}
}
int main (int argc, char **argv) int main (int argc, char **argv)
{ {
dlibServer();
std::string strFilePath = GetProcDir(); std::string strFilePath = GetProcDir();
std::string strPathImg = strFilePath + "tmpimg.jpg"; std::string strPathImg = strFilePath + "tmpimg.jpg";
std::string strPathIni = strFilePath + "config.ini"; std::string strPathIni = strFilePath + "config.ini";
std::string strUrl = ZIni::readString("SYS", "serachURL", "", strPathIni.c_str()); std::string strUrl = ZIni::readString("SYS", "serachURL", "", strPathIni.c_str());
accuracy = ZIni::readDouble("SYS", "accuracy", 0.06, strPathIni.c_str());
////初始化WSA
//WORD sockVersion = MAKEWORD(2, 2);
//WSADATA wsaData;
//if (WSAStartup(sockVersion, &wsaData) != 0)
//{
// return 0;
//}
//启动外卖监控线程
//HANDLE hSocket = CreateThread(NULL, 0, FunSocketServer, NULL, 0, NULL);
//CloseHandle(hSocket);
vector<string> arguments = get_arguments(argc, argv); vector<string> arguments = get_arguments(argc, argv);
// Some initial parameters that can be overriden from command line // Some initial parameters that can be overriden from command line
vector<string> files, depth_directories, output_video_files, out_dummy; vector<string> files, depth_directories, output_video_files, out_dummy;
bool bTakePic = false; //是否开始拍照
// By default try webcam 0 // By default try webcam 0
int device = 0; int device = 0;
...@@ -538,6 +725,14 @@ int main (int argc, char **argv) ...@@ -538,6 +725,14 @@ int main (int argc, char **argv)
} }
sServerResponse response = GetServerResultData(postResponseStr.data()); sServerResponse response = GetServerResultData(postResponseStr.data());
//-------------socket返回给pos----------------
if (response.vecUser.size() > 0) {
socketBackJson = GetSocketResultJson(100, "success", response.vecUser[0].memberTel.data());
}
ReleaseSemaphore(g_hSemaphore, 1, NULL);
//---------------------------------------------
// global release // global release
curl_global_cleanup(); curl_global_cleanup();
bTakePic = false; bTakePic = false;
...@@ -596,7 +791,7 @@ int main (int argc, char **argv) ...@@ -596,7 +791,7 @@ int main (int argc, char **argv)
{ {
return(0); return(0);
} }
else if (character_press == ' ') //空格键按下,开始拍照 else if (character_press == 's')
{ {
bTakePic = true; bTakePic = true;
} }
......
...@@ -116,6 +116,7 @@ ...@@ -116,6 +116,7 @@
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
......
...@@ -104,4 +104,46 @@ sServerResponse GetServerResultData(const char* json) ...@@ -104,4 +104,46 @@ sServerResponse GetServerResultData(const char* json)
} }
return res; return res;
}
int SocketRequestJson(const char* json)
{
int result = 0;
rapidjson::Document document; // 定义一个Document对象
document.Parse(json); // 解析,Parse()无返回值,也不会抛异常
if (document.HasParseError()) // 通过HasParseError()来判断解析是否成功
{
// 可通过GetParseError()取得出错代码,
// 注意GetParseError()返回的是一个rapidjson::ParseErrorCode类型的枚举值
// 使用函数rapidjson::GetParseError_En()得到错误码的字符串说明,这里的En为English简写
// 函数GetErrorOffset()返回出错发生的位置
//LOG_ERROR("GetTakeawayPrintInfo JSON parse error: (%d:%d)", document.GetParseError(), document.GetErrorOffset())
//return false;
}
else
{
rapidjson::Value& fm_cmd = document["fm_cmd"];
result = fm_cmd.GetInt();
}
return result;
}
std::string GetSocketResultJson(const int statuscode, const char* msg, const char* phone)
{
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.StartObject();
writer.Key("statusCode");
writer.Int(statuscode);
writer.Key("msg");
writer.String(msg);
writer.Key("member_id");
writer.String(phone);
writer.EndObject();
return buffer.GetString();
} }
\ No newline at end of file
...@@ -20,4 +20,7 @@ std::string GetIdentityUserJson(const char* photo); ...@@ -20,4 +20,7 @@ std::string GetIdentityUserJson(const char* photo);
sServerResponse GetServerResultData(const char* json); sServerResponse GetServerResultData(const char* json);
int SocketRequestJson(const char* json);
std::string GetSocketResultJson(const int statuscode, const char* msg, const char* phone);
#endif #endif
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Copyright (C) 2016, Carnegie Mellon University and University of Cambridge, // Copyright (C) 2016, Carnegie Mellon University and University of Cambridge,
// all rights reserved. // all rights reserved.
// //
// THIS SOFTWARE IS PROVIDED “AS ISFOR ACADEMIC USE ONLY AND ANY EXPRESS // THIS SOFTWARE IS PROVIDED “AS IS?FOR ACADEMIC USE ONLY AND ANY EXPRESS
// OR IMPLIED WARRANTIES WARRANTIES, INCLUDING, BUT NOT LIMITED TO, // OR IMPLIED WARRANTIES WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
// POSSIBILITY OF SUCH DAMAGE. // POSSIBILITY OF SUCH DAMAGE.
// //
// Notwithstanding the license granted herein, Licensee acknowledges that certain components // Notwithstanding the license granted herein, Licensee acknowledges that certain components
// of the Software may be covered by so-called “open sourcesoftware licenses (“Open Source // of the Software may be covered by so-called “open source?software licenses (“Open Source
// Components”), which means any software licenses approved as open source licenses by the // Components?, which means any software licenses approved as open source licenses by the
// Open Source Initiative or any substantially similar licenses, including without limitation any // Open Source Initiative or any substantially similar licenses, including without limitation any
// license that, as a condition of distribution of the software licensed under such license, // license that, as a condition of distribution of the software licensed under such license,
// requires that the distributor make the software available in source code format. Licensor shall // requires that the distributor make the software available in source code format. Licensor shall
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Copyright (C) 2016, Carnegie Mellon University and University of Cambridge, // Copyright (C) 2016, Carnegie Mellon University and University of Cambridge,
// all rights reserved. // all rights reserved.
// //
// THIS SOFTWARE IS PROVIDED “AS ISFOR ACADEMIC USE ONLY AND ANY EXPRESS // THIS SOFTWARE IS PROVIDED “AS IS?FOR ACADEMIC USE ONLY AND ANY EXPRESS
// OR IMPLIED WARRANTIES WARRANTIES, INCLUDING, BUT NOT LIMITED TO, // OR IMPLIED WARRANTIES WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
// POSSIBILITY OF SUCH DAMAGE. // POSSIBILITY OF SUCH DAMAGE.
// //
// Notwithstanding the license granted herein, Licensee acknowledges that certain components // Notwithstanding the license granted herein, Licensee acknowledges that certain components
// of the Software may be covered by so-called “open sourcesoftware licenses (“Open Source // of the Software may be covered by so-called “open source?software licenses (“Open Source
// Components”), which means any software licenses approved as open source licenses by the // Components?, which means any software licenses approved as open source licenses by the
// Open Source Initiative or any substantially similar licenses, including without limitation any // Open Source Initiative or any substantially similar licenses, including without limitation any
// license that, as a condition of distribution of the software licensed under such license, // license that, as a condition of distribution of the software licensed under such license,
// requires that the distributor make the software available in source code format. Licensor shall // requires that the distributor make the software available in source code format. Licensor shall
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment