#define _WIN32_DCOM #include <SDKDDKVer.h> #include <direct.h> #include <thr/threads.h> #include <stdlib.h> #include <assert.h> #include <stdio.h> #include <tchar.h> #include <io.h> #include <time.h> #include <sys/types.h> #include <sys/stat.h> #include "....liblibciicii11includelibcii.h" #include "algo.h" extern TCHAR tcLogFile[56]; // #ifdef UNICODE #define __FILET__ __FILEW__ #define __FUNCTIONT__ __FUNCTIONW__ #else #define __FILET__ __FILE__ #define __FUNCTIONT__ __FUNCTION__ #endif #define TRACE_OUT(out ,fmt,...) do{TCHAR tcDate[24],tcTime[24];_tstrdate_s(tcDate),_tstrtime_s(tcTime); _ftprintf_s(out, _T(" %s %s - FILE:%s - FUN:%s - LINE:%d MESSAGE: ") fmt _T(" ") ,tcDate,tcTime,__FILET__,__FUNCTIONT__,__LINE__, ##__VA_ARGS__); } while (0) // #define TRACE_LOG(fmt,...) do{ FILE* stream ; TRACE_OUT(stderr, fmt , ##__VA_ARGS__); if( 0 != _tfopen_s(&stream,tcLogFile,_T("a"))) break; TRACE_OUT(stream, fmt , ##__VA_ARGS__); fclose(stream); } while(0) #import <IsVision_9007ISCFaceRecognize.dll> no_namespace #import <IsVision_9007ISCFaceDetect.dll> no_namespace #import <IsVision_9007ISCFaceRectify.dll> no_namespace #import <IsVision_9007ISCFaceTools.dll> no_namespace #import <IsVision_9007ISCFaceTracking.dll> no_namespace #import <IsVision_9007ISCImageProc.dll> no_namespace #import <IsVision_9007ISCImageQuality.dll> no_namespace #import <IsVision_9007ISXImage.dll> no_namespace #import <IsVision_9007ISCFaceMedia.dll> no_namespace #ifdef __cplusplus typedef struct Algo_T * LpAlgo_T; typedef struct Args_T * LpArgs_T; typedef struct Pipe_T * LpPipe_T; typedef struct Item_T * LpItem_T; #define T LpAlgo_T #define R LpArgs_T #define P LpPipe_T #define I LpItem_T #else #define T Arena_T #define R Args_T #define P Pipe_T typedef struct Algo_T *Algo_T; typedef struct Args_T *Args_T; typedef struct Pipe_T *Pipe_T; #endif #define _CT_FileName_ 0x01 #define _CT_FileData_ 0x02 #define _CT_ImageRgb_ 0x03 #define _IRecognize 0x0001 #define _ITransImage 0x0002 #define _IFaceDetect 0x0004 #define _IFaceRectify 0x0008 #define _IFaceTool 0x0010 #define _IFaceTrack 0x0020 #define _IImageProc 0x0040 #define _IImageQuality 0x0080 #define _IVisionImage 0x0100 #define _IPosition 0x0200 #define _IComImageAll 0x01FF #ifndef _THR_THREADS_H #define mtx_init(mtx, typ) InitializeSRWLock(mtx) #define mtx_destroy(mtx) __noop #define mtx_lock(mtx) AcquireSRWLockExclusive (mtx) #define mtx_unlock(mtx) ReleaseSRWLockExclusive (mtx); #define cnd_init(cnd) InitializeConditionVariable(cnd); #define cnd_destroy(cnd) __noop #define cnd_signal(cnd) WakeConditionVariable(cnd) #define cnd_wait(cnd, mtx) SleepConditionVariableSRW(cnd, mtx, INFINITE, 0) #define mtx_t_ SRWLOCK #define cnd_t_ CONDITION_VARIABLE #else #define mtx_t_ mtx_t #define cnd_t_ cnd_t #endif #define NUMBER_ALGO 7 #define NUMBER_ITEM 7 struct Global_T { long nNumberCpu; }; static volatile struct Global_T _global; TCHAR tcLogFile[56]; struct Args_T { enumTemplateType eTemplateType; PTP_WORK pwkNewTemplate; _TCHAR tcIniFile[56]; _TCHAR tcSN[56]; _TCHAR tcLicense[56]; long lTemplateSize; int iNetLogin; int used; int total; T* algo; SRWLOCK srw_; static mtx_t_ mtx_; static long once; static long count_ref; }; mtx_t_ Args_T::mtx_; long Args_T::once = 0L; long Args_T::count_ref = 0L; // struct Algo_T { IRecognizePtr m_IRecognize; ITransImagePtr m_ITransImage; IDetectPtr m_pIDetect; IFaceRectifyPtr m_pFaceRectify; IFaceToolsPtr m_pIFaceTool; IFaceTrackingPtr m_pIFaceTrack; IImageProcPtr m_pIImageProc; IImageQualityPtr m_pIImageQuality; IVisionImagePtr m_pIVisionImage; IPositionPtr m_pIPosition; unsigned long nIptrFlag; R m_lpArgs; }; typedef struct Data_T *LpData_T; typedef struct Pipe_T { LpData_T* ptr; size_t length; size_t used; size_t iget; size_t iput; mtx_t_ _mtx; cnd_t_ _cndr; cnd_t_ _cndw; }* LpPipe_T; enum eOptType{ eNewTemplate, eNewTemplate2, eCmprTemplate ,eNullOpt }; struct Item_T { T cl; eOptType opt; HANDLE event; LpPipe_T pipe; LpPipe_T pipe2; long ref_count; long ref_signal; long num_thread; long seq; bool result; static long ref_selfbuf; static I selfbuf[NUMBER_ITEM]; // union { struct { const char* srcpath; const char* wildcard; const char* dstfile; }ready; struct { unsigned char** img; long* length; long size; long size2; const char* imgtype; unsigned char** tmp; }ready2; }; void* pvResult; LpList_T list; SRWLOCK mtx_; }; long Item_T::ref_selfbuf; I Item_T::selfbuf[NUMBER_ITEM]; struct Data_T { union { struct { char* file; }result; struct { unsigned char** img; long* length; long size; const char* imgtype; long pos; }result2; }; void* pvResult; struct { double simi; long seq; long size; }vTemplate; eOptType opt; }; static P Pipe_new(size_t length) { P pi = (P)malloc(sizeof *pi); assert(pi); mtx_init(&pi->_mtx, _Mtx_plain); cnd_init(&pi->_cndr); cnd_init(&pi->_cndw); pi->ptr = (LpData_T*)calloc(length, sizeof LpData_T ); assert(pi->ptr); pi->length = length; pi->used = 0; pi->iget = 0; pi->iput = 0; return pi; } static void Pipe_put(P pi, LpData_T ptr) { assert(pi); mtx_lock(&pi->_mtx); while (pi->used >= pi->length) { cnd_wait(&pi->_cndr, &pi->_mtx); } pi->ptr[pi->iput++] = ptr; ++pi->used; if (pi->iput >= pi->length) { pi->iput = 0; } cnd_signal(&pi->_cndw); mtx_unlock(&pi->_mtx); } static void Pipe_get(P pi, LpData_T* ptr) { assert(pi); mtx_lock(&pi->_mtx); while (pi->used <= 0) { cnd_wait(&pi->_cndw, &pi->_mtx); } --pi->used; *ptr = pi->ptr[pi->iget++]; if (pi->iget >= pi->length) { pi->iget = 0; } cnd_signal(&pi->_cndr); mtx_unlock(&pi->_mtx); } static void Pipe_free(P* pi) { assert(pi && *pi); assert((*pi)->ptr); free((*pi)->ptr); mtx_destroy(&(*pi)->_mtx); cnd_destroy(&(*pi)->_cndr); cnd_destroy(&(*pi)->_cndw); free(*pi); (*pi) = nullptr; } static R Args_new() { static R ag = nullptr; if (0 == InterlockedCompareExchange(&ag->once, 1L, 0L)) { ag = (R)malloc( sizeof *ag ); ag->count_ref = 0L; ag->pwkNewTemplate = nullptr;// CreateThreadpoolWork(__ThreadCreateTemplate, ag, NULL); InitializeSRWLock(&ag->srw_); mtx_init(&ag->mtx_, _Mtx_plain); { TCHAR tcFileName[56], tcModuleFile[MAX_PATH]; SYSTEM_INFO _syInfo; GetSystemInfo(&_syInfo); _global.nNumberCpu = _syInfo.dwNumberOfProcessors; GetModuleFileName(NULL, tcModuleFile, MAX_PATH); _tsplitpath_s(tcModuleFile, NULL, 0, NULL, 0, tcFileName, 56, NULL, 0); _tmakepath_s(ag->tcIniFile, 56, NULL, NULL, tcFileName, _T("ini")); _tmakepath_s(tcLogFile, 56, NULL, NULL, tcFileName, _T("log")); if (_taccess_s(ag->tcIniFile, 0x04) == 0) { GetPrivateProfileString(_T("Register"), _T("sn"), _T(""), ag->tcSN, _countof(ag->tcSN), ag->tcIniFile); GetPrivateProfileString(_T("Register"), _T("license"), _T(""), ag->tcLicense, _countof(ag->tcLicense), ag->tcIniFile); ag->iNetLogin = GetPrivateProfileInt(_T("Register"), _T("netlogin"), 1, ag->tcIniFile); ag->eTemplateType = (enumTemplateType)GetPrivateProfileInt(_T("Parameters"), _T("TemplateType"), 2, ag->tcIniFile); } else { ag->iNetLogin = 1; ag->eTemplateType = (enumTemplateType)2; _tcscpy_s(ag->tcSN, _T("")); _tcscpy_s(ag->tcLicense, _T("")); } } ag->total = _global.nNumberCpu; ag->used = 0; ag->algo = (T*)malloc(ag->total * sizeof T); _tprintf(_T("+++++++++++++++++++++++++++++++++++++++++++++++++++++++Args_new. ")); } InterlockedIncrement(&ag->count_ref); return ag; } static T Algo_get(R ag) { T al; mtx_lock(&ag->mtx_); if (ag->used > 0) al = ag->algo[--ag->used]; else al = nullptr; mtx_unlock(&ag->mtx_); return al; } static bool Algo_put(T al) { R ag = al->m_lpArgs; mtx_lock(&ag->mtx_); if (ag->used < ag->total && ag->count_ref > 0 ) ag->algo[ag->used++] = al; else al = nullptr; mtx_unlock(&ag->mtx_); return al != nullptr; } static I Item_get() { I im = nullptr; mtx_lock(&Args_T::mtx_); if (Item_T::ref_selfbuf > 0) { im = Item_T::selfbuf[--Item_T::ref_selfbuf]; im->ref_count = 0; im->seq = 0; im->cl = nullptr; im->result = false; im->pipe->used = 0; im->pipe->iget = 0; im->pipe->iput = 0; im->pipe2->used = 0; im->pipe2->iget = 0; im->pipe2->iput = 0; } else im = nullptr; mtx_unlock(&Args_T::mtx_); return im; } static bool Item_put(I im) { mtx_lock(&Args_T::mtx_); if (Item_T::ref_selfbuf < NUMBER_ITEM && Args_T::count_ref > 0) Item_T::selfbuf[Item_T::ref_selfbuf++] = im; else im = nullptr; mtx_unlock(&Args_T::mtx_); return im != nullptr; } static void Item_free(I *im) { assert(im && *im); if (!Item_put(*im)) { CloseHandle((*im)->event); Pipe_free(&(*im)->pipe); Pipe_free(&(*im)->pipe2); free(*im); (*im) = nullptr; } } static I Item_new() { I im = Item_get(); if (nullptr == im) { im = (I)malloc(sizeof *im); im->ref_count = 0; im->seq = 0; im->cl = nullptr; im->result = false; InitializeSRWLock(&im->mtx_); im->pipe = Pipe_new(3702); im->pipe2 = Pipe_new(3702); im->event = CreateEvent(NULL, FALSE, FALSE, NULL); } return im; } static void Args_free(R* ag) { long ref; assert(ag && *ag); ref = InterlockedDecrement(&(*ag)->count_ref); if (ref == 0) { T al;// this code is not excute I im; assert(ref == 0); InterlockedExchange(&(*ag)->once, 0L); while (al = Algo_get(*ag)) { Algo_free(&al); } while (im = Item_get()) { Item_free(&im); } mtx_destroy(&(*ag)->mtx_); free((*ag)->algo); free(*ag); (*ag) = nullptr; _tprintf(_T("-------------------------------------------------------Args_free. ")); } } T Algo_new() { R ag = Args_new(); T al = Algo_get(ag); if (!al) { CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_SPEED_OVER_MEMORY); al = new Algo_T; al->nIptrFlag = 0UL; al->m_lpArgs = ag; } assert(al); return al; } static bool NewTemplateFile( const char* filename) { size_t len; if (0 == _access(filename, 0x06)) { return true; } if ( 0 < (len = strlen(filename))) { char* tmpbuf, *p, c; tmpbuf = (char*)malloc(len + 1); strcpy_s(tmpbuf, len + 1, filename); for (p = tmpbuf; *p; ++p) { if ('\' != *p && '/' != *p) { continue; } c = *++p; *p = '