[ADD] auto calibration in game scene
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
#include "HandDetectRegion.h"
|
#include "HandDetectRegion.h"
|
||||||
|
#define TIME_DURATION 1.0f
|
||||||
namespace computervision
|
namespace computervision
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -20,6 +20,15 @@ namespace computervision
|
|||||||
Mat input_frame = GenerateHandMaskSquare(camera_frame);
|
Mat input_frame = GenerateHandMaskSquare(camera_frame);
|
||||||
frame_out = input_frame.clone();
|
frame_out = input_frame.clone();
|
||||||
|
|
||||||
|
if (!background_calibrated || !skin_calibrated)
|
||||||
|
if (time >= TIME_DURATION)
|
||||||
|
{
|
||||||
|
std::cout << "timer finised, seconds left: " << seconds_left << std::endl;
|
||||||
|
seconds_left--;
|
||||||
|
time = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// detect skin color
|
// detect skin color
|
||||||
skin_detector.drawSkinColorSampler(camera_frame,start_x_pos,start_y_pos,region_width,region_height);
|
skin_detector.drawSkinColorSampler(camera_frame,start_x_pos,start_y_pos,region_width,region_height);
|
||||||
|
|
||||||
@@ -32,6 +41,30 @@ namespace computervision
|
|||||||
// draw the hand rectangle on the camera input, and draw text showing if the hand is open or closed.
|
// draw the hand rectangle on the camera input, and draw text showing if the hand is open or closed.
|
||||||
DrawHandMask(&camera_frame);
|
DrawHandMask(&camera_frame);
|
||||||
|
|
||||||
|
if (seconds_left <= 0)
|
||||||
|
{
|
||||||
|
if (!background_calibrated)
|
||||||
|
{
|
||||||
|
background_remover.calibrate(input_frame);
|
||||||
|
background_calibrated = true;
|
||||||
|
hand_calibrator.SetBackGroundCalibrated(background_calibrated);
|
||||||
|
seconds_left = 5;
|
||||||
|
time = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
if (!skin_calibrated)
|
||||||
|
{
|
||||||
|
skin_detector.calibrate(input_frame);
|
||||||
|
skin_calibrated = true;
|
||||||
|
hand_calibrator.SetSkinCalibration(skin_calibrated);
|
||||||
|
time = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
//imshow("output" + region_id, frame_out);
|
//imshow("output" + region_id, frame_out);
|
||||||
//imshow("foreground" + region_id, foreground);
|
//imshow("foreground" + region_id, foreground);
|
||||||
//imshow("handMask" + region_id, handMask);
|
//imshow("handMask" + region_id, handMask);
|
||||||
@@ -47,7 +80,10 @@ namespace computervision
|
|||||||
|
|
||||||
hand_calibrator.DrawBackgroundSkinCalibrated(camera_frame);
|
hand_calibrator.DrawBackgroundSkinCalibrated(camera_frame);
|
||||||
|
|
||||||
|
std::string calibration_text = (!background_calibrated ? "calibrating background in " : (!skin_calibrated ? "calibrating skin in " : ""));
|
||||||
|
calibration_text += std::to_string(seconds_left);
|
||||||
|
if (!background_calibrated || !skin_calibrated)
|
||||||
|
cv:putText(camera_frame, calibration_text, cv::Point(5, 400), cv::FONT_HERSHEY_COMPLEX, 1.0, cv::Scalar(255, 0, 255), 2);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,4 +138,9 @@ namespace computervision
|
|||||||
hand_calibrator.SetSkinCalibration(true);
|
hand_calibrator.SetSkinCalibration(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HandDetectRegion::UpdateTime(float delta_time)
|
||||||
|
{
|
||||||
|
time += delta_time;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <opencv2/core.hpp>
|
#include <opencv2/core.hpp>
|
||||||
#include <opencv2/imgproc.hpp>
|
#include <opencv2/imgproc.hpp>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#include "async/StaticCameraInstance.h"
|
#include "async/StaticCameraInstance.h"
|
||||||
#include "calibration/HandCalibrator.h"
|
#include "calibration/HandCalibrator.h"
|
||||||
#include "BackgroundRemover.h"
|
#include "BackgroundRemover.h"
|
||||||
@@ -36,8 +38,10 @@ namespace computervision
|
|||||||
std::vector<int> CalculateSkinTresholds();
|
std::vector<int> CalculateSkinTresholds();
|
||||||
|
|
||||||
void setSkinTresholds(std::vector<int>& tresholds);
|
void setSkinTresholds(std::vector<int>& tresholds);
|
||||||
|
void UpdateTime(float delta_time);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
int start_x_pos;
|
int start_x_pos;
|
||||||
int start_y_pos;
|
int start_y_pos;
|
||||||
int region_height;
|
int region_height;
|
||||||
@@ -51,6 +55,15 @@ namespace computervision
|
|||||||
std::string region_id;
|
std::string region_id;
|
||||||
|
|
||||||
bool DrawHandMask(cv::Mat* input);
|
bool DrawHandMask(cv::Mat* input);
|
||||||
|
|
||||||
|
float time = 0;
|
||||||
|
int seconds_left = 5; // calibration countdown
|
||||||
|
|
||||||
|
bool background_calibrated = false;
|
||||||
|
bool skin_calibrated = false;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,14 +51,9 @@ namespace computervision
|
|||||||
bool ObjectDetection::DetectHand(Mat camera_frame, bool& hand_present)
|
bool ObjectDetection::DetectHand(Mat camera_frame, bool& hand_present)
|
||||||
{
|
{
|
||||||
//calculate deltatime
|
//calculate deltatime
|
||||||
|
if (!background_calibrated || !skin_calibrated)
|
||||||
double current_time = glfwGetTime();
|
{
|
||||||
static double last_frame_time = current_time;
|
UpdateTime();
|
||||||
double delt_time = current_time - last_frame_time;
|
|
||||||
std::cout << "delta time : " << delt_time << std::endl;
|
|
||||||
last_frame_time = current_time;
|
|
||||||
|
|
||||||
time += delt_time;
|
|
||||||
|
|
||||||
if (time >= TIME_DURATION)
|
if (time >= TIME_DURATION)
|
||||||
{
|
{
|
||||||
@@ -66,9 +61,7 @@ namespace computervision
|
|||||||
seconds_left--;
|
seconds_left--;
|
||||||
time = 0;
|
time = 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Mat input_frame = GenerateHandMaskSquare(camera_frame);
|
Mat input_frame = GenerateHandMaskSquare(camera_frame);
|
||||||
@@ -119,10 +112,11 @@ namespace computervision
|
|||||||
hand_calibrator.SetAmountOfFingers(fingers_amount);
|
hand_calibrator.SetAmountOfFingers(fingers_amount);
|
||||||
finger_count.DrawHandContours(camera_frame);
|
finger_count.DrawHandContours(camera_frame);
|
||||||
hand_calibrator.DrawHandCalibrationText(camera_frame);
|
hand_calibrator.DrawHandCalibrationText(camera_frame);
|
||||||
|
|
||||||
std::string calibration_text = (!background_calibrated ? "calibrating background in " : (!skin_calibrated ? "calibrating skin in " : ""));
|
std::string calibration_text = (!background_calibrated ? "calibrating background in " : (!skin_calibrated ? "calibrating skin in " : ""));
|
||||||
calibration_text += std::to_string(seconds_left);
|
calibration_text += std::to_string(seconds_left);
|
||||||
if (!background_calibrated || !skin_calibrated)
|
if (!background_calibrated || !skin_calibrated)
|
||||||
cv:putText(camera_frame, calibration_text, cv::Point(5, 400), cv::FONT_HERSHEY_COMPLEX, 1.0, cv::Scalar(255, 0, 255),2);
|
cv:putText(camera_frame, calibration_text, cv::Point(5, 400), cv::FONT_HERSHEY_COMPLEX, 1.0, cv::Scalar(255, 0, 255), 2);
|
||||||
|
|
||||||
imshow("camera", camera_frame);
|
imshow("camera", camera_frame);
|
||||||
|
|
||||||
@@ -201,5 +195,15 @@ namespace computervision
|
|||||||
imshow("Webcam image", img);
|
imshow("Webcam image", img);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ObjectDetection::UpdateTime()
|
||||||
|
{
|
||||||
|
double current_time = glfwGetTime();
|
||||||
|
static double last_frame_time = current_time;
|
||||||
|
double delt_time = current_time - last_frame_time;
|
||||||
|
last_frame_time = current_time;
|
||||||
|
|
||||||
|
time += delt_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -86,6 +86,7 @@ namespace computervision
|
|||||||
private:
|
private:
|
||||||
bool is_hand_open;
|
bool is_hand_open;
|
||||||
bool is_hand_present;
|
bool is_hand_present;
|
||||||
|
void UpdateTime();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ namespace scene
|
|||||||
int furniture_count_old;
|
int furniture_count_old;
|
||||||
int score;
|
int score;
|
||||||
|
|
||||||
|
float delta_time = 0;
|
||||||
|
|
||||||
std::vector<computervision::HandDetectRegion> regions;
|
std::vector<computervision::HandDetectRegion> regions;
|
||||||
computervision::HandDetectRegion reg_left("left", 0, 0, 150, 150), reg_right("right", 0, 0, 150, 150), reg_up("up", 0, 0, 150, 150);
|
computervision::HandDetectRegion reg_left("left", 0, 0, 150, 150), reg_right("right", 0, 0, 150, 150), reg_up("up", 0, 0, 150, 150);
|
||||||
|
|
||||||
@@ -73,11 +75,8 @@ namespace scene
|
|||||||
score_pointer = std::make_unique<gui::GuiTexture>(render_engine::loader::LoadTexture(texture_path), glm::vec2(-0.9f, 0.8f), glm::vec2(0.07, 0.15));
|
score_pointer = std::make_unique<gui::GuiTexture>(render_engine::loader::LoadTexture(texture_path), glm::vec2(-0.9f, 0.8f), glm::vec2(0.07, 0.15));
|
||||||
|
|
||||||
score_textures.push_back(score_pointer);
|
score_textures.push_back(score_pointer);
|
||||||
|
|
||||||
std::cout << "Add to score_pointer: " << texture_path << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Size textures: " << score_textures.size() << std::endl;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,6 +267,7 @@ namespace scene
|
|||||||
//updates certain variables
|
//updates certain variables
|
||||||
void scene::In_Game_Scene::update(GLFWwindow* window)
|
void scene::In_Game_Scene::update(GLFWwindow* window)
|
||||||
{
|
{
|
||||||
|
UpdateDeltaTime();
|
||||||
//camera.Move(window);
|
//camera.Move(window);
|
||||||
main_character->Move(window);
|
main_character->Move(window);
|
||||||
|
|
||||||
@@ -330,6 +330,10 @@ namespace scene
|
|||||||
|
|
||||||
void scene::In_Game_Scene::update_hand_detection()
|
void scene::In_Game_Scene::update_hand_detection()
|
||||||
{
|
{
|
||||||
|
reg_left.UpdateTime(delta_time);
|
||||||
|
reg_right.UpdateTime(delta_time);
|
||||||
|
reg_up.UpdateTime(delta_time);
|
||||||
|
|
||||||
cv::Mat camera_frame;
|
cv::Mat camera_frame;
|
||||||
static_camera::getCap().read(camera_frame);
|
static_camera::getCap().read(camera_frame);
|
||||||
reg_left.DetectHand(camera_frame);
|
reg_left.DetectHand(camera_frame);
|
||||||
@@ -352,14 +356,20 @@ namespace scene
|
|||||||
|
|
||||||
toolbox::GetDigitsFromNumber(score, digits);
|
toolbox::GetDigitsFromNumber(score, digits);
|
||||||
|
|
||||||
std::cout << "Digits size: " << digits.size() << std::endl;
|
|
||||||
|
|
||||||
for (int i = digits.size()-1; i >= 0; i--)
|
for (int i = digits.size()-1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
std::cout << "Digit in digits: " << i << std::endl;
|
|
||||||
score_textures[digits[i]].get()->position.x = 0.15 * i -0.9;
|
score_textures[digits[i]].get()->position.x = 0.15 * i -0.9;
|
||||||
render_engine::renderer::Render(score_textures[digits[i]], *gui_shader);
|
render_engine::renderer::Render(score_textures[digits[i]], *gui_shader);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void In_Game_Scene::UpdateDeltaTime()
|
||||||
|
{
|
||||||
|
double current_time = glfwGetTime();
|
||||||
|
static double last_frame_time = current_time;
|
||||||
|
delta_time = current_time - last_frame_time;
|
||||||
|
last_frame_time = current_time;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,8 @@ namespace scene
|
|||||||
|
|
||||||
std::vector<std::shared_ptr<gui::GuiTexture>> score_guis;
|
std::vector<std::shared_ptr<gui::GuiTexture>> score_guis;
|
||||||
|
|
||||||
|
void UpdateDeltaTime();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief renders the objects/gui models
|
* @brief renders the objects/gui models
|
||||||
* @param
|
* @param
|
||||||
|
|||||||
Reference in New Issue
Block a user