123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- #include "stdafx.h"
- #include "SpBase.h"
- #include "SpIni.h"
- #include "..\\mod_assistantchannel\\AssistantChannel_client_g.h"
- using namespace AssistantChannel;
- #include "..\\mod_AssistantChannel\\chan_protocol.h"
- #include "rec_common.h"
- #include "libvideoqueue.h"
- #include "jpeg2k.h"
- #include "libfacecapture.h"
- #include "y2k_time.h"
- #include "sysvar.h"
- #include "Event.h"
- // 脸部跟踪&拍照 devel: 0x302
- class CFaceTrackingEntity;
- class ChannelClient : public ChannelService_ClientBase
- {
- public:
- ChannelClient(CFaceTrackingEntity *pEntity);
- virtual void OnMessage(ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData);
- };
- class CFaceTrackingEntity : public CEntityBase, public CHostApi , public CVideoMonitorEvent, public ISysVarListener, public ITimerListener,public ILogListener
- {
- public:
- CFaceTrackingEntity() : m_facecapture(NULL), bIsSessionChange(FALSE),strCustomerID(false),strSessionID(false),bIsCustomerChange(FALSE)
- {
- // note: this object initialize at DllMain, so it suggests keep your code simple here.
- // 1) do simple initializing here
- // 2) dont do complex operation, complex operation such as create new process(thread) and so on
- }
- virtual ~CFaceTrackingEntity() {}
- virtual const char *GetEntityName() const { return "FaceTracking"; }
- virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- LOG_FUNCTION();
- //MessageBoxA(0,0,0,0);
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- ErrorCodeEnum Error;
- BOOL bRet = FALSE;
- m_facecapture = new Clibfacecapture(&bRet, this, this, REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE, REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE, REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
- if (!bRet)
- {
- Dbg("load libface capture failed!");
- pTransactionContext->SendAnswer(Error_Resource);
- return;
- }
- Dbg("load libface capture ok!");
- #if 0
- m_pChannelClient = new ChannelClient(this);
- Error = m_pChannelClient->Connect();
- if (Error != Error_Succeed) {
- m_pChannelClient->SafeDelete();
- pTransactionContext->SendAnswer(Error);
- return;
- }
- ChannelService_BeginRecv_Sub Sub;
- Sub.type = ACM_TYPE_PHT;
- Error = m_pChannelClient->BeginRecv(Sub);
- if (Error != Error_Succeed) {
- m_pChannelClient->GetFunction()->CloseSession();
- m_pChannelClient->SafeDelete();
- m_pChannelClient = NULL;
- pTransactionContext->SendAnswer(Error);
- return;
- }
- #endif
- Error = GetFunction()->RegistSysVarEvent("SessionID", this);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("register sysvar %s failed!", "SessionID");
- }
- Error = GetFunction()->RegistSysVarEvent("CustomerID", this);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("register sysvar %s failed!", "CustomerID");
- }
- Error = GetFunction()->RegistSysVarEvent("CameraState", this);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("register sysvar %s failed!", "CameraState");
- }
- CSimpleStringA strValue;
- GetFunction()->GetSysVar("CameraState", strValue);
- if (strValue[0] == 'N')
- {
- m_facecapture->SetCameraState(0);
- Dbg("set Camera State to 0");
- }
- else if (strValue[0] == 'E')
- {
- m_facecapture->SetCameraState(1);
- Dbg("set Camera State to 1");
- }
- else if (strValue[0] == 'O')
- {
- m_facecapture->SetCameraState(2);
- Dbg("set Camera State to 2");
- }
- else if (strValue[0] == 'B')
- {
- m_facecapture->SetCameraState(3);
- Dbg("set Camera State to 3");
- }
- //is Pad Version
- CSystemStaticInfo stStaticinfo;
- spFunction->GetSystemStaticInfo(stStaticinfo);
- if (stricmp(stStaticinfo.strMachineType,"RVC.PAD")==0)
- {
- m_bIsPadType = TRUE;
- }
- else
- {
- m_bIsPadType = FALSE;
- }
- //int nCount;
- //Error = DecideCameraCount(nCount);
- //if (Error != Error_Succeed)
- //{
- // LOG_TRACE("decide camera count failed!");
- //}
- //Dbg("camera count:%d", nCount);
- if (!m_bIsPadType)
- {
- spFunction->SetTimer(1, this, 500);
- }
- spFunction->SubscribeLog(m_UUid1, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_HEADLIGHT_GREEN_OFF,NULL,false);
- spFunction->SubscribeLog(m_UUid2, this, Log_Event, Severity_None, Error_IgnoreAll, EVENT_MOD_BEGIN_RECORD,NULL,false);
- spFunction->SubscribeLog(m_UUid3, this, Log_Event, Severity_None, Error_IgnoreAll, EVENT_MOD_END_RECORD,NULL,false);
- pTransactionContext->SendAnswer(Error);
- }
- virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- LOG_FUNCTION();
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- spFunction->KillTimer(1); // if timer 1 not exist also ok
- delete m_facecapture;
- m_facecapture = NULL;
- spFunction->UnsubscribeLog(m_UUid1);
- spFunction->UnsubscribeLog(m_UUid2);
- spFunction->UnsubscribeLog(m_UUid3);
- spFunction->UnregistSysVarEvent("SessionID");
- spFunction->UnregistSysVarEvent("CustomerID");
- spFunction->UnregistSysVarEvent("CameraState");
- pTransactionContext->SendAnswer(Error_Succeed);
- }
- virtual void OnPaused()
- {
- Capture(0);
- }
- void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
- const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
- const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage)
- {
- if (dwUserCode == LOG_EVT_HEADLIGHT_GREEN_OFF)
- {
- Dbg("set light change");
- m_facecapture->SetLightChange();
- }
- else if (dwUserCode == EVENT_MOD_BEGIN_RECORD)
- {
- CSimpleStringA strValue;
- GetFunction()->GetSysVar("CameraState", strValue);
- if (strValue[0] == 'N')
- {
- m_facecapture->SetCameraState(0);
- Dbg("Begin start Camera,set Camera State to 0");
- }
- else if (strValue[0] == 'E')
- {
- m_facecapture->SetCameraState(1);
- Dbg("Begin start Camera,set Camera State to 1");
- }
- else if (strValue[0] == 'O')
- {
- m_facecapture->SetCameraState(2);
- Dbg("Begin start Camera,set Camera State to 2");
- }
- else if (strValue[0] == 'B')
- {
- m_facecapture->SetCameraState(3);
- Dbg("Begin start Camera,set Camera State to 3");
- }
- }
- else if (dwUserCode == EVENT_MOD_END_RECORD)
- {
- Dbg(" stop Camera,set camera stat to Error");
- m_facecapture->SetCameraState(3);
- }
- }
- void Capture(int id)
- {
- #if 0
- CImageFrame frm;
- frm.data = new unsigned char[REC_COMMON_VIDEO_SNAPSHOT_WIDTH*REC_COMMON_VIDEO_SNAPSHOT_HEIGHT*4];
- frm.width = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
- frm.height = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
- frm.framesize = frm.width * frm.height * 3;
- if (m_facecapture->SnapShot(frm)) {
- Dbg("get video frm ok!");
- int rc;
- jpeg2k_raw_image raw_image;
- jpeg2k_coded_image codec_image = {0};
- raw_image.data = frm.data;
- raw_image.width = frm.width;
- raw_image.height = frm.height;
- raw_image.len = raw_image.width * raw_image.height * 3;
- rc = jpeg2k_encode(&raw_image, &codec_image, 10);
- if (rc == 0) {
- ChannelService_Send_Info Info;
- Info.compress = false;
- Info.encrypt = false;
- Info.type = ACM_TYPE_PHT;
- Info.id = id;
- Info.sub_type = ACM_PHT_ANS | ACM_PHT_SNAPSHOT;
- Info.data.m_pData = codec_image.data;
- Info.data.m_iLength = codec_image.len;
- m_pChannelClient->Send(Info);
- }
- jpeg2k_encode_free(&codec_image);
- } else {
- Dbg("get video frm failed!");
- }
- delete frm.data;
- #endif
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // ITimerListener implementation
-
- virtual void OnTimeout(DWORD dwTimerID)
- {
- //LOG_FUNCTION();
- __int64 uid;
- CCustomerStatus status;
- //Dbg("before GetMainCustomerStatus");
- if (m_facecapture->GetMainCustomerStatus(uid, status))
- {
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- CSimpleStringA strValue;
- //Dbg("OnTimeout, check value, eCamera = %d", status.stCustomerPos.eCamera);
- spFunction->GetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, strValue); // E or O
- if (status.stCustomerPos.eCamera == EnvironCamera)
- {
- if (strValue[0] == 'O')
- {
- //Dbg("ActiveTrackingCamera change from Opt->Env!");
- spFunction->SetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, ACTIVETRACKINGCAMERA_ENV); // from Operation -> Environment
- }
- }
- else
- {
- if (strValue[0] == 'E')
- {
- //Dbg("ActiveTrackingCamera change from Env->Opt!");
- spFunction->SetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, ACTIVETRACKINGCAMERA_OPT); // from Environment -> Operation
- }
- }
- } else
- {
- Dbg("GetMainCustomerStatus failed!");
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // CHostApi implementation
- virtual unsigned __int64 GenerateUUID()
- {
- m_lastUUID = CUUID::Create(m_lastUUID);
- return m_lastUUID;
- }
- virtual BOOL IsCustomerChange()
- {
- BOOL bChange = bIsSessionChange||bIsCustomerChange;
- return bChange;
- }
- virtual void GetFaceImgName(char* FaceName)
- {
- CSimpleStringA strPath;
- ErrorCodeEnum Error = GetFunction()->GetPath("UploadPhoto", strPath);
- if (Error == Error_Succeed)
- {
- if (((const char*)strCustomerID == NULL)||(_stricmp(strCustomerID,"N") == 0))
- {
- sprintf(FaceName, "%s\\%s_0", (LPCSTR)strPath, strSessionID);
- }
- else
- {
- sprintf(FaceName, "%s\\%s_%s", (LPCSTR)strPath, strSessionID,strCustomerID);
- }
- //Debug("get file name =%s",FaceName);
- bIsSessionChange = FALSE;
- bIsCustomerChange = FALSE;
- }
- else
- {
- Dbg("getpath uploadphoto failed!");
- }
- }
- virtual BOOL LoadConfig(CFaceCaptureConfig &config)
- {
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- CSmartPointer<IConfigInfo> spConfig;
- ErrorCodeEnum Error = spFunction->OpenConfig(Config_Software, spConfig);
- if (Error == Error_Succeed) {
- SpIniMappingTable table;
- CSimpleStringA strPath;
- config.nServersType = 0;
- GetFunction()->GetPath("cfg", strPath);
- strcpy(config.strFaceDataDirPath, strPath);
- strcat(config.strFaceDataDirPath, "\\facedata");
- table.AddEntryInt("general", "PrimCamera", config.nPrimCamera, 0);
- table.AddEntryInt("general", "ContourMinAera", config.nContourMinAera, 0);
- table.AddEntryInt("general", "UpCameraEdgeLimit", config.nUpCameraEdgeLimit, 0);
- table.AddEntryInt("general", "DownCameraEdgeLimit", config.nDownCameraEdgeLimit, 0);
- table.AddEntryString("general", "FaceDataDirPath", strPath, 0);
- table.AddEntryFloat("general", "OperateFaceSize", config.fOperateFaceSize, 0.0f);
- table.AddEntryFloat("general", "CloseFaceSize", config.fCloseFaceSize, 0.0f);
- table.AddEntryFloat("general", "FarFaceSize", config.fFarFaceSize, 0.0f);
- table.AddEntryFloat("general", "SearchFaceSize", config.fSearchFaceSize, 0.0f);
- table.AddEntryFloat("general", "DetectFaceSize", config.fDetectFaceSize, 0.0f);
- table.AddEntryInt("general", "FaceSizeOffset", config.nFaceSizeOffset, 0);
- table.AddEntryInt("general", "SleepLong", config.nSleepLong, 0);
- table.AddEntryInt("general", "SleepMiddle", config.nSleepMiddle, 0);
- table.AddEntryInt("general", "SleepShort", config.nSleepShort, 0);
- table.AddEntryInt("general", "ThresholdNum", config.nThresholdNum, 0);
- Error = table.Load(spConfig);
- }
- return Error == Error_Succeed;
- }
- virtual void Debug(const char *fmt, ...)
- {
- va_list arg;
- va_start(arg, fmt);
- vDbg(fmt, arg);
- va_end(arg);
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // CVideoMonitorEvent
- virtual void GenerateCloseEvent()
- {
- LogEvent(Severity_Middle, 0x30200001, "客户接近 1.5m");
- }
- virtual void GenerateLeaveEvent()
- {
- LogEvent(Severity_Middle, 0x30200002, "客户消失或退出到接近距离1.5M之外");
- }
- virtual void GenerateEnterOperateEvent()
- {
- LogEvent(Severity_Middle, 0x30200003, "客户进入操作距离0.5m");
- }
- virtual void GenerateBackToCloseEvent()
- {
- LogEvent(Severity_Middle, 0x30200004, "客户退回到接近距离");
- }
- virtual void GenerateAppearEvent() //有人出现在视野中
- {
- LogEvent(Severity_Middle, 0x30200006, "有人出现");
- }
- virtual void GenerateCustomerChangeEvent()
- {
- LogEvent(Severity_Middle, 0x30200005, "客户已经换人");
- }
- virtual void GenerateCaptureFaceEvent()
- {
- LogEvent(Severity_Middle, 0x30200011, "捕获脸部事件");
- }
- virtual void GenerateLoseFaceEvent()
- {
- LogEvent(Severity_Middle, 0x30200012, "失去脸部事件");
- }
-
- virtual void GenerateFaceCaptureFC()
- {
- //发送最高等级错误事件,由健康模块重启对应实体
- LogEvent(Severity_High,EVENT_MOD_FACE_BREAKDOWN,"the opencv library breakdown");
- }
- private:
- virtual void OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
- {
- if (_stricmp(pszKey,"SessionID") == 0)
- {
- GetFunction()->GetSysVar("SessionID",strSessionID);
- //Dbg("sessionID change to %s",strSessionID);
- bIsSessionChange = TRUE;
- }
- if (_stricmp(pszKey,"CustomerID") == 0)
- {
- GetFunction()->GetSysVar("CustomerID",strCustomerID);
- //Dbg("customerID change to %s",strCustomerID);
- bIsCustomerChange = TRUE;
- }
- if (_stricmp(pszKey,"CameraState") == 0)
- {
- CSimpleStringA str;
- GetFunction()->GetSysVar("CameraState",str);
- //Dbg("customerID change to %s",strCustomerID);
- if (str == 'N')
- {
- m_facecapture->SetCameraState(0);
- Dbg("set Camera State to 0");
- }
- else if (str == 'E')
- {
- m_facecapture->SetCameraState(1);
- Dbg("set Camera State to 1");
- }
- else if (str == 'O')
- {
- m_facecapture->SetCameraState(2);
- Dbg("set Camera State to 2");
- }
- else if (str == 'B')
- {
- m_facecapture->SetCameraState(3);
- Dbg("set Camera State to 3");
- }
- }
- }
- // we use root.ini Video section config to decide camera count
- ErrorCodeEnum DecideCameraCount(int &nCount)
- {
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- CSmartPointer<IConfigInfo> spConfig;
- ErrorCodeEnum Error = spFunction->OpenConfig(Config_Root, spConfig);
- if (Error == Error_Succeed)
- {
- CSimpleStringA strVideoEnv;
- CSimpleStringA strVideoOpt;
- SpIniMappingTable table;
- nCount = 0;
- table.AddEntryString("Video", "EnvCamera", strVideoEnv, "$");
- table.AddEntryString("Video", "OptCamera", strVideoOpt, "$");
- Error = table.Load(spConfig);
- if (Error == Error_Succeed)
- {
- if (strVideoEnv.GetLength() > 1)
- nCount++;
- if (strVideoOpt.GetLength() > 1)
- nCount++;
- }
- }
- return Error;
- }
- private:
- CUUID m_UUid1,m_UUid2,m_UUid3;
- BOOL bIsSessionChange;
- BOOL bIsCustomerChange;
- BOOL m_bIsPadType;
- CSimpleStringA strCustomerID;
- CSimpleStringA strSessionID;
- CUUID m_lastUUID;
- ChannelClient *m_pChannelClient;
- Clibfacecapture *m_facecapture;
- };
- void ChannelClient::OnMessage( ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData )
- {
- LOG_FUNCTION();
- if (Error == Error_Succeed)
- {
- CFaceTrackingEntity *pEntity = static_cast<CFaceTrackingEntity*>(m_pEntityBase);
- int cat = ACM_PHT_CAT(Msg.sub_type);
- if (cat == ACM_PHT_REQ)
- {
- //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
- pEntity->Capture(Msg.id);
- //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
- } else
- {
- _ASSERT(0);
- }
- }
- }
- ChannelClient::ChannelClient( CFaceTrackingEntity *pEntity ) : ChannelService_ClientBase(pEntity)
- {
- }
- SP_BEGIN_ENTITY_MAP()
- SP_ENTITY(CFaceTrackingEntity)
- SP_END_ENTITY_MAP()
|