diff --git a/ECTrainer1/ECTrainer1Dlg.cpp b/ECTrainer1/ECTrainer1Dlg.cpp index 8349ae9..a68c105 100644 --- a/ECTrainer1/ECTrainer1Dlg.cpp +++ b/ECTrainer1/ECTrainer1Dlg.cpp @@ -511,7 +511,7 @@ Point2f gp; std::ofstream ofs; int64 start = 0; - RingBuffer gpCx(DATA_MEAN_SIZE), gpCy(DATA_MEAN_SIZE); + MeanBuffer gpCx(DATA_MEAN_SIZE), gpCy(DATA_MEAN_SIZE); Sleep(500); // 最初のKeepAlive送信待ち diff --git a/ECTrainer1/RingBuffer.h b/ECTrainer1/RingBuffer.h index 0201a42..df36e8f 100644 --- a/ECTrainer1/RingBuffer.h +++ b/ECTrainer1/RingBuffer.h @@ -2,7 +2,7 @@ #include -class RingBuffer +class MeanBuffer { float *_buffer; int _size; @@ -10,7 +10,7 @@ float _sum; public: - RingBuffer(int size) { + MeanBuffer(int size) { _size = size; _next = 0; _sum = 0; @@ -18,7 +18,7 @@ for (int i = 0; i < _size; i++) _buffer[i] = 0; } - ~RingBuffer() { + ~MeanBuffer() { delete[] _buffer; } diff --git a/ECTrainer2/BaseProcess.cpp b/ECTrainer2/BaseProcess.cpp index 52e924d..7f8440f 100644 --- a/ECTrainer2/BaseProcess.cpp +++ b/ECTrainer2/BaseProcess.cpp @@ -14,7 +14,7 @@ // ���[�v bool BaseProcess::MainLoop() { - while (_pEct->IsRunning()) { + while (_pEct->IsAppRun()) { Sleep(1); } return true; diff --git a/ECTrainer2/BitalMonitor.cpp b/ECTrainer2/BitalMonitor.cpp index 9732219..960b545 100644 --- a/ECTrainer2/BitalMonitor.cpp +++ b/ECTrainer2/BitalMonitor.cpp @@ -5,15 +5,16 @@ // �R���X�g���N�^ BitalMonitor::BitalMonitor(ECTrainer* pEct) - :BaseProcess(pEct) { - + :BaseProcess(pEct) + , _rrinterval(ECTrainer::RINGBUFSIZE) + , _heartbeat(ECTrainer::RINGBUFSIZE) +{ } - // ������ bool BitalMonitor::Init() { - bool rv = _Com.Open(4, _T("baud=19200 parity=N data=8 stop=1")); + bool rv = _Com.Open(COM_PORT, _T("baud=19200 parity=N data=8 stop=1")); return rv; } @@ -27,7 +28,7 @@ const char LF = 10; int count = 0; - while (_pEct->IsRunning()) { + while (_pEct->IsAppRun()) { // �o�C�^�� DWORD readBytes = _Com.Receive((BYTE*)comBuf, 256); @@ -44,7 +45,10 @@ //std::cout << "value:" << sp << std::endl; if (*sp == '#') { int RR = atoi(sp + 1); - if (RR > 0) _pEct->SetHeartBeat(60000 / RR); + if (RR > 0) { + _rrinterval.Put(RR); + _heartbeat.Put(60000 / RR); + } //std::cout << "Read:" << RR << std::endl; } sp = pt + 1; diff --git a/ECTrainer2/BitalMonitor.h b/ECTrainer2/BitalMonitor.h index fef6606..f7528a7 100644 --- a/ECTrainer2/BitalMonitor.h +++ b/ECTrainer2/BitalMonitor.h @@ -2,14 +2,20 @@ #include "BaseProcess.h" #include "GComPort.h" +#include "RingBuffer.h" class BitalMonitor : public BaseProcess { + const int COM_PORT = 4; GComPort _Com; + RingBuffer _rrinterval; + RingBuffer _heartbeat; public: BitalMonitor(ECTrainer* pEct); bool Init(); bool MainLoop(); + int GetRR() { return _rrinterval.Get(); } + int GetHB() { return _heartbeat.Get(); } }; diff --git a/ECTrainer2/ECTrainer.cpp b/ECTrainer2/ECTrainer.cpp index 4aae2bb..04cf562 100644 --- a/ECTrainer2/ECTrainer.cpp +++ b/ECTrainer2/ECTrainer.cpp @@ -8,14 +8,14 @@ #include "EyeTrack.h" #include "TobiiREST.h" #include "BitalMonitor.h" +#include "Worker.h" // �R���X�g���N�^ ECTrainer::ECTrainer(HINSTANCE hInstance) - : _Running(true) + : _AppRunning(true) , _HomographyOK(false) , _CalibResult(0) , _hInstance(hInstance) - , _HeartBeat(0) { _pMarker = new Marker(); _pProcs[GUI] = new ECTrainerGUI(this); @@ -25,6 +25,7 @@ _pProcs[EYETR] = new EyeTrack(this, _pMarker); _pProcs[REST] = new TobiiREST(this); _pProcs[BITAL] = new BitalMonitor(this); + _pProcs[WORKER] = new Worker(this); _MovieToShow = _T(""); } @@ -39,7 +40,7 @@ bool ECTrainer::Process() { // �v���Z�X������ if (!((ECTrainerGUI*)_pProcs[GUI])->Init(_hInstance)) return false; - for (int i = 0; i < PROC::NUM; i++) { + for (int i = 1; i < PROC::NUM; i++) { if (!_pProcs[i]->Init()) return false; } @@ -121,7 +122,7 @@ // ����摜���̒����_�ݒ� void ECTrainer::SetGazeV(cv::Point gazeV) { - ((EyeTrack*)_pProcs[EYETR])->SetGazeV(gazeV); + //((EyeTrack*)_pProcs[EYETR])->SetGazeV(gazeV); } // ����摜���̒����_�擾 diff --git a/ECTrainer2/ECTrainer.h b/ECTrainer2/ECTrainer.h index 94831c9..f70df5d 100644 --- a/ECTrainer2/ECTrainer.h +++ b/ECTrainer2/ECTrainer.h @@ -5,37 +5,47 @@ #define ADDR "192.168.71.50" +class BaseProcess; class ECTrainerGUI; +class ImageProc; class SceneCamera; class Stimulus; -class Marker; -class ImageProc; class EyeTrack; class TobiiREST; class BitalMonitor; -class BaseProcess; +class Worker; +class Marker; class ECTrainer { private: - enum PROC {GUI, IMGPROC, SCNCAM, STIM, EYETR, REST, BITAL, NUM }; - HINSTANCE _hInstance; + enum PROC { GUI, IMGPROC, SCNCAM, STIM, EYETR, REST, BITAL, WORKER, NUM }; BaseProcess* _pProcs[PROC::NUM]; + HINSTANCE _hInstance; Marker* _pMarker; - bool _Running; // ���s���t���O + bool _AppRunning; // ���s���t���O bool _HomographyOK; // �ϊ��s��̊l���L�� int _CalibResult; // �L�����u���[�V�������� 0:�����{ 1:���� -1:���s cv::Size _SceneSize; // ���E�J�����̉摜�T�C�Y std::wstring _MovieToShow; // �Đ����铮��t�@�C�� - int _HeartBeat; // �S���� // �X���b�h�J�n�_ static DWORD WINAPI ThreadEntry(LPVOID lpParameter); static DWORD WINAPI KeepAliveThreadEntry(LPVOID lpParameter); public: + static const int RINGBUFSIZE = 4; ECTrainer(HINSTANCE hInstance); ~ECTrainer(); + ECTrainerGUI* PECTrainerGUI() { return (ECTrainerGUI*)_pProcs[GUI]; } + ImageProc* PImageProc() { return (ImageProc*)_pProcs[IMGPROC]; } + SceneCamera* PSceneCamera() { return (SceneCamera*)_pProcs[SCNCAM]; } + Stimulus* PStimulus() { return (Stimulus*)_pProcs[STIM]; } + EyeTrack* PEyeTrack() { return (EyeTrack*)_pProcs[EYETR]; } + TobiiREST* PTobiiREST() { return (TobiiREST*)_pProcs[REST]; } + BitalMonitor* PBitalMonitor() { return (BitalMonitor*)_pProcs[BITAL]; } + Worker* PWorker() { return (Worker*)_pProcs[WORKER]; } + Marker* PMarker() { return _pMarker; } bool Process(); void CalibStart(); int CheckCalibResult(); @@ -52,8 +62,8 @@ cv::Point2f GetEyeL(); // �C�����C���֐� - bool IsRunning() { return _Running; } - void Stop() { _Running = false; } + bool IsAppRun() { return _AppRunning; } // �A�v���P�[�V�������s�����ǂ��� + void StopApp() { _AppRunning = false; } // �A�v���P�[�V�������~ void SetHomographyStatus(bool ok) { _HomographyOK = ok; } bool GetHomographyStatus() { return _HomographyOK; } void SetSceneSize(cv::Size s) { _SceneSize = s; } @@ -61,7 +71,4 @@ void SetCalibResult(int result) { _CalibResult = result; } void SetMovieToShow(std::wstring file) { _MovieToShow = file; } std::wstring GetMovieToShow() { return _MovieToShow; } - void SetHeartBeat(int hb) { _HeartBeat = hb; } - int GetHeartBeat() { return _HeartBeat; } - }; diff --git a/ECTrainer2/ECTrainer2.vcxproj b/ECTrainer2/ECTrainer2.vcxproj index 64b857a..4add593 100644 --- a/ECTrainer2/ECTrainer2.vcxproj +++ b/ECTrainer2/ECTrainer2.vcxproj @@ -155,6 +155,7 @@ + @@ -169,10 +170,12 @@ + + diff --git a/ECTrainer2/ECTrainer2.vcxproj.filters b/ECTrainer2/ECTrainer2.vcxproj.filters index 719b44b..810db5f 100644 --- a/ECTrainer2/ECTrainer2.vcxproj.filters +++ b/ECTrainer2/ECTrainer2.vcxproj.filters @@ -54,6 +54,9 @@ ソース ファイル + + ソース ファイル + @@ -86,9 +89,6 @@ ヘッダー ファイル - - ヘッダー ファイル - ヘッダー ファイル @@ -104,6 +104,15 @@ ヘッダー ファイル + + ヘッダー ファイル + + + ヘッダー ファイル + + + ヘッダー ファイル + diff --git a/ECTrainer2/ECTrainerGUI.cpp b/ECTrainer2/ECTrainerGUI.cpp index 723cac8..2e0189f 100644 --- a/ECTrainer2/ECTrainerGUI.cpp +++ b/ECTrainer2/ECTrainerGUI.cpp @@ -1,6 +1,7 @@ -#include "ECTrainer.h" #include "ECTrainerGUI.h" +#include "ECTrainer.h" +#include "BitalMonitor.h" #include #pragma comment(lib,"winmm.lib") #include @@ -33,7 +34,7 @@ , _SceneBufferHeight(0) , _DispImageHeight(0) , _SceneBufferScale(1.F) - , _PlayIndex(0) + //, _PlayIndex(0) { //_MovieFiles.push_back(_T("../movies/instruction.avi")); //_MovieFiles.push_back(_T("../movies/test4.avi")); @@ -47,6 +48,7 @@ // �f�X�g���N�^ ECTrainerGUI::~ECTrainerGUI() { + SAFE_RELEASE(_pMediaPosition); SAFE_RELEASE(_pMediaEvent); SAFE_RELEASE(_pMediaCont); SAFE_RELEASE(_pCGB2); @@ -146,7 +148,7 @@ float targetSize = 2.0; clock_t startContact = 0; - while (_pEct->IsRunning() && cv::getWindowProperty(WIN_MAIN, 0) >= 0) { + while (_pEct->IsAppRun() && cv::getWindowProperty(WIN_MAIN, 0) >= 0) { // ���C���E�C���h�E�t���[���N���A _MainFrame = cv::Scalar(49, 52, 49); @@ -168,50 +170,59 @@ imagePos.y = (float)(mp.y - DISP_IMAGE_POS.y) / _DispImageHeight; } - // �h���摜 + // �\���o�b�t�@���� + cv::Mat CurDisplay = _DispImage.clone(); + + // �����_�̕\�� if (_pEct->GetGazeV().x >= 0) { cv::circle(_SceneBuffer, cv::Point((int)(_pEct->GetGazeV().x * _SceneBufferScale), (int)(_pEct->GetGazeV().y * _SceneBufferScale)), 10, CV_RGB(0, 0, 255), 2); } - fContact = false; - if (fStimulus) { - float dx = _pEct->GetEyeR().x - _pEct->GetEyeL().x; - float dy = _pEct->GetEyeR().y - _pEct->GetEyeL().y; - float eyesDistance = sqrtf(dx * dx + dy * dy); - float areaSize = eyesDistance * targetSize / 2.0F; - if (fShowEyesPos) { - cv::circle(_DispImage, - cv::Point((int)(_DispImage.cols * _pEct->GetEyeR().x), (int)(_DispImage.rows * _pEct->GetEyeR().y)), - (int)(_DispImage.rows * 0.03), CV_RGB(0, 128, 0), 2); - cv::circle(_DispImage, - cv::Point((int)(_DispImage.cols * _pEct->GetEyeL().x), (int)(_DispImage.rows * _pEct->GetEyeL().y)), - (int)(_DispImage.rows * 0.03), CV_RGB(0, 128, 0), 2); - cv::circle(_DispImage, - cv::Point((int)(_DispImage.cols * _pEct->GetEyeR().x), (int)(_DispImage.rows * _pEct->GetEyeR().y)), - (int)(_DispImage.cols * areaSize), CV_RGB(200, 200, 0), 2); - cv::circle(_DispImage, - cv::Point((int)(_DispImage.cols * _pEct->GetEyeL().x), (int)(_DispImage.rows * _pEct->GetEyeL().y)), - (int)(_DispImage.cols * areaSize), CV_RGB(200, 200, 0), 2); - } - if (_pEct->GetGazeI().x >= 0) { - float dxR = _pEct->GetEyeR().x - _pEct->GetGazeI().x; - float dyR = _pEct->GetEyeR().y - _pEct->GetGazeI().y; - float edR = sqrtf(dxR * dxR + dyR * dyR); - float dxL = _pEct->GetEyeL().x - _pEct->GetGazeI().x; - float dyL = _pEct->GetEyeL().y - _pEct->GetGazeI().y; - float edL = sqrtf(dxL * dxL + dyL * dyL); - if (edR <= areaSize || edL <= areaSize) { - fContact = true; - } - } - if (_pEct->GetGazeV().x > 200 && _pEct->GetGazeV().x < 1600 - && _pEct->GetGazeV().y >100 && _pEct->GetGazeV().y < 900 - && _PlayIndex % 2 == 1) { - fContact = true; - } + if (_pEct->GetGazeI().x >= 0) { + cv::circle(CurDisplay, cv::Point((int)_pEct->GetGazeI().x/2, + (int)_pEct->GetGazeI().y/2), 10, CV_RGB(0, 0, 255), 2); } + // �h���摜 + //fContact = false; + //if (fStimulus) { + // float dx = _pEct->GetEyeR().x - _pEct->GetEyeL().x; + // float dy = _pEct->GetEyeR().y - _pEct->GetEyeL().y; + // float eyesDistance = sqrtf(dx * dx + dy * dy); + // float areaSize = eyesDistance * targetSize / 2.0F; + // if (fShowEyesPos) { + // cv::circle(_DispImage, + // cv::Point((int)(_DispImage.cols * _pEct->GetEyeR().x), (int)(_DispImage.rows * _pEct->GetEyeR().y)), + // (int)(_DispImage.rows * 0.03), CV_RGB(0, 128, 0), 2); + // cv::circle(_DispImage, + // cv::Point((int)(_DispImage.cols * _pEct->GetEyeL().x), (int)(_DispImage.rows * _pEct->GetEyeL().y)), + // (int)(_DispImage.rows * 0.03), CV_RGB(0, 128, 0), 2); + // cv::circle(_DispImage, + // cv::Point((int)(_DispImage.cols * _pEct->GetEyeR().x), (int)(_DispImage.rows * _pEct->GetEyeR().y)), + // (int)(_DispImage.cols * areaSize), CV_RGB(200, 200, 0), 2); + // cv::circle(_DispImage, + // cv::Point((int)(_DispImage.cols * _pEct->GetEyeL().x), (int)(_DispImage.rows * _pEct->GetEyeL().y)), + // (int)(_DispImage.cols * areaSize), CV_RGB(200, 200, 0), 2); + // } + // if (_pEct->GetGazeI().x >= 0) { + // float dxR = _pEct->GetEyeR().x - _pEct->GetGazeI().x; + // float dyR = _pEct->GetEyeR().y - _pEct->GetGazeI().y; + // float edR = sqrtf(dxR * dxR + dyR * dyR); + // float dxL = _pEct->GetEyeL().x - _pEct->GetGazeI().x; + // float dyL = _pEct->GetEyeL().y - _pEct->GetGazeI().y; + // float edL = sqrtf(dxL * dxL + dyL * dyL); + // if (edR <= areaSize || edL <= areaSize) { + // fContact = true; + // } + // } + // if (_pEct->GetGazeV().x > 200 && _pEct->GetGazeV().x < 1600 + // && _pEct->GetGazeV().y >100 && _pEct->GetGazeV().y < 900 + // && _PlayIndex % 2 == 1) { + // fContact = true; + // } + //} + // �R���^�N�g���� if (fContact) { if (contactLevel == 0) { @@ -242,18 +253,18 @@ cvui::beginColumn(_MainFrame, 10, 100, 140, -1, 10); if (fStimulus) { if (cvui::button(140, 30, "STOP")) { - if (!StopMovie()) _pEct->Stop(); + if (!StopMovie()) _pEct->StopApp(); //fKeepContact = false; _pEct->StopStim(); fStimulus = false; } - if (cvui::button(140, 30, "NEXT")) { - //fKeepContact = false; - //if (!StopMovie()) _pEct->Stop(); - //_PlayIndex = (_PlayIndex + 1) % _MovieFiles.size(); - //if (!PlayMovie()) _pEct->Stop(); - //_pEct->Next(); - } + //if (cvui::button(140, 30, "NEXT")) { + // //fKeepContact = false; + // //if (!StopMovie()) _pEct->Stop(); + // //_PlayIndex = (_PlayIndex + 1) % _MovieFiles.size(); + // //if (!PlayMovie()) _pEct->Stop(); + // //_pEct->Next(); + //} cvui::trackbar(140, &targetSize, (float)0, (float)3.0); cvui::checkbox("Show Eyes", &fShowEyesPos); cvui::text(fContact ? "Eyes Contact!" : "No contact"); @@ -277,24 +288,31 @@ cvui::printf("Img Gaze %.2f, %.2f", _pEct->GetGazeI().x, _pEct->GetGazeI().y); cvui::text(_pEct->GetHomographyStatus() ? "Homography OK" : "No homography"); cvui::printf("Battery %d %%", _pEct->BatteryLevel()); + if (IsPlaying()) { + REFTIME tm; + _pMediaPosition->get_CurrentPosition(&tm); + cvui::printf("Movie pos: %.1f", tm); + } bool snapshot = cvui::button(140, 30, "SNAPSHOT"); - if (cvui::button(140, 30, "QUIT")) _pEct->Stop(); - cvui::printf("HeartBeat (bpm)", _pEct->GetHeartBeat()); - cvui::text(cv::format("%3d", _pEct->GetHeartBeat()), 2.0); + if (cvui::button(140, 30, "QUIT")) _pEct->StopApp(); + cvui::printf("HeartBeat (bpm)"); + cvui::text(cv::format("%3d", _pEct->PBitalMonitor()->GetHB()), 2.0); cvui::endColumn(); cvui::text(_MainFrame, SCENE_BUFFER_POS.x, 100, "SCENE CAMERA"); cvui::image(_MainFrame, SCENE_BUFFER_POS.x, SCENE_BUFFER_POS.y, _SceneBuffer); cvui::text(_MainFrame, DISP_IMAGE_POS.x, 100, "STIMULUS IMAGE"); - cvui::image(_MainFrame, DISP_IMAGE_POS.x, DISP_IMAGE_POS.y, _DispImage); + cv::Mat disp; + cv::resize(CurDisplay, disp, cv::Size(IMAGE_WIDTH, _DispImageHeight)); + cvui::image(_MainFrame, DISP_IMAGE_POS.x, DISP_IMAGE_POS.y, disp); // �h������ std::wstring movieToShow = _pEct->GetMovieToShow(); if (movieToShow.size() > 0) { _pEct->SetMovieToShow(L""); if (movieToShow == L"STOP") { - if (!StopMovie()) _pEct->Stop(); + if (!StopMovie()) _pEct->StopApp(); } else { - if (!PlayMovie(movieToShow)) _pEct->Stop(); + if (!PlayMovie(movieToShow)) _pEct->StopApp(); } } @@ -303,7 +321,7 @@ long eventCode; _pMediaEvent->WaitForCompletion(1, &eventCode);//����̏�Ԃ�⑫ if (eventCode != 0) { - if (!StopMovie()) _pEct->Stop(); + if (!StopMovie()) _pEct->StopApp(); //_PlayIndex = (_PlayIndex + 1) % _MovieFiles.size(); //if (!PlayMovie()) _pEct->Stop(); } @@ -341,9 +359,9 @@ PlaySound(_T("../voices/Excellent_L.wav"), NULL, SND_FILENAME | SND_ASYNC); break; } - if (key == KEY_ESC) _pEct->Stop(); + if (key == KEY_ESC) _pEct->StopApp(); } - _pEct->Stop(); + _pEct->StopApp(); return true; } @@ -395,6 +413,9 @@ // ���f�B�A�C�x���g�C���^�[�t�F�C�X�̎擾 CHECK(_pGB->QueryInterface(IID_IMediaEvent, (void**)&_pMediaEvent)); + // ���f�B�A�|�W�V�����C���^�[�t�F�C�X�̎擾 + CHECK(_pGB->QueryInterface(IID_IMediaPosition, (void**)&_pMediaPosition)); + ShowWindow(_hWnd, SW_SHOWNORMAL); _pMediaCont->Run(); @@ -403,19 +424,28 @@ // �����~ bool ECTrainerGUI::StopMovie() { + if (!IsPlaying()) return false; _pMediaCont->Stop(); + SAFE_RELEASE(_pMediaPosition); SAFE_RELEASE(_pMediaEvent); SAFE_RELEASE(_pMediaCont); SAFE_RELEASE(_pCGB2); SAFE_RELEASE(_pSource); SAFE_RELEASE(_pVMR9); SAFE_RELEASE(_pGB); - //pMediaPosition->Release(); ShowWindow(_hWnd, SW_HIDE); return true; } +// �Đ���Ԃ̊m�F +bool ECTrainerGUI::IsPlaying() { + if (!_pMediaCont) return false; + FILTER_STATE fs; + _pMediaCont->GetState(100, (OAFilterState*)&fs); + return (fs == State_Running); +} + // ����摜�o�b�t�@�ɉ摜��ݒ� void ECTrainerGUI::SetSceneBuffer(cv::Mat& img) { _SceneBufferScale = (float)IMAGE_WIDTH / img.cols; @@ -425,8 +455,8 @@ // �h���摜�o�b�t�@�ɉ摜��ݒ� void ECTrainerGUI::SetDispBuffer(cv::Mat& img) { + img.copyTo(_DispImage); _DispImageHeight = img.rows * IMAGE_WIDTH / img.cols; - cv::resize(img, _DispImage, cv::Size(IMAGE_WIDTH, _DispImageHeight)); cv::Mat buf(_DispBuffer.size(), CV_8UC3, cv::Scalar(0)); cv::Rect roiRect; diff --git a/ECTrainer2/ECTrainerGUI.h b/ECTrainer2/ECTrainerGUI.h index c06c886..2a424e9 100644 --- a/ECTrainer2/ECTrainerGUI.h +++ b/ECTrainer2/ECTrainerGUI.h @@ -35,7 +35,7 @@ int _SceneBufferHeight; float _SceneBufferScale; int _DispImageHeight; - int _PlayIndex; + //int _PlayIndex; RECT _DispRect; HWND _hWnd; @@ -47,10 +47,12 @@ ICaptureGraphBuilder2* _pCGB2; IMediaControl* _pMediaCont; IMediaEvent* _pMediaEvent; + IMediaPosition* _pMediaPosition; bool InitDx(HINSTANCE hInstance); bool PlayMovie(std::wstring movie); bool StopMovie(); + bool IsPlaying(); void GetDisplayInfo(); static BOOL CALLBACK MonitorEnumProc(HMONITOR hMon, HDC hdcMon, LPRECT lpMon, LPARAM dwDate); diff --git a/ECTrainer2/EyeTrack.cpp b/ECTrainer2/EyeTrack.cpp index 65b308b..4a0b598 100644 --- a/ECTrainer2/EyeTrack.cpp +++ b/ECTrainer2/EyeTrack.cpp @@ -4,7 +4,7 @@ #include "EyeTrack.h" #include "ECTrainer.h" #include "Marker.h" -#include "RingBuffer.h" +#include "MeanBuffer.h" #pragma comment(lib, "ws2_32.lib") #include @@ -13,9 +13,10 @@ EyeTrack::EyeTrack(ECTrainer* pEct, Marker* pMarker) : BaseProcess(pEct) , _pMarker(pMarker) - , _GazeV(-1, -1) + //, _GazeV(-1, -1) , _GazeI(-1.F, -1.F) , _KeepAliveSignal(false) + , _GazeV(4) { } @@ -53,13 +54,13 @@ cv::Point2f gp; cv::Size sceneSize = _pEct->GetSceneSize(); std::cout << "sceneSize: " << sceneSize.width << "," << sceneSize.height << std::endl; - RingBuffer gpCx(DATA_MEAN_SIZE), gpCy(DATA_MEAN_SIZE); + MeanBuffer gpCx(DATA_MEAN_SIZE), gpCy(DATA_MEAN_SIZE); //std::ofstream ofs; //ofs.open("datalog.txt"); while (!_KeepAliveSignal) Sleep(1); // �ŏ���KeepAlive���M�҂� - while (_pEct->IsRunning()) { + while (_pEct->IsAppRun()) { // �f�[�^��M memset(buf, '\0', BUFLEN); @@ -79,14 +80,17 @@ if (gp.x > 0 && gp.y > 0) { gpCx.Push(gp.x * sceneSize.width); gpCy.Push(gp.y * sceneSize.height); - _GazeV = cv::Point((int)gpCx.Mean(), (int)gpCy.Mean()); - _GazeI = _pMarker->ConvV2I(_GazeV); + _GazeV.Put(cv::Point2f(gpCx.Mean(), gpCy.Mean())); + //_GazeI = _pMarker->ConvV2I(_GazeV.Get()); //printf("_GazeV %d,%d ", _GazeV.x, _GazeV.y); //printf("_GazeI %f,%f ", _GazeI.x, _GazeI.y); } + else { + _GazeV.Put(gp); + } //printf("\n"); - gp = cv::Point(0, 0); + gp = cv::Point2f(-1.F, -1.F); lastGidx = gd.gidx; } if (gd.isGP && gd.s == 0) { @@ -116,13 +120,13 @@ strcpy_s(message, BUFLEN, KA_DATA_MSG.c_str()); int slen = sizeof(_socketAddr); - while (_pEct->IsRunning()) { + while (_pEct->IsAppRun()) { if (sendto(_socket, message, (int)strlen(message), 0, (struct sockaddr*)&_socketAddr, slen) == SOCKET_ERROR) { std::cerr << "Keep Alive Data Sent Error" << std::endl; } else { _KeepAliveSignal = true; } - for (int i = 0; i < KEEP_ALIVE_WAIT_COUNT && _pEct->IsRunning(); i++) Sleep(100); + for (int i = 0; i < KEEP_ALIVE_WAIT_COUNT && _pEct->IsAppRun(); i++) Sleep(100); } return true; diff --git a/ECTrainer2/EyeTrack.h b/ECTrainer2/EyeTrack.h index 834e085..e1695cf 100644 --- a/ECTrainer2/EyeTrack.h +++ b/ECTrainer2/EyeTrack.h @@ -2,6 +2,7 @@ #include "BaseProcess.h" #include "myOpenCV.h" +#include "RingBuffer.h" class Marker; @@ -12,7 +13,8 @@ static const int DATA_MEAN_SIZE = 20; // �����_�f�[�^�ړ����σT�C�Y Marker* _pMarker; - cv::Point _GazeV; // ����摜���̒����_���W + RingBuffer _GazeV; // ����摜���̒����_���W + //cv::Point _GazeV; // ����摜���̒����_���W cv::Point2f _GazeI; // �h���摜���̒����_���W�i���K�����W�n�j SOCKET _socket; struct sockaddr_in _socketAddr; @@ -23,8 +25,9 @@ bool Init(); bool MainLoop(); bool KeepAliveLoop(); - void SetGazeV(cv::Point gazeV) { _GazeV = gazeV; } - cv::Point GetGazeV() { return _GazeV; } + //void SetGazeV(cv::Point gazeV) { _GazeV = gazeV; } + bool IsNewGazeV() { return _GazeV.IsNew(); } + cv::Point2f GetGazeV() { return _GazeV.Get(); } cv::Point2f GetGazeI() { return _GazeI; } }; diff --git a/ECTrainer2/ImageProc.cpp b/ECTrainer2/ImageProc.cpp index f85c4f6..80bf468 100644 --- a/ECTrainer2/ImageProc.cpp +++ b/ECTrainer2/ImageProc.cpp @@ -11,8 +11,8 @@ // ���[�v bool ImageProc::MainLoop() { int lastRead = _Read; - while (_pEct->IsRunning()) { - while (_Read == lastRead && _pEct->IsRunning()) Sleep(1); + while (_pEct->IsAppRun()) { + while (_Read == lastRead && _pEct->IsAppRun()) Sleep(1); cv::Mat img = _Frames[_Read].clone(); lastRead = _Read; bool hOK = _pMarker->Detect(img); diff --git a/ECTrainer2/MeanBuffer.h b/ECTrainer2/MeanBuffer.h new file mode 100644 index 0000000..df36e8f --- /dev/null +++ b/ECTrainer2/MeanBuffer.h @@ -0,0 +1,35 @@ +#pragma once + +#include + +class MeanBuffer +{ + float *_buffer; + int _size; + int _next; + float _sum; + +public: + MeanBuffer(int size) { + _size = size; + _next = 0; + _sum = 0; + _buffer = new float[_size]; + for (int i = 0; i < _size; i++) _buffer[i] = 0; + } + + ~MeanBuffer() { + delete[] _buffer; + } + + void Push(float val) { + _sum -= _buffer[_next]; + _buffer[_next] = val; + _sum += val; + _next = (_next + 1) % _size; + } + + float Mean() { + return _sum / _size; + } +}; diff --git a/ECTrainer2/RingBuffer.h b/ECTrainer2/RingBuffer.h index 0201a42..81c0e1f 100644 --- a/ECTrainer2/RingBuffer.h +++ b/ECTrainer2/RingBuffer.h @@ -1,35 +1,68 @@ #pragma once -#include +#include + +template class RingBuffer { - float *_buffer; + T* _data; + std::mutex* _mtxData; + std::mutex _mtxIndex; int _size; - int _next; - float _sum; - + int _write; + int _read; + bool _update; public: - RingBuffer(int size) { - _size = size; - _next = 0; - _sum = 0; - _buffer = new float[_size]; - for (int i = 0; i < _size; i++) _buffer[i] = 0; - } - - ~RingBuffer() { - delete[] _buffer; - } - - void Push(float val) { - _sum -= _buffer[_next]; - _buffer[_next] = val; - _sum += val; - _next = (_next + 1) % _size; - } - - float Mean() { - return _sum / _size; - } + RingBuffer(int size); + ~RingBuffer(); + void Put(T value); + T Get(); + bool IsNew() { return _update; }; }; + +template +RingBuffer::RingBuffer(int size) + : _data(NULL) + , _write(0) + , _read(0) + , _update(false) +{ + _size = size; + _data = new T[_size]; + _mtxData = new std::mutex[_size]; +} + +template +RingBuffer::~RingBuffer() { + delete[] _data; + delete[] _mtxData; +} + +template +void RingBuffer::Put(T value) { + int write = _write; + _mtxData[write].lock(); + _data[write] = value; + _mtxData[write].unlock(); + + _mtxIndex.lock(); + _read = write; + _write = (write + 1) % _size; + _update = true; + _mtxIndex.unlock(); +} + +template +T RingBuffer::Get() { + int read = _read; + _mtxData[read].lock(); + T value = _data[read]; + _mtxData[read].unlock(); + + _mtxIndex.lock(); + _update = false; + _mtxIndex.unlock(); + + return value; +} diff --git a/ECTrainer2/SceneCamera.cpp b/ECTrainer2/SceneCamera.cpp index 899c9c5..6621d46 100644 --- a/ECTrainer2/SceneCamera.cpp +++ b/ECTrainer2/SceneCamera.cpp @@ -32,7 +32,7 @@ // ���[�v bool SceneCamera::MainLoop() { - while (_pEct->IsRunning()) { + while (_pEct->IsAppRun()) { // �V�[���B�e cv::Mat scene; _SceneCam >> scene; diff --git a/ECTrainer2/Stimulus.cpp b/ECTrainer2/Stimulus.cpp index f1cd002..253a934 100644 --- a/ECTrainer2/Stimulus.cpp +++ b/ECTrainer2/Stimulus.cpp @@ -41,7 +41,7 @@ // ���[�v bool Stimulus::MainLoop() { - while (_pEct->IsRunning()) { + while (_pEct->IsAppRun()) { if (_StimNo >= 0) { DWORD current = GetTickCount(); if ((float)(current - _StartTime) / 1000.F > _StimInfoSet[_StimNo].dulation) { diff --git a/ECTrainer2/TobiiREST.cpp b/ECTrainer2/TobiiREST.cpp index 0ff1218..976e2c2 100644 --- a/ECTrainer2/TobiiREST.cpp +++ b/ECTrainer2/TobiiREST.cpp @@ -32,7 +32,7 @@ // ���[�v bool TobiiREST::MainLoop() { - while (_pEct->IsRunning()) { + while (_pEct->IsAppRun()) { if (_Start) { _Start = false; if (StartCalibration()) { diff --git a/ECTrainer2/Worker.cpp b/ECTrainer2/Worker.cpp new file mode 100644 index 0000000..9d929ef --- /dev/null +++ b/ECTrainer2/Worker.cpp @@ -0,0 +1,33 @@ +#include "Worker.h" +#include "ECTrainer.h" +#include "EyeTrack.h" +#include "myOpenCV.h" +#include +#include + +Worker::Worker(ECTrainer* pEct) : BaseProcess(pEct) { + +} + +bool Worker::Init() { + return true; +} + +bool Worker::MainLoop() { + int64 start = cv::getTickCount(); + FILE* fp; + fopen_s(&fp, "log.txt", "w"); + printf("time,gazeVx,gazeVy\n"); + + while (_pEct->IsAppRun()) { + Sleep(0); + if (!_pEct->PEyeTrack()->IsNewGazeV()) continue; + + float elapse = (float)(cv::getTickCount() - start) * 1000.F / cv::getTickFrequency(); + cv::Point2f gazeV = (_pEct->GetGazeV()); + fprintf(fp, "%.1f, %.1f, %.1f\n", elapse, gazeV.x, gazeV.y); + } + + fclose(fp); + return true; +} diff --git a/ECTrainer2/Worker.h b/ECTrainer2/Worker.h new file mode 100644 index 0000000..da6b035 --- /dev/null +++ b/ECTrainer2/Worker.h @@ -0,0 +1,12 @@ +#pragma once + +#include "BaseProcess.h" + +class Worker : public BaseProcess +{ + +public: + Worker(ECTrainer* pEct); + bool Init(); + bool MainLoop(); +}; diff --git "a/Tobii\346\216\245\347\266\232.bat" "b/Tobii\346\216\245\347\266\232.bat" new file mode 100644 index 0000000..68b653b --- /dev/null +++ "b/Tobii\346\216\245\347\266\232.bat" @@ -0,0 +1,8 @@ + +netsh wlan connect name="TG02B-080200002641" + +timeout /t 1 > nul + +netsh wlan show interface + +pause