From 05ae8ee019545f4779ff7c6929fafd3146fd04c1 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 25 May 2021 14:49:04 +0200 Subject: [PATCH] [FEATURE] finished hand open/closed recognition --- src/computervision/FingerCount.cpp | 7 +++++++ src/computervision/FingerCount.h | 9 +++++++++ src/computervision/ObjectDetection.cpp | 13 +++++++++---- src/computervision/ObjectDetection.h | 4 ++-- src/main.cpp | 4 +--- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/computervision/FingerCount.cpp b/src/computervision/FingerCount.cpp index 9f0f577..590e2a2 100644 --- a/src/computervision/FingerCount.cpp +++ b/src/computervision/FingerCount.cpp @@ -151,9 +151,16 @@ namespace computervision drawVectorPoints(frame, filtered_finger_points, color_yellow, false); putText(frame, to_string(filtered_finger_points.size()), center_bounding_rect, FONT_HERSHEY_PLAIN, 3, color_purple); + amount_of_fingers = filtered_finger_points.size(); + return contours_image; } + int FingerCount::getAmountOfFingers() + { + return amount_of_fingers; + } + double FingerCount::findPointsDistance(Point a, Point b) { Point difference = a - b; return sqrt(difference.ddot(difference)); diff --git a/src/computervision/FingerCount.h b/src/computervision/FingerCount.h index 5d47c47..3319150 100644 --- a/src/computervision/FingerCount.h +++ b/src/computervision/FingerCount.h @@ -24,6 +24,13 @@ namespace computervision */ Mat findFingersCount(Mat input_image, Mat frame); + /** + * @brief gets the currently held-up finger count. + * + * @return the currently held-up finger count + */ + int getAmountOfFingers(); + private: // colors to use Scalar color_blue; @@ -34,6 +41,8 @@ namespace computervision Scalar color_yellow; Scalar color_purple; + int amount_of_fingers; + /** * @brief finds the distance between 2 points. * diff --git a/src/computervision/ObjectDetection.cpp b/src/computervision/ObjectDetection.cpp index cd8e5d0..62281c2 100644 --- a/src/computervision/ObjectDetection.cpp +++ b/src/computervision/ObjectDetection.cpp @@ -72,8 +72,9 @@ namespace computervision return true; } - bool ObjectDetection::detectHand(Mat inputFrame) + bool ObjectDetection::detectHand(Mat cameraFrame) { + Mat inputFrame = generateHandMaskSquare(cameraFrame); frameOut = inputFrame.clone(); skinDetector.drawSkinColorSampler(frameOut); @@ -84,13 +85,17 @@ namespace computervision handMask = skinDetector.getSkinMask(foreground); fingerCountDebug = fingerCount.findFingersCount(handMask, frameOut); + int fingers_amount = fingerCount.getAmountOfFingers(); //backgroundRemover.calibrate(frame); + drawHandMaskRect(&cameraFrame); + string hand_text = fingers_amount > 0 ? "open" : "closed"; + putText(cameraFrame,hand_text, Point(10, 75), FONT_HERSHEY_PLAIN, 2.0, Scalar(255, 0, 255),3); + imshow("camera", cameraFrame); - - imshow("output", frameOut); + /* imshow("output", frameOut); imshow("foreground", foreground); imshow("handMask", handMask); - imshow("handDetection", fingerCountDebug); + imshow("handDetection", fingerCountDebug);*/ int key = waitKey(1); diff --git a/src/computervision/ObjectDetection.h b/src/computervision/ObjectDetection.h index 52ac82a..941f49f 100644 --- a/src/computervision/ObjectDetection.h +++ b/src/computervision/ObjectDetection.h @@ -64,10 +64,10 @@ namespace computervision /** * @brief detects a hand based on the given hand mask input frame. * - * @param inputFrame the input frame with only the hand + * @param inputFrame the input frame from the camera * @return true if the webcam is connected, false if not. */ - bool detectHand(cv::Mat inputFrame); + bool detectHand(cv::Mat cameraFrame); /** * @brief draws the hand mask rectangle on the given input matrix. diff --git a/src/main.cpp b/src/main.cpp index 643231c..7bab3f9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -87,9 +87,7 @@ int main(void) render_engine::renderer::Render(entity, shader); cameraFrame = objDetect.readCamera(); - objDetect.detectHand(objDetect.generateHandMaskSquare(cameraFrame)); - objDetect.drawHandMaskRect(&cameraFrame); - cv::imshow("camera",cameraFrame); + objDetect.detectHand(cameraFrame); // Finish up