Skip to content

Commit

Permalink
Merge pull request #34 from AIRLegend/dev
Browse files Browse the repository at this point in the history
Refactor, Bugfixing and new features
  • Loading branch information
AIRLegend authored Aug 29, 2020
2 parents 7362e27 + 11c235b commit ac39185
Show file tree
Hide file tree
Showing 35 changed files with 991 additions and 532 deletions.
12 changes: 2 additions & 10 deletions AITracker/src/data.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "data.h"

FaceData::FaceData()
FaceData::FaceData():
landmark_coords{0}
{
rotation = new double[3]{ 0,0,0 };
translation = new double[3]{ 0,0,0 };
Expand All @@ -22,15 +23,6 @@ std::string FaceData::to_string()
std::to_string(this->translation[1]) + ", Z: " +
std::to_string(this->translation[2]) ;


/*datastring +=
std::string("\n\Crop coords: ") +
std::to_string(this->face_coords[0]) + ", " +
std::to_string(this->face_coords[1]) + ", " +
std::to_string(this->face_coords[2]) + ", " +
std::to_string(this->face_coords[3]);
*/

return datastring;
}

Expand Down
4 changes: 4 additions & 0 deletions AITracker/src/filters.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "filters.h"

#include <cstring>

MAFilter::MAFilter(int steps, int array_size)
{
this->n_steps = steps;
Expand Down Expand Up @@ -41,6 +43,8 @@ EAFilter::EAFilter(int array_size)
{
this->array_size = array_size;
this->last_value = new float[array_size];
//If the last value is not 0, the first readings could bounce
std::memset(last_value, 0, array_size * sizeof(float));
}

EAFilter::~EAFilter()
Expand Down
62 changes: 24 additions & 38 deletions AITracker/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,31 @@



Tracker::Tracker(PositionSolver* solver, std::wstring& detection_model_path, std::wstring& landmark_model_path):
improc()
Tracker::Tracker(std::unique_ptr<PositionSolver>&& solver, std::wstring& detection_model_path, std::wstring& landmark_model_path):
improc(),
memory_info(allocator.GetInfo()),
enviro(std::make_unique<Ort::Env>(ORT_LOGGING_LEVEL_WARNING, "env")),
detection_input_node_names{ "input" },
detection_output_node_names{ "output", "maxpool" },
landmarks_input_node_names{ "input" },
landmarks_output_node_names{ "output" }
{

this->solver = solver;


session_options = new Ort::SessionOptions();
session_options->SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
session_options->SetInterOpNumThreads(1);
session_options->SetInterOpNumThreads(1);
allocator = new Ort::AllocatorWithDefaultOptions();
memory_info = (Ort::MemoryInfo*)allocator->GetInfo();
enviro = new Ort::Env(ORT_LOGGING_LEVEL_WARNING, "env");

enviro->DisableTelemetryEvents();

this->solver = std::move(solver);

session = new Ort::Session(*enviro, detection_model_path.data(), *session_options);
session_lm = new Ort::Session(*enviro, landmark_model_path.data(), *session_options);
auto session_options = Ort::SessionOptions();
session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
session_options.SetInterOpNumThreads(1);
session_options.SetIntraOpNumThreads(1);

tensor_input_size = tensor_input_dims[1] * tensor_input_dims[2] * tensor_input_dims[3];
enviro->DisableTelemetryEvents();

detection_input_node_names = {"input"};
detection_output_node_names = {"output", "maxpool"};
landmarks_input_node_names = {"input"};
landmarks_output_node_names = {"output"};
}
session = std::make_unique<Ort::Session>(*enviro, detection_model_path.data(), session_options);
session_lm = std::make_unique<Ort::Session>(*enviro, landmark_model_path.data(), session_options);

Tracker::~Tracker()
{
delete this->session_options;
delete this->enviro;
delete this->session;
delete this->session_lm;
delete this->solver;
tensor_input_size = tensor_input_dims[1] * tensor_input_dims[2] * tensor_input_dims[3];
}

void Tracker::predict(cv::Mat& image, FaceData& face_data, IFilter* filter)
void Tracker::predict(cv::Mat& image, FaceData& face_data, const std::unique_ptr<IFilter>& filter)
{
cv::Mat img_copy = image.clone();
img_copy.convertTo(img_copy, CV_32F);
Expand Down Expand Up @@ -95,7 +81,7 @@ void Tracker::detect_face(const cv::Mat& image, FaceData& face_data)
improc.normalize(resized);
improc.transpose((float*)resized.data, buffer_data);

Ort::Value input_tensor = Ort::Value::CreateTensor<float>(*memory_info, buffer_data, tensor_input_size, tensor_input_dims, 4);
Ort::Value input_tensor = Ort::Value::CreateTensor<float>(memory_info, buffer_data, tensor_input_size, tensor_input_dims, 4);


auto output_tensors = session->Run(Ort::RunOptions{ nullptr },
Expand All @@ -107,7 +93,7 @@ void Tracker::detect_face(const cv::Mat& image, FaceData& face_data)

cv::Mat out(4, tensor_detection_output_dims, CV_32F, output_arr);
cv::Mat maxpool(4, tensor_detection_output_dims, CV_32F, maxpool_arr);


cv::Mat first(56, 56, CV_32F, out.ptr<float>(0,0));
cv::Mat second(56, 56, CV_32F, out.ptr<float>(0,1));
Expand All @@ -123,7 +109,7 @@ void Tracker::detect_face(const cv::Mat& image, FaceData& face_data)


face_data.face_detected = c > .6 ? true : false;

if (face_data.face_detected)
{
float face[] = { x - r, y - r, 2 * r, 2 * r };
Expand All @@ -144,7 +130,7 @@ void Tracker::detect_face(const cv::Mat& image, FaceData& face_data)
face_data.face_coords[2] = face[2];
face_data.face_coords[3] = face[3];
}

}


Expand All @@ -156,10 +142,10 @@ void Tracker::detect_landmarks(const cv::Mat& image, int x0, int y0, float scale
improc.normalize(resized);
improc.transpose((float*)resized.data, buffer_data);

Ort::Value input_tensor = Ort::Value::CreateTensor<float>(*memory_info, buffer_data, tensor_input_size, tensor_input_dims, 4);
Ort::Value input_tensor = Ort::Value::CreateTensor<float>(memory_info, buffer_data, tensor_input_size, tensor_input_dims, 4);


auto output_tensors = session_lm->Run(Ort::RunOptions{ nullptr },
auto output_tensors = session_lm->Run(Ort::RunOptions{ nullptr },
landmarks_input_node_names.data(), &input_tensor, 1, landmarks_output_node_names.data(), 1);

float* output_arr = output_tensors[0].GetTensorMutableData<float>();
Expand Down
19 changes: 9 additions & 10 deletions AITracker/src/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@ class Tracker
{

public:
PositionSolver* solver;
std::unique_ptr<PositionSolver> solver;

Tracker(PositionSolver* solver, std::wstring& detection_model_path, std::wstring& landmark_model_path);
~Tracker();
void predict(cv::Mat& image, FaceData& face_data, IFilter *filter=nullptr);
Tracker(std::unique_ptr<PositionSolver>&& solver, std::wstring& detection_model_path, std::wstring& landmark_model_path);

void predict(cv::Mat& image, FaceData& face_data, const std::unique_ptr<IFilter>& filter = {});

private:
ImageProcessor improc;
Ort::AllocatorWithDefaultOptions allocator = {};
const OrtMemoryInfo* memory_info;

std::string detection_model_path;
Ort::Env* enviro;
Ort::SessionOptions* session_options;
Ort::Session* session;
Ort::Session* session_lm;
Ort::AllocatorWithDefaultOptions* allocator;
Ort::MemoryInfo* memory_info;
std::unique_ptr<Ort::Env> enviro;
std::unique_ptr<Ort::Session> session;
std::unique_ptr<Ort::Session> session_lm;

std::vector<const char*> detection_input_node_names;
std::vector<const char*> detection_output_node_names;
Expand Down
19 changes: 10 additions & 9 deletions Client/Client.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<AdditionalIncludeDirectories>$(Qt_INCLUDEPATH_);$(SolutionDir)Dependencies\libusb\include\libusb-1.0;$(SolutionDir)Dependencies\OpenCV\include\;$(SolutionDir)Dependencies\onnxruntime\include\;$(SolutionDir)AITracker\src\;$(ProjectDir)Include;$(QTDIR)\include;$(QTDIR)\include\QtUiTools;$(QTDIR)\include\QtWidgets;$(QTDIR)\include\QtQuick;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtQml;$(QTDIR)\include\QtNetwork;$(QTDIR)\include\QtCore;$(QTDIR)\mkspecs\win32-msvc;$(Platform)\$(Configuration)\uic;$(Platform)\$(Configuration)\moc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(Qt_INCLUDEPATH_);$(SolutionDir)Dependencies\libusb\include\libusb-1.0;$(SolutionDir)Dependencies\OpenCV\include\;$(SolutionDir)Dependencies\onnxruntime\include\;$(SolutionDir)AITracker\src\;$(ProjectDir)Include;$(QTDIR)\include;$(QTDIR)\include\QtUiTools;$(QTDIR)\include\QtWidgets;$(QTDIR)\include\QtQuick;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtQml;$(QTDIR)\include\QtNetwork;$(QTDIR)\include\QtCore;$(QTDIR)\mkspecs\win32-msvc;$(Platform)\$(Configuration)\uic;$(Platform)\$(Configuration)\moc;.\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(Qt_LIBPATH_);$(SolutionDir)Dependencies\libusb\MS64\static;$(SolutionDir)Dependencies\OpenCV\lib;$(SolutionDir)Dependencies\onnxruntime\lib\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
Expand All @@ -86,7 +86,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debugg|x64'">
<ClCompile>
<AdditionalIncludeDirectories>$(Qt_INCLUDEPATH_);$(ProjectDir)Dependencies\OpenCV\include\;$(SolutionDir)Dependencies\libusb\include\libusb-1.0;$(SolutionDir)Dependencies\OpenCV\include\;$(SolutionDir)Dependencies\onnxruntime\include\;$(SolutionDir)AITracker\src\;$(ProjectDir)Include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(Qt_INCLUDEPATH_);$(ProjectDir)Dependencies\OpenCV\include\;$(SolutionDir)Dependencies\libusb\include\libusb-1.0;$(SolutionDir)Dependencies\OpenCV\include\;$(SolutionDir)Dependencies\onnxruntime\include\;$(SolutionDir)AITracker\src\;$(ProjectDir)Include;.\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(Qt_LIBPATH_);C:\Users\Alvaro\source\repos\Camera\Dependencies\libusb\MS64\static;C:\Users\Alvaro\source\repos\Camera\Dependencies\OpenCV\lib;$(SolutionDir)Dependencies\onnxruntime\lib\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
Expand All @@ -105,7 +105,7 @@
<AdditionalDependencies>$(Qt_LIBS_);opencv_world430d.lib;onnxruntime.lib;Ws2_32.lib;legacy_stdio_definitions.lib;libusb-1.0.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<ClCompile>
<AdditionalIncludeDirectories>$(Qt_INCLUDEPATH_);$(SolutionDir)Dependencies\libusb\include\libusb-1.0;$(SolutionDir)Dependencies\OpenCV\include\;$(SolutionDir)Dependencies\onnxruntime\include\;$(SolutionDir)PS3Driver\include\;$(SolutionDir)AITracker\src\;$(ProjectDir)Include;$(QTDIR)\include;$(QTDIR)\include\QtUiTools;$(QTDIR)\include\QtWidgets;$(QTDIR)\include\QtQuick;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtQml;$(QTDIR)\include\QtNetwork;$(QTDIR)\include\QtCore;$(QTDIR)\mkspecs\win32-msvc;$(Platform)\$(Configuration)\uic;$(Platform)\$(Configuration)\moc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(Qt_INCLUDEPATH_);$(SolutionDir)Dependencies\libusb\include\libusb-1.0;$(SolutionDir)Dependencies\OpenCV\include\;$(SolutionDir)Dependencies\onnxruntime\include\;$(SolutionDir)PS3Driver\include\;$(SolutionDir)AITracker\src\;$(ProjectDir)Include;$(QTDIR)\include;$(QTDIR)\include\QtUiTools;$(QTDIR)\include\QtWidgets;$(QTDIR)\include\QtQuick;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtQml;$(QTDIR)\include\QtNetwork;$(QTDIR)\include\QtCore;$(QTDIR)\mkspecs\win32-msvc;$(Platform)\$(Configuration)\uic;$(Platform)\$(Configuration)\moc;.\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<ProjectReference>
<UseLibraryDependencyInputs>true</UseLibraryDependencyInputs>
Expand Down Expand Up @@ -178,23 +178,20 @@
<ClCompile Include="src\camera\OCVCamera.cpp" />
<ClCompile Include="src\camera\CameraFactory.cpp" />
<ClCompile Include="src\model\Config.cpp" />
<ClCompile Include="src\model\IPResolver.cpp" />
<ClCompile Include="src\camera\Ps3Camera.cpp" />
<ClCompile Include="src\presenter\presenter.cpp" />
<ClCompile Include="src\Main.cpp" />
<ClCompile Include="src\model\UDPSender.cpp" />
<ClCompile Include="src\view\ConfigWindow.cpp" />
<ClCompile Include="src\view\WindowMain.cpp" />
<ClCompile Include="src\tracker\TrackerFactory.cpp" />
<ClCompile Include="src\tracker\TrackerWrapper.cpp" />
<QtRcc Include="res\Resource.qrc" />
<QtRcc Include="src\view\qml.qrc" />
<CustomBuild Include="src\view\Resource.qrc" />
</ItemGroup>
<ItemGroup>
<QtMoc Include="src\view\WindowMain.h" />
</ItemGroup>
<ItemGroup>
<QtUic Include="src\view\MainWindow.ui" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\camera\CameraSettings.h" />
<ClInclude Include="src\camera\Camera.h" />
Expand All @@ -204,12 +201,12 @@
<ClInclude Include="Include\ps3eye.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="src\model\Config.h" />
<ClInclude Include="src\model\IPResolver.h" />
<ClInclude Include="src\camera\Ps3Camera.h" />
<ClInclude Include="src\presenter\i_presenter.h" />
<ClInclude Include="src\presenter\presenter.h" />
<ClInclude Include="src\model\UDPSender.h" />
<ClInclude Include="src\tracker\ITrackerWrapper.h" />
<QtMoc Include="src\view\ConfigWindow.h" />
<ClInclude Include="src\view\i_view.h" />
<ClInclude Include="src\tracker\TrackerFactory.h" />
<ClInclude Include="src\tracker\TrackerWrapper.h" />
Expand All @@ -229,6 +226,10 @@
<Image Include="res\logo.png" />
<Image Include="res\logo_256px.ico" />
</ItemGroup>
<ItemGroup>
<QtUic Include="src\view\ConfigWindow.ui" />
<QtUic Include="src\view\MainWindow.ui" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Condition="Exists('$(SolutionDir)\QtMsBuild\qt.targets')">
<Import Project="$(SolutionDir)\QtMsBuild\qt.targets" />
Expand Down
30 changes: 24 additions & 6 deletions Client/Client.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="src\model\IPResolver.cpp" />
<ClCompile Include="src\camera\Ps3Camera.cpp" />
<ClCompile Include="src\presenter\presenter.cpp" />
<ClCompile Include="src\Main.cpp" />
Expand All @@ -28,10 +27,12 @@
<ClCompile Include="src\camera\CameraSettings.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\view\ConfigWindow.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Include\ps3eye.h" />
<ClInclude Include="src\model\IPResolver.h" />
<ClInclude Include="src\camera\Ps3Camera.h" />
<ClInclude Include="src\presenter\i_presenter.h" />
<ClInclude Include="src\presenter\presenter.h" />
Expand Down Expand Up @@ -70,16 +71,15 @@
</ItemGroup>
<ItemGroup>
<QtMoc Include="src\view\WindowMain.h" />
<QtMoc Include="src\view\ConfigWindow.h">
<Filter>Header Files</Filter>
</QtMoc>
</ItemGroup>
<ItemGroup>
<QtRcc Include="src\view\qml.qrc" />
<QtRcc Include="res\Resource.qrc">
<Filter>Resource Files</Filter>
</QtRcc>
</ItemGroup>
<ItemGroup>
<QtUic Include="src\view\MainWindow.ui" />
</ItemGroup>
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
Expand All @@ -96,6 +96,11 @@
<Extensions>qrc;*</Extensions>
<ParseFiles>false</ParseFiles>
</Filter>
<Filter Include="Form Files">
<UniqueIdentifier>{99349809-55BA-4b9d-BF79-8FDBB0286EB3}</UniqueIdentifier>
<Extensions>ui</Extensions>
<ParseFiles>true</ParseFiles>
</Filter>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Client.rc" />
Expand All @@ -104,4 +109,17 @@
<Image Include="res\logo.png" />
<Image Include="res\logo_256px.ico" />
</ItemGroup>
<ItemGroup>
<QtUic Include="src\view\MainWindow.ui">
<Filter>Form Files</Filter>
</QtUic>
<QtUic Include="src\view\ConfigWindow.ui">
<Filter>Form Files</Filter>
</QtUic>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="src\view\Resource.qrc">
<Filter>Resource Files</Filter>
</CustomBuild>
</ItemGroup>
</Project>
11 changes: 6 additions & 5 deletions Client/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
#include "tracker/TrackerFactory.h"



int main(int argc, char *argv[])
{

SetEnvironmentVariable(LPWSTR("OMP_NUM_THREADS"), LPWSTR("1"));
omp_set_num_threads(1); // Disable ONNX paralelization so we dont steal all cpu cores.
omp_set_dynamic(0);
Expand All @@ -27,10 +28,10 @@ int main(int argc, char *argv[])
WindowMain w;
w.show();

ConfigMgr conf_mgr("./prefs.ini");
TrackerFactory t_factory("./models/");
Presenter p((IView&)w, &t_factory, (ConfigMgr*)&conf_mgr);
auto conf_mgr = std::make_unique<ConfigMgr>("./prefs.ini");
auto t_factory = std::make_unique<TrackerFactory>("./models/");

Presenter p((IView&)w, std::move(t_factory), std::move(conf_mgr));

return app.exec();
}
Loading

0 comments on commit ac39185

Please sign in to comment.