Skip to content

Commit

Permalink
Merge pull request #25 from AIRLegend/dev
Browse files Browse the repository at this point in the history
Bugfixing
  • Loading branch information
AIRLegend authored Aug 25, 2020
2 parents 6840017 + b67d24c commit 0078f28
Show file tree
Hide file tree
Showing 18 changed files with 150 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Client/Client.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\camera\CameraSettings.cpp" />
<ClCompile Include="src\camera\NullCamera.cpp" />
<ClCompile Include="src\camera\OCVCamera.cpp" />
<ClCompile Include="src\camera\CameraFactory.cpp" />
Expand All @@ -195,6 +196,7 @@
<QtUic Include="src\view\MainWindow.ui" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\camera\CameraSettings.h" />
<ClInclude Include="src\camera\Camera.h" />
<ClInclude Include="src\camera\NullCamera.h" />
<ClInclude Include="src\camera\OCVCamera.h" />
Expand Down
6 changes: 6 additions & 0 deletions Client/Client.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<ClCompile Include="src\tracker\TrackerFactory.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\camera\CameraSettings.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Include\ps3eye.h" />
Expand Down Expand Up @@ -61,6 +64,9 @@
<ClInclude Include="src\tracker\TrackerFactory.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\camera\CameraSettings.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<QtMoc Include="src\view\WindowMain.h" />
Expand Down
3 changes: 3 additions & 0 deletions Client/src/camera/Camera.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include "CameraSettings.h"

class Camera
{
Expand All @@ -11,6 +12,8 @@ class Camera
virtual void start_camera() = 0;
virtual void stop_camera() = 0;
virtual void get_frame(uint8_t* buffer) = 0;
virtual void set_settings(CameraSettings& settings) = 0;
virtual CameraSettings get_settings() = 0;

Camera(int width, int height, int fps) {
this->width = width;
Expand Down
7 changes: 6 additions & 1 deletion Client/src/camera/CameraFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "OCVCamera.h"
#include "NullCamera.h"

Camera* CameraFactory::buildCamera(int width, int height)
Camera* CameraFactory::buildCamera(int width, int height, int exposure, int gain)
{
Camera *camera = NULL;
bool error = false;
Expand Down Expand Up @@ -35,5 +35,10 @@ Camera* CameraFactory::buildCamera(int width, int height)
camera = new NullCamera;
}

CameraSettings cam_settings;
cam_settings.exposure = exposure;
cam_settings.gain = gain;
camera->set_settings(cam_settings);

return camera;
}
2 changes: 1 addition & 1 deletion Client/src/camera/CameraFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
class CameraFactory
{
public:
Camera* buildCamera(int width, int height);
Camera* buildCamera(int width, int height, int exposure=-1, int gain=-1);
};

17 changes: 17 additions & 0 deletions Client/src/camera/CameraSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "CameraSettings.h"


CameraSettings::CameraSettings()
{
exposure = -1;
gain = -1;
}

CameraSettings::CameraSettings(CameraSettings& settings)
{
exposure = settings.exposure;
gain = settings.gain;
}

CameraSettings::~CameraSettings()
{}
12 changes: 12 additions & 0 deletions Client/src/camera/CameraSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

struct CameraSettings
{
int exposure;
int gain;

CameraSettings();
CameraSettings(CameraSettings& settings);
~CameraSettings();
};

2 changes: 2 additions & 0 deletions Client/src/camera/NullCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ class NullCamera : public Camera
void start_camera() {};
void stop_camera() {};
void get_frame(uint8_t* buffer) {};
void set_settings(CameraSettings& settings) {};
CameraSettings get_settings() { return CameraSettings(); };
};
24 changes: 20 additions & 4 deletions Client/src/camera/OCVCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

OCVCamera::OCVCamera(int width, int height, int fps) :
Camera(width, height, fps),
size(width, height),
size(0, 0),
cap()
{
CV_BACKEND = cv::CAP_DSHOW;
Expand All @@ -15,7 +15,8 @@ OCVCamera::OCVCamera(int width, int height, int fps) :
throw std::runtime_error("No compatible camera found.");
}
is_valid = true;
cap.set(cv::CAP_PROP_BUFFERSIZE, 1);

w_scale = (float)width/(float)cam_native_width;
}

OCVCamera::~OCVCamera()
Expand All @@ -30,8 +31,10 @@ bool OCVCamera::is_camera_available()
cap.open(0, CV_BACKEND);
available = cap.isOpened();
if (available)
{
cam_native_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
cap.release();

}
return available;
}

Expand All @@ -53,9 +56,22 @@ void OCVCamera::get_frame(uint8_t* buffer)
{
cv::Mat frame;
cap.read(frame);
cv::resize(frame, frame, size);
//Scale maintaining aspect ratio. If distorted, the model will get confused.
//TODO: Maybe cropping (width,height) section from the center is better.
//cv::resize(frame, frame, size, w_scale, w_scale);
cv::resize(frame, frame, size, w_scale, w_scale);
cv::flip(frame, frame, 1);
for (int i = 0; i < frame.cols * frame.rows * 3; i++)
buffer[i] = frame.data[i];

}

void OCVCamera::set_settings(CameraSettings& settings)
{
//TODO
}

CameraSettings OCVCamera::get_settings()
{
return CameraSettings();
}
4 changes: 4 additions & 0 deletions Client/src/camera/OCVCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class OCVCamera : public Camera
private:
cv::VideoCapture cap;
cv::Size size;
float w_scale;
int cam_native_width;
int CV_BACKEND;

bool is_camera_available();
Expand All @@ -17,5 +19,7 @@ class OCVCamera : public Camera
void start_camera();
void stop_camera();
void get_frame(uint8_t* buffer);
void set_settings(CameraSettings& settings);
CameraSettings get_settings();
};

31 changes: 27 additions & 4 deletions Client/src/camera/Ps3Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
#include <iostream>


Ps3Camera::Ps3Camera(int width, int height, int fps):
Ps3Camera::Ps3Camera(int width, int height, int fps) :
Camera(width, height, fps),
ctx(width, height, fps)
ctx(width, height, fps),
setting()
{
if (!this->ctx.hasDevices())
{
std::cout << "SOME ERROR BUILDING" << std::endl;
throw std::runtime_error("No PS3 Camera found.");
}
ctx.eye->setFlip(true);
this->is_valid = true;

setting.exposure = 140;
setting.gain = 1;
}

Ps3Camera::~Ps3Camera()
Expand All @@ -35,4 +38,24 @@ void Ps3Camera::stop_camera()
void Ps3Camera::get_frame(uint8_t *buffer)
{
this->ctx.eye->getFrame(buffer);
}
}

void Ps3Camera::set_settings(CameraSettings& settings)
{
if (settings.exposure >= 0)
{
setting.exposure = min(settings.exposure, 254);
ctx.eye->setExposure(setting.exposure);
}

if (settings.gain >= 0)
{
setting.gain = min(settings.gain, 60);
ctx.eye->setGain(setting.gain);
}
}

CameraSettings Ps3Camera::get_settings()
{
return CameraSettings(setting);
}
3 changes: 3 additions & 0 deletions Client/src/camera/Ps3Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ class Ps3Camera : public Camera

private:
ps3eye_context ctx;
CameraSettings setting;

public:
Ps3Camera(int width = 640, int height = 480, int fps = 30);
~Ps3Camera();
void start_camera();
void stop_camera();
void get_frame(uint8_t* buffer);
void set_settings(CameraSettings& settings);
CameraSettings get_settings();
};

6 changes: 6 additions & 0 deletions Client/src/model/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ ConfigData ConfigData::getGenericConfig()
conf.video_fps = 30;
conf.use_landmark_stab = true;
conf.x, conf.y, conf.z, conf.pitch, conf.yaw, conf.roll = 0;
conf.cam_exposure = -1;
conf.cam_gain = -1;
return conf;
}

Expand Down Expand Up @@ -48,6 +50,8 @@ void ConfigMgr::updateConfig(const ConfigData& data)
conf.setValue("video_height", data.video_height);
conf.setValue("stabilize_landmarks", data.use_landmark_stab);
conf.setValue("fps", data.video_fps);
conf.setValue("cam_exposure", data.cam_exposure);
conf.setValue("cam_gain", data.cam_gain);
}

ConfigData ConfigMgr::getConfig()
Expand All @@ -64,6 +68,8 @@ ConfigData ConfigMgr::getConfig()
c.video_width = conf.value("video_width", 640).toInt();
c.video_height = conf.value("video_height", 480).toInt();
c.video_fps = conf.value("fps", 30).toInt();
c.cam_exposure= conf.value("cam_exposure", -1).toInt();
c.cam_gain = conf.value("cam_gain", -1).toInt();
return c;
}

3 changes: 3 additions & 0 deletions Client/src/model/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ struct ConfigData

float x, y, z, yaw, pitch, roll;

int cam_exposure;
int cam_gain;

std::vector<std::string> model_names;
int selected_model;

Expand Down
10 changes: 2 additions & 8 deletions Client/src/presenter/presenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@ Presenter::Presenter(IView& view, TrackerFactory* t_factory, ConfigMgr* conf_mgr

this->filter = nullptr;


this->tracker_factory = t_factory;

// Init available model names to show in the GUI
this->tracker_factory->get_model_names(state.model_names);

//this->filter = new MAFilter(3, 66*2);
//this->filter = new EAFilter(66 * 2);
//this->filter = nullptr;
// Setup a filter to stabilize the recognized facial landmarks if needed.
update_stabilizer(state);


CameraFactory camfactory;
camera = camfactory.buildCamera(state.video_width, state.video_height);
camera = camfactory.buildCamera(state.video_width, state.video_height, state.cam_exposure, state.cam_gain);

if (!camera->is_valid)
{
Expand All @@ -45,13 +43,9 @@ Presenter::Presenter(IView& view, TrackerFactory* t_factory, ConfigMgr* conf_mgr
int port = state.port;
init_sender(ip_str, port);


// Build tracker
init_tracker(state.selected_model);

// Get avilable model types
this->tracker_factory->get_model_names(state.model_names);

}

// Check if there was a problem initing tracker
Expand Down
17 changes: 17 additions & 0 deletions Doc/USER_GUIDE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# User guide: Setup, common problems and tips

## Video version

Thanks to Sims Smith, for making a tutorial on how to setup this software to work with XPlane and Flight Simulator 2020. Although it's made for AITrack v0.4, the core process is almost the same.

Although this tutorial covers pretty much everything, it's worth pointing out that:


<center>

[<img src="https://img.youtube.com/vi/LPlahUVPx4o/hqdefault.jpg" width="50%">](https://youtu.be/LPlahUVPx4o)

</center>

1) You don't need to configure "Use remote client" anymore if you're running Opentrack in your local machine.
2) You should take you time for [tweaking your curves in Opentrack](https://www.youtube.com/watch?v=u0TBI7SoGkc) to your preferences.
3) Experiment with Opentrack's built in filters. Acella it's the recommended one at the moment. Configure it's smoothing parameter to reduce camera shaking.

## Configuring opentrack and AiTrack

AiTrack sends data over UDP to opentrack, which in turn, sends it to your game, so both of them need to be running.
Expand Down
10 changes: 10 additions & 0 deletions PS3Driver/src/ps3eye.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ USBMgr::USBMgr()
active_camera_count = 0;
libusb_init(&usb_context);
libusb_set_debug(usb_context, 1);

libusb_set_option(usb_context,
LIBUSB_OPTION_LOG_LEVEL,
LIBUSB_LOG_LEVEL_INFO);
}

USBMgr::~USBMgr()
Expand Down Expand Up @@ -392,9 +396,14 @@ int USBMgr::listDevices( std::vector<PS3EYECam::PS3EYERef>& list )
libusb_device *dev;
libusb_device **devs;
libusb_device_handle *devhandle;

int i = 0;
int cnt;

int res = libusb_init(&usb_context);
if (res < 0)
return 0;

cnt = (int)libusb_get_device_list(usb_context, &devs);

if (cnt < 0) {
Expand Down Expand Up @@ -986,6 +995,7 @@ static void LIBUSB_CALL transfer_completed_callback(struct libusb_transfer *xfr)
bool PS3EYECam::devicesEnumerated = false;
std::vector<PS3EYECam::PS3EYERef> PS3EYECam::devices;


const std::vector<PS3EYECam::PS3EYERef>& PS3EYECam::getDevices( bool forceRefresh )
{
if( devicesEnumerated && ( ! forceRefresh ) )
Expand Down
Loading

0 comments on commit 0078f28

Please sign in to comment.