From ca2959bf2d1e3a3c10f4a67c93b2c8e248e847a2 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 4 Jun 2021 14:48:19 +0200 Subject: [PATCH] [FIX] detecting hand when its just a finger or hair --- src/computervision/HandPresentChecker.cpp | 14 +++++++++++--- src/computervision/ObjectDetection.cpp | 4 ++-- src/computervision/ObjectDetection.h | 3 ++- src/scenes/startup_Scene.cpp | 5 ++++- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/computervision/HandPresentChecker.cpp b/src/computervision/HandPresentChecker.cpp index 2cb4784..74ec08d 100644 --- a/src/computervision/HandPresentChecker.cpp +++ b/src/computervision/HandPresentChecker.cpp @@ -2,6 +2,7 @@ #include #include +#define MIN_HAND_SIZE 10000 namespace computervision { @@ -10,9 +11,16 @@ namespace computervision std::vector> points; cv::Mat imgCont; cv::findContours(inputImage, points, cv::RetrievalModes::RETR_LIST, cv::ContourApproximationModes::CHAIN_APPROX_SIMPLE); - bool hand_present = points.size() > 0; - std::cout << (hand_present ? "hey a hand!" : "damn no hand") << std::endl; - return hand_present; + + if (points.size() == 0) return false; + + for (int p = 0; p < points.size(); p++) + { + int area = cv::contourArea(points[p]); + if (area > MIN_HAND_SIZE) return true; + } + + return false; } } diff --git a/src/computervision/ObjectDetection.cpp b/src/computervision/ObjectDetection.cpp index f0edf1e..d0f554f 100644 --- a/src/computervision/ObjectDetection.cpp +++ b/src/computervision/ObjectDetection.cpp @@ -41,7 +41,7 @@ namespace computervision return cap; } - bool ObjectDetection::detectHand(Mat cameraFrame) + bool ObjectDetection::detectHand(Mat cameraFrame, bool& hand_present) { Mat inputFrame = generateHandMaskSquare(cameraFrame); frameOut = inputFrame.clone(); @@ -72,7 +72,7 @@ namespace computervision //imshow("handMask", handMask); //imshow("handDetection", fingerCountDebug); - check_if_hand_present(handMask); + hand_present = check_if_hand_present(handMask); diff --git a/src/computervision/ObjectDetection.h b/src/computervision/ObjectDetection.h index 45f4c6d..5deeaa6 100644 --- a/src/computervision/ObjectDetection.h +++ b/src/computervision/ObjectDetection.h @@ -54,9 +54,10 @@ namespace computervision * @brief detects a hand based on the given hand mask input frame. * * @param inputFrame the input frame from the camera + * @param hand_present boolean that will hold true if the hand is detected, false if not. * @return true if hand is open, false if hand is closed */ - bool detectHand(cv::Mat cameraFrame); + bool detectHand(cv::Mat cameraFrame, bool& hand_present); /** * @brief draws the hand mask rectangle on the given input matrix. diff --git a/src/scenes/startup_Scene.cpp b/src/scenes/startup_Scene.cpp index 7d94bb0..3a990f1 100644 --- a/src/scenes/startup_Scene.cpp +++ b/src/scenes/startup_Scene.cpp @@ -3,6 +3,7 @@ #include #include "startup_Scene.h" #include "../computervision/ObjectDetection.h" +#include namespace scene { @@ -29,7 +30,9 @@ namespace scene void scene::Startup_Scene::update(GLFWwindow* window) { - objDetect.detectHand(objDetect.readCamera()); + bool hand_detected = false; + objDetect.detectHand(objDetect.readCamera(),hand_detected); + if (hand_detected) std::cout << "there's a hand!" << std::endl; }