...
 
Commits (2)
......@@ -30,6 +30,9 @@
#include "ScreenCameraInfo.h"
#include "KalmanFilter.h"
#include "MousePositionProvider.h"
#include "Recorder.h"
#include "RecordManager.h"
#define debugcout(x) std::cout<<#x<<x<<std::endl
......
#include "KalmanFilter.h"
#include <opencv2/opencv.hpp>
#include "Kalmanfilter.h"
KalmanFilter::KalmanFilter(int number, int dtype) {
int depth = CV_MAT_DEPTH(dtype);
......
#pragma once
#include <vector>
#include <CoreOpenCVExtensions/CoreOpenCVExtensions.h>
#include <opencv2/opencv.hpp>
class KalmanFilter
{
......
#include <atomic>
#include "MousePositionProvider.h"
//#include <tobii/tobii_streams.h>
#include <assert.h>
#include <CameraProfileDelegate/CameraProfileTask.h>
using namespace cv;
Point currentMousePosition = Point(-1,-1);
/*
Point currentGazePosition = Point(-1, -1);
bool gazeValid = false;
void gaze_point_callback(tobii_gaze_point_t const* gaze_point, void* user_data)
{
if (gaze_point->validity == TOBII_VALIDITY_VALID) {
currentGazePosition.x = gaze_point->position_xy[0] * ScreenCameraInfo::getCurrentDiviceInfo().screenPhysicalResolution.width;
currentGazePosition.y = gaze_point->position_xy[1] * ScreenCameraInfo::getCurrentDiviceInfo().screenPhysicalResolution.height;
gazeValid = true;
return;
}
gazeValid = false;
}
tobii_api_t* api;
tobii_error_t error_tobii;
tobii_device_t* device;
*/
void onmouse(int event, int x, int y, int flags, void* param) {
if (event == CV_EVENT_MOUSEMOVE) {
currentMousePosition = Point(x, y);
}
}
void setWindowNameOfMouse(string title) {
setMouseCallback(title,onmouse);
}
Point getMousePosition() {
return currentMousePosition;
}
/*
Point getGazePosition() {
error_tobii = tobii_api_create(&api, NULL, NULL);
assert(error_tobii == TOBII_ERROR_NO_ERROR);
error_tobii = tobii_device_create(api, NULL, &device);
assert(error_tobii == TOBII_ERROR_NO_ERROR);
error_tobii = tobii_gaze_point_subscribe(device, gaze_point_callback, 0);
assert(error_tobii == TOBII_ERROR_NO_ERROR);
while(!gazeValid){
error_tobii = tobii_wait_for_callbacks(device);
assert(error_tobii == TOBII_ERROR_NO_ERROR || error_tobii == TOBII_ERROR_TIMED_OUT);
error_tobii = tobii_process_callbacks(device);
assert(error_tobii == TOBII_ERROR_NO_ERROR);
}
gazeValid = false;
error_tobii = tobii_gaze_point_unsubscribe(device);
assert(error_tobii == TOBII_ERROR_NO_ERROR);
error_tobii = tobii_device_destroy(device);
assert(error_tobii == TOBII_ERROR_NO_ERROR);
error_tobii = tobii_api_destroy(api);
assert(error_tobii == TOBII_ERROR_NO_ERROR);
api = NULL;
device = NULL;
return currentGazePosition;
}
*/
\ No newline at end of file
#pragma once
#include <opencv2/opencv.hpp>
using namespace std;
cv::Point getMousePosition();
void setWindowNameOfMouse(string);
//cv::Point getGazePosition();
\ No newline at end of file
#pragma once
#include <opencv2/opencv.hpp>
#include <thread>
#include <string>
#include <vector>
using namespace std;
class RecordManager {
private:
vector<string> recordName;
vector<std::function<string()>> recordGetter;
public:
void addRecorder(string name, std::function<string(void)> getter) {
recordName.push_back(name);
recordGetter.push_back(getter);
}
vector<string> getStatus() {
vector<string> result;
getStatus(result);
return result;
}
void getStatus(vector<string> & target) {
for (int i = 0; i < recordName.size(); i++) {
target.push_back(recordName[i] + ":" + recordGetter[i]());
}
}
void drawOnTopRight(cv::Mat & canvas) {
vector<string> items;
getStatus(items);
int offsetLeft = 0;
int offsetTop = 0;
double fontScale = 0.5;
int thickness = 1;
for (string i : items) {
cv::Size stringSize = getTextSize(cv::String(i), CV_FONT_HERSHEY_SIMPLEX, fontScale, thickness, 0);
offsetLeft = 5;
offsetTop += stringSize.height+5;
putText(canvas, cv::String(i), cv::Point(offsetLeft, offsetTop), CV_FONT_HERSHEY_SIMPLEX, fontScale, cv::Scalar(0, 0, 0), thickness );
}
}
};
\ No newline at end of file
#pragma once
#include <opencv2/opencv.hpp>
#include <string>
#include <list>
#include <sstream>
using namespace std;
template <class T>
class Recorder
{
public:
virtual void push(T value)=0;
virtual T get() = 0;
virtual string toString()=0;
Recorder() {};
virtual ~Recorder() {};
};
template <class T>
class NormalRecorder : public Recorder<T>
{
private:
T currentValue;
public:
void push(T value) {
currentValue = value;
}
T get() {
return currentValue;
}
string toString() {
stringstream buffer;
buffer << currentValue;
return buffer.str();
}
};
template <class T>
class VarianceRecorder : public Recorder<T>
{
private:
list<T> currentValues;
T variance;
int listSize = 5;
bool caculated = false;
void caculate() {
T averageValue = 0;
for (T i : currentValues) {
averageValue = averageValue + i / double(currentValues.size());
}
T tmpVariance = 0;
for (T i : currentValues) {
tmpVariance = tmpVariance + (i - averageValue) * (i - averageValue) / double(currentValues.size());
}
variance = tmpVariance;
caculated = true;
}
public:
VarianceRecorder() {}
int getListSize() {
return listSize;
}
void setListSize(int value) {
listSize = value > 0 ? value : 0;
while (currentValues.size() > listSize) {
currentValues.pop_front();
}
caculated = false;
}
void push(T value) {
currentValues.push_back(value);
while (currentValues.size() > listSize) {
currentValues.pop_front();
}
caculated = false;
}
T get(){
if (caculated) {
return variance;
}
else {
caculate();
return variance;
}
}
string toString() {
get();
stringstream buffer;
buffer << variance;
return buffer.str();
}
};
template <>
class VarianceRecorder<cv::Vec2d> : public Recorder<cv::Vec2d>
{
private:
list<cv::Vec2d> currentValues;
double variance;
int listSize = 5;
bool caculated = false;
bool caculateAsVector = false;
void caculate() {
if (caculateAsVector) {
double averageDistance = 0;
for (cv::Vec2d i : currentValues) {
averageDistance += sqrt(i[0] * i[0] + i[1] * i[1]) / double(currentValues.size());
}
double tmpVariance = 0;
for (cv::Vec2d i : currentValues) {
tmpVariance += (sqrt(i[0] * i[0] + i[1] * i[1]) - averageDistance) * (sqrt(i[0] * i[0] + i[1] * i[1]) - averageDistance) / double(currentValues.size());
}
variance = tmpVariance;
}
else {
cv::Vec2d averageValue = 0;
for (cv::Vec2d i : currentValues) {
averageValue = averageValue + i / double(currentValues.size());
}
double averageDistance = 0;
for (cv::Vec2d vec : currentValues) {
cv::Vec2d i = vec - averageValue;
averageDistance += sqrt(i[0] * i[0] + i[1] * i[1]) / double(currentValues.size());
}
double tmpVariance = 0;
for (cv::Vec2d vec : currentValues) {
cv::Vec2d i = vec - averageValue;
tmpVariance += (sqrt(i[0] * i[0] + i[1] * i[1]) - averageDistance) * (sqrt(i[0] * i[0] + i[1] * i[1]) - averageDistance) / double(currentValues.size());
}
variance = tmpVariance;
}
caculated = true;
}
public:
VarianceRecorder() {}
VarianceRecorder(int size) :Recorder(), listSize(size) {}
int getListSize() {
return listSize;
}
void setListSize(int value) {
listSize = value > 0 ? value : 0;
while (currentValues.size() > listSize) {
currentValues.pop_front();
}
caculated = false;
}
void push(cv::Vec2d value) {
currentValues.push_back(value);
while (currentValues.size() > listSize) {
currentValues.pop_front();
}
caculated = false;
}
cv::Vec2d get() {
if (caculated) {
return variance;
}
else {
caculate();
return variance;
}
}
string toString() {
get();
stringstream buffer;
buffer << variance;
return buffer.str();
}
void caculatingAsPoint() {
caculateAsVector = false;
}
void caculatingAsVector() {
caculateAsVector = true;
}
};
\ No newline at end of file