[ADD] hand detection in startup scene

This commit is contained in:
Sem van der Hoeven
2021-06-18 17:15:26 +02:00
parent c1ba33e55b
commit 7c3d68a742
7 changed files with 59 additions and 73 deletions

View File

@@ -23,6 +23,7 @@ namespace computervision
bool background_calibrated = false; bool background_calibrated = false;
bool skin_calibrated = false; bool skin_calibrated = false;
bool hand_open = false;
ObjectDetection::ObjectDetection() ObjectDetection::ObjectDetection()
{ {
@@ -94,6 +95,7 @@ namespace computervision
skin_calibrated = true; skin_calibrated = true;
hand_calibrator.SetSkinCalibration(skin_calibrated); hand_calibrator.SetSkinCalibration(skin_calibrated);
time = 0; time = 0;
calibration_callback();
} }
} }
@@ -126,8 +128,8 @@ namespace computervision
hand_present = hand_calibrator.CheckIfHandPresent(handMask, handcalibration::HandDetectionType::MENU); hand_present = hand_calibrator.CheckIfHandPresent(handMask, handcalibration::HandDetectionType::MENU);
hand_calibrator.SetHandPresent(hand_present); hand_calibrator.SetHandPresent(hand_present);
hand_open = fingers_amount > 0;
return fingers_amount > 0; return hand_open;
} }
void ObjectDetection::CalculateDifference() void ObjectDetection::CalculateDifference()
@@ -187,5 +189,15 @@ namespace computervision
time += delt_time; time += delt_time;
} }
bool ObjectDetection::IsHandOpen()
{
return hand_open;
}
bool ObjectDetection::IsCalibrated()
{
return background_calibrated && skin_calibrated;
}
} }

View File

@@ -11,6 +11,7 @@
#include <opencv2/video.hpp> #include <opencv2/video.hpp>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <functional>
#include "background_remover.h" #include "background_remover.h"
#include "skin_detector.h" #include "skin_detector.h"
#include "finger_count.h" #include "finger_count.h"
@@ -81,6 +82,8 @@ namespace computervision
*/ */
bool IsHandOpen(); bool IsHandOpen();
bool IsCalibrated();
/** /**
* @brief checks whether the hand is held within the detection square. * @brief checks whether the hand is held within the detection square.
@@ -91,10 +94,13 @@ namespace computervision
cv::VideoCapture GetCap(); cv::VideoCapture GetCap();
void SetCalibrationCallback(std::function<void()> fun) { calibration_callback = fun; };
private: private:
bool is_hand_open; bool is_hand_open;
bool is_hand_present; bool is_hand_present;
void UpdateTime(); void UpdateTime();
std::function<void()> calibration_callback;
}; };

View File

@@ -23,11 +23,10 @@ namespace scene
{ {
shaders::GuiShader* gui_shader_gameOver; shaders::GuiShader* gui_shader_gameOver;
std::vector<gui::GuiTexture*> guis_gameOver; std::vector<gui::GuiTexture*> guis_gameOver;
computervision::ObjectDetection objDetect_gameOver;
std::vector<std::shared_ptr<gui::GuiTexture>> score_textures_gameOver; std::vector<std::shared_ptr<gui::GuiTexture>> score_textures_gameOver;
double delta_time = 0;
float item_number_gameOver = 0; double time = 0;
bool hand_mode_gameOver = false; bool game_over = false;
Game_Over_Scene::Game_Over_Scene(int score) Game_Over_Scene::Game_Over_Scene(int score)
{ {
@@ -76,21 +75,6 @@ namespace scene
} }
} }
gui::GuiTexture* GetMenuItemGameOver(bool hand_state) {
if (hand_state)
item_number_gameOver += 0.20f;
int temp_item_number = item_number_gameOver;
//If temp_item_number is equal to the size of the array, set item_number bac to zero to loop through the array again
if (temp_item_number == guis_gameOver.size()) {
item_number_gameOver = 0;
temp_item_number = 0;
}
std::cout << guis_gameOver[temp_item_number]->texture << std::endl;
return guis_gameOver[temp_item_number];
}
scene::Scenes scene::Game_Over_Scene::start(GLFWwindow* window) { scene::Scenes scene::Game_Over_Scene::start(GLFWwindow* window) {
gui::Button button_start_scene(render_engine::loader::LoadTexture("res/Birb1.jpg"), glm::vec2(0.0f, -0.5f), glm::vec2(0.25f, 0.25f)); gui::Button button_start_scene(render_engine::loader::LoadTexture("res/Birb1.jpg"), glm::vec2(0.0f, -0.5f), glm::vec2(0.25f, 0.25f));
button_start_scene.SetHoverTexture(render_engine::loader::LoadTexture("res/Birb2.jpg")); button_start_scene.SetHoverTexture(render_engine::loader::LoadTexture("res/Birb2.jpg"));
@@ -112,46 +96,8 @@ namespace scene
{ {
render(); render();
update(window); update(window);
if (hand_mode_gameOver) if (game_over) button_start_scene.ForceClick(GLFW_MOUSE_BUTTON_LEFT);
{
cameraFrame = objDetect_gameOver.ReadCamera();
bool detect = false;
bool hand_detection = objDetect_gameOver.DetectHand(cameraFrame, detect);
if (hand_detection)
{
hand_closed = false;
std::cout << "hand is opened" << std::endl;
//Loop through menu items
chosen_item_gameOver = GetMenuItemGameOver(true);
gui::Button* new_button = ConvertGuiTextureToButtonGameOver(chosen_item_gameOver);
if (new_button != NULL) {
const float x_pos = (chosen_item_gameOver->position.x + 1.0) * WINDOW_WIDTH / 2;
const float y_pos = (1.0 - chosen_item_gameOver->position.y) * WINDOW_HEIGHT / 2;
//Set cursor to location of selected menu_item
glfwSetCursorPos(window, x_pos, y_pos);
}
}
else if (!hand_detection)
{
std::cout << "hand is closed" << std::endl;
//Gets selected menu_item
chosen_item_gameOver = GetMenuItemGameOver(false);
gui::Button* new_button = ConvertGuiTextureToButtonGameOver(chosen_item_gameOver);
if (new_button != NULL && !hand_closed) {
//Run function click
new_button->ForceClick(GLFW_MOUSE_BUTTON_LEFT);
hand_closed = true;
}
}
}
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
} }
@@ -169,7 +115,10 @@ namespace scene
render_engine::renderer::Prepare(); render_engine::renderer::Prepare();
// Render GUI items // Render GUI items
render_engine::renderer::Render(guis_gameOver, *gui_shader_gameOver); //render_engine::renderer::Render(guis_gameOver, *gui_shader_gameOver);
render_engine::renderer::Render(game_over_texture, *gui_shader_gameOver);
DrawScore(end_score);
} }
/** /**
@@ -182,11 +131,16 @@ namespace scene
if (new_button != NULL) if (new_button != NULL)
new_button->Update(window); new_button->Update(window);
} }
bool hand_present;
objDetect_gameOver.DetectHand(objDetect_gameOver.ReadCamera(), hand_present); UpdateDeltaTime();
time += delta_time;
if (time >= 5.0)
{
game_over = true;
}
render_engine::renderer::Render(game_over_texture, *gui_shader_gameOver);
DrawScore(end_score);
} }
/** /**
@@ -199,9 +153,6 @@ namespace scene
//return_value = scene::Scenes::STARTUP; //return_value = scene::Scenes::STARTUP;
cv::destroyWindow("camera"); cv::destroyWindow("camera");
} }
else if (glfwGetKey(window, GLFW_KEY_BACKSPACE) == GLFW_PRESS) {
hand_mode_gameOver = !hand_mode_gameOver;
}
} }
void Game_Over_Scene::DrawScore(int score) void Game_Over_Scene::DrawScore(int score)
@@ -217,4 +168,13 @@ namespace scene
render_engine::renderer::Render(score_textures_gameOver[digits[i]], *gui_shader_gameOver); render_engine::renderer::Render(score_textures_gameOver[digits[i]], *gui_shader_gameOver);
} }
} }
void Game_Over_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;
}
} }

View File

@@ -14,6 +14,7 @@ namespace scene
scene::Scenes return_value = scene::Scenes::GAMEOVER; scene::Scenes return_value = scene::Scenes::GAMEOVER;
std::vector<std::shared_ptr<gui::GuiTexture>> score_guis_gameOver; std::vector<std::shared_ptr<gui::GuiTexture>> score_guis_gameOver;
std::shared_ptr<gui::GuiTexture> game_over_texture; std::shared_ptr<gui::GuiTexture> game_over_texture;
void UpdateDeltaTime();
public: public:

View File

@@ -275,6 +275,7 @@ namespace scene
*ptr = score; *ptr = score;
std::cout << "Score: " << score << std::endl; std::cout << "Score: " << score << std::endl;
return_value = scene::Scenes::GAMEOVER; return_value = scene::Scenes::GAMEOVER;
cv::destroyWindow("camera");
} }
camera->Follow(main_character->GetPosition()); camera->Follow(main_character->GetPosition());

View File

@@ -40,6 +40,11 @@ namespace scene
gui_shader1->Init(); gui_shader1->Init();
} }
void Startup_Scene::EnableHandMode()
{
hand_mode = true;
}
gui::Button* ConvertGuiTextureToButton(gui::GuiTexture* texture) { gui::Button* ConvertGuiTextureToButton(gui::GuiTexture* texture) {
gui::Button* button; gui::Button* button;
if (texture != NULL) if (texture != NULL)
@@ -92,7 +97,7 @@ namespace scene
scene::Scenes scene::Startup_Scene::start(GLFWwindow *window) scene::Scenes scene::Startup_Scene::start(GLFWwindow *window)
{ {
// GUI stuff // GUI stuff
gui::Button button_start(render_engine::loader::LoadTexture("res/menu_item_start1.png"), glm::vec2(0.0f, 0.6f), glm::vec2(0.25f, 0.25f)); gui::Button button_start(render_engine::loader::LoadTexture("res/menu_item_start1.png"), glm::vec2(0.0f, 0.3f), glm::vec2(0.25f, 0.25f));
button_start.SetHoverTexture(render_engine::loader::LoadTexture("res/menu_item_start1_hover.png")); button_start.SetHoverTexture(render_engine::loader::LoadTexture("res/menu_item_start1_hover.png"));
button_start.SetClickedTexture(render_engine::loader::LoadTexture("res/menu_item_start1_click.png")); button_start.SetClickedTexture(render_engine::loader::LoadTexture("res/menu_item_start1_click.png"));
std::function<void()> start_fun = [this]() std::function<void()> start_fun = [this]()
@@ -106,7 +111,7 @@ namespace scene
guis1.push_back(&button_start); guis1.push_back(&button_start);
gui::Button button_quit(render_engine::loader::LoadTexture("res/menu_item_quit1.png"), glm::vec2(0.0f, -0.6f), glm::vec2(0.25f, 0.25f)); gui::Button button_quit(render_engine::loader::LoadTexture("res/menu_item_quit1.png"), glm::vec2(0.0f, -0.3f), glm::vec2(0.25f, 0.25f));
button_quit.SetHoverTexture(render_engine::loader::LoadTexture("res/menu_item_quit1_hover.png")); button_quit.SetHoverTexture(render_engine::loader::LoadTexture("res/menu_item_quit1_hover.png"));
button_quit.SetClickedTexture(render_engine::loader::LoadTexture("res/menu_item_quit1_click.png")); button_quit.SetClickedTexture(render_engine::loader::LoadTexture("res/menu_item_quit1_click.png"));
std::function<void()> quit_fun = [this]() std::function<void()> quit_fun = [this]()
@@ -118,11 +123,11 @@ namespace scene
button_quit.SetOnClickAction(quit_fun); button_quit.SetOnClickAction(quit_fun);
guis1.push_back(&button_quit); guis1.push_back(&button_quit);
computervision::ObjectDetection objDetect;
cv::Mat cameraFrame; cv::Mat cameraFrame;
gui::GuiTexture* chosen_item = NULL; //This is the selected menu_item gui::GuiTexture* chosen_item = NULL; //This is the selected menu_item
bool hand_closed = false; //Flag to prevent multiple button presses bool hand_closed = false; //Flag to prevent multiple button presses
std::function<void()> calibration_func = [this]() {EnableHandMode(); };
objDetect.SetCalibrationCallback(calibration_func);
while (return_value == scene::Scenes::STARTUP) while (return_value == scene::Scenes::STARTUP)
{ {
render(); render();

View File

@@ -11,6 +11,7 @@ namespace scene
private: private:
//return_value is an enum that is necessary for the scene switching. Whenever this changes, the scene will change to a different scene. //return_value is an enum that is necessary for the scene switching. Whenever this changes, the scene will change to a different scene.
scene::Scenes return_value = scene::Scenes::STARTUP; scene::Scenes return_value = scene::Scenes::STARTUP;
void EnableHandMode();
public: public:
/** /**