From cadee7d8e9a2a4fc5db6010f8ea996f8dbdf4a8b Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 8 Jun 2021 13:38:47 +0200 Subject: [PATCH] [ADD] hand detection type enum --- src/computervision/HandDetectRegion.cpp | 19 ++++-------- .../calibration/HandCalibrator.cpp | 11 +++++-- .../calibration/HandCalibrator.h | 11 ++++++- src/scenes/in_Game_Scene.cpp | 29 ++++++++++++++++++ src/scenes/in_Game_Scene.h | 1 + src/scenes/startup_Scene.cpp | 30 ------------------- 6 files changed, 53 insertions(+), 48 deletions(-) diff --git a/src/computervision/HandDetectRegion.cpp b/src/computervision/HandDetectRegion.cpp index cf09f07..f789db6 100644 --- a/src/computervision/HandDetectRegion.cpp +++ b/src/computervision/HandDetectRegion.cpp @@ -29,23 +29,11 @@ namespace computervision // detect the hand contours handMask = skin_detector.getSkinMask(foreground); - // count the amount of fingers and put the info on the matrix - //fingerCountDebug = finger_count.findFingersCount(handMask, frame_out); - - //// get the amount of fingers - //int fingers_amount = finger_count.getAmountOfFingers(); - // draw the hand rectangle on the camera input, and draw text showing if the hand is open or closed. DrawHandMask(&camera_frame); - - //hand_calibrator.SetAmountOfFingers(fingers_amount); - //finger_count.DrawHandContours(camera_frame); - //hand_calibrator.DrawHandCalibrationText(camera_frame); - //imshow("camera", camera_frame); - - imshow("output" + region_id, frame_out); - imshow("foreground" + region_id, foreground); + //imshow("output" + region_id, frame_out); + //imshow("foreground" + region_id, foreground); imshow("handMask" + region_id, handMask); /*imshow("handDetection", fingerCountDebug);*/ @@ -83,6 +71,7 @@ namespace computervision void HandDetectRegion::CalibrateBackground() { + std::cout << "calibrating background " << region_id << std::endl; background_remover.calibrate(frame_out); hand_calibrator.SetBackGroundCalibrated(true); } @@ -94,11 +83,13 @@ namespace computervision std::vector HandDetectRegion::CalculateSkinTresholds() { + std::cout << "calibrating skin " << region_id << std::endl; return skin_detector.calibrateAndReturn(frame_out); } void HandDetectRegion::setSkinTresholds(std::vector& tresholds) { + std::cout << "setting skin " << region_id << std::endl; skin_detector.setTresholds(tresholds); } diff --git a/src/computervision/calibration/HandCalibrator.cpp b/src/computervision/calibration/HandCalibrator.cpp index 36c64ea..86f3aa5 100644 --- a/src/computervision/calibration/HandCalibrator.cpp +++ b/src/computervision/calibration/HandCalibrator.cpp @@ -1,7 +1,8 @@ #include "HandCalibrator.h" -#define MIN_HAND_SIZE 10000 +#define MIN_MENU_HAND_SIZE 10000 +#define MIN_GAME_HAND_SIZE 10000 // todo change namespace computervision { namespace handcalibration @@ -56,7 +57,7 @@ namespace computervision fingers_amount = amount; } - bool HandCalibrator::CheckIfHandPresent(cv::Mat input_image) + bool HandCalibrator::CheckIfHandPresent(cv::Mat input_image, HandDetectionType type) { std::vector> points; cv::findContours(input_image, points, cv::RetrievalModes::RETR_LIST, cv::ContourApproximationModes::CHAIN_APPROX_SIMPLE); @@ -66,7 +67,11 @@ namespace computervision for (int p = 0; p < points.size(); p++) { int area = cv::contourArea(points[p]); - if (area > MIN_HAND_SIZE) return true; + if (type == handcalibration::HandDetectionType::MENU) + if (area > MIN_MENU_HAND_SIZE) return true; + + if (type == handcalibration::HandDetectionType::GAME) + if (area > MIN_GAME_HAND_SIZE) return true; } return false; diff --git a/src/computervision/calibration/HandCalibrator.h b/src/computervision/calibration/HandCalibrator.h index 9b2f7ae..fadc66d 100644 --- a/src/computervision/calibration/HandCalibrator.h +++ b/src/computervision/calibration/HandCalibrator.h @@ -7,11 +7,19 @@ namespace computervision { namespace handcalibration { + enum class HandDetectionType + { + MENU, + GAME + }; + class HandCalibrator { public: HandCalibrator(); + + /** * @brief draws the text to show the status of the calibration on the image * @@ -45,7 +53,7 @@ namespace computervision * * @param input_image the input image to check. */ - bool CheckIfHandPresent(cv::Mat input_image); + bool CheckIfHandPresent(cv::Mat input_image, HandDetectionType type); /** * @brief sets the amount of fingers that are currently detected. @@ -53,6 +61,7 @@ namespace computervision * @param amount the amount of fingers. */ void SetAmountOfFingers(int amount); + private: bool background_calibrated; diff --git a/src/scenes/in_Game_Scene.cpp b/src/scenes/in_Game_Scene.cpp index 0e7c268..bbadf93 100644 --- a/src/scenes/in_Game_Scene.cpp +++ b/src/scenes/in_Game_Scene.cpp @@ -10,6 +10,9 @@ #include "../renderEngine/renderer.h" #include "../shaders/entity_shader.h" #include "../toolbox/toolbox.h" +#include +#include "../computervision/HandDetectRegion.h" +#include "../computervision/ObjectDetection.h" namespace scene @@ -23,6 +26,11 @@ namespace scene entities::Camera camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0)); std::vector guis; + std::vector regions; + computervision::ObjectDetection objDetect; + computervision::HandDetectRegion reg1("left", 20, 100, 150, 150); + computervision::HandDetectRegion reg2("right", 200, 200, 150, 150); + In_Game_Scene::In_Game_Scene() { @@ -105,6 +113,7 @@ namespace scene void scene::In_Game_Scene::update(GLFWwindow* window) { camera.Move(window); + update_hand_detection(); } void scene::In_Game_Scene::onKey(GLFWwindow* window, int key, int scancode, int action, int mods) @@ -113,6 +122,26 @@ namespace scene { return_value = scene::Scenes::STOP; } + + if (glfwGetKey(window, GLFW_KEY_B) == GLFW_PRESS) + { + reg1.CalibrateBackground(); + reg2.CalibrateBackground(); + } + + if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) + { + std::vector tresholds = reg1.CalculateSkinTresholds(); + reg2.setSkinTresholds(tresholds); + } } + void scene::In_Game_Scene::update_hand_detection() + { + cv::Mat camera_frame = objDetect.ReadCamera(); + reg1.DetectHand(camera_frame); + reg2.DetectHand(camera_frame); + + cv::imshow("camera", camera_frame); + } } diff --git a/src/scenes/in_Game_Scene.h b/src/scenes/in_Game_Scene.h index 4581855..cb420d3 100644 --- a/src/scenes/in_Game_Scene.h +++ b/src/scenes/in_Game_Scene.h @@ -8,6 +8,7 @@ namespace scene { private: scene::Scenes return_value = scene::Scenes::INGAME; + void update_hand_detection(); public: In_Game_Scene(); diff --git a/src/scenes/startup_Scene.cpp b/src/scenes/startup_Scene.cpp index dc9d12c..7ef51db 100644 --- a/src/scenes/startup_Scene.cpp +++ b/src/scenes/startup_Scene.cpp @@ -8,15 +8,9 @@ namespace scene { - std::vector regions; - computervision::ObjectDetection objDetect; - computervision::HandDetectRegion reg1("left",20,100,150,150); - computervision::HandDetectRegion reg2("right",200,200,150,150); scene::Scenes scene::Startup_Scene::start(GLFWwindow *window) { - regions.push_back(reg1); - regions.push_back(reg2); while (return_value == scene::Scenes::STARTUP) { render(); @@ -36,30 +30,6 @@ namespace scene void scene::Startup_Scene::update(GLFWwindow* window) { - cv::Mat camera_frame = objDetect.ReadCamera(); - reg1.DetectHand(camera_frame); - reg2.DetectHand(camera_frame); - - cv::imshow("camera", camera_frame); - - int key = cv::waitKey(1); - - if (key == 98) // b, calibrate the background - { - for (int i = 0; i < regions.size(); i++) - { - regions[i].CalibrateBackground(); - } - } - else if (key == 115) // s, calibrate the skin color - { - std::vector tresholds = regions[0].CalculateSkinTresholds(); - for (int i = 1; i < regions.size(); i++) - { - regions[i].setSkinTresholds(tresholds); - } - - } }