From 612adf6e9bba9fffd05b3f7228c77460a4a8c550 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Mon, 31 May 2021 15:50:06 +0200 Subject: [PATCH] [EDIT] mutex? --- src/computervision/ObjectDetection.cpp | 16 +++++---- src/computervision/OpenPoseVideo.cpp | 31 ++++++++++------- src/computervision/OpenPoseVideo.h | 3 +- src/computervision/VideoCapture.cpp | 19 +++++++++-- src/computervision/VideoCapture.h | 1 + .../async/async_arm_detection.cpp | 33 +++++++------------ src/main.cpp | 7 ++-- 7 files changed, 66 insertions(+), 44 deletions(-) diff --git a/src/computervision/ObjectDetection.cpp b/src/computervision/ObjectDetection.cpp index 33ade7d..44541be 100644 --- a/src/computervision/ObjectDetection.cpp +++ b/src/computervision/ObjectDetection.cpp @@ -27,11 +27,12 @@ namespace computervision } cv::Mat ObjectDetection::readCamera() { - videocapture::getMutex()->lock(); + /*videocapture::getMutex()->lock(); videocapture::getCap().read(img); - videocapture::getMutex()->unlock(); + videocapture::getMutex()->unlock();*/ + img = videocapture::readFrame(); return img; } @@ -78,10 +79,13 @@ namespace computervision void ObjectDetection::calculateDifference() { - videocapture::getMutex()->lock(); - videocapture::getCap().read(img); - videocapture::getCap().read(img2); - videocapture::getMutex()->unlock(); + //videocapture::getMutex()->lock(); + //videocapture::getCap().read(img); + //videocapture::getCap().read(img2); + //videocapture::getMutex()->unlock() + + img = videocapture::readFrame(); + img2 = videocapture::readFrame(); cv::cvtColor(img, imgGray, cv::COLOR_RGBA2GRAY); cv::cvtColor(img2, img2Gray, cv::COLOR_RGBA2GRAY); diff --git a/src/computervision/OpenPoseVideo.cpp b/src/computervision/OpenPoseVideo.cpp index 9cf1cad..416440f 100644 --- a/src/computervision/OpenPoseVideo.cpp +++ b/src/computervision/OpenPoseVideo.cpp @@ -39,18 +39,16 @@ namespace computervision int nPoints = 18; #endif Net net; + int inWidth = 368; + int inHeight = 368; + float thresh = 0.01; void OpenPoseVideo::setup() { net = readNetFromCaffe(protoFile, weightsFile); } - void OpenPoseVideo::movementSkeleton(Mat inputImage, std::function)> f) { - std::cout << "movement skeleton start" << std::endl; - - int inWidth = 368; - int inHeight = 368; - float thresh = 0.01; - + cv::Mat OpenPoseVideo::getBlobFromImage(cv::Mat inputImage) + { Mat frame; int frameWidth = inputImage.size().width; int frameHeight = inputImage.size().height; @@ -58,9 +56,17 @@ namespace computervision double t = (double)cv::getTickCount(); std::cout << "reading input image and blob" << std::endl; - frame = inputImage; + frame = inputImage.clone(); Mat inpBlob = blobFromImage(frame, 1.0 / 255, Size(inWidth, inHeight), Scalar(0, 0, 0), false, false); + return inpBlob; + } + + void OpenPoseVideo::movementSkeleton(Mat inputImage, Mat inpBlob, std::function)> f) { + std::cout << "movement skeleton start" << std::endl; + int frameWidth = inputImage.size().width; + int frameHeight = inputImage.size().height; + std::cout << "done reading image and blob" << std::endl; net.setInput(inpBlob); @@ -68,6 +74,7 @@ namespace computervision std::cout << "done setting input to net" << std::endl; Mat output = net.forward(); + int H = output.size[2]; int W = output.size[3]; @@ -89,15 +96,15 @@ namespace computervision p.x *= (float)frameWidth / W; p.y *= (float)frameHeight / H; - circle(frame, cv::Point((int)p.x, (int)p.y), 8, Scalar(0, 255, 255), -1); - cv::putText(frame, cv::format("%d", n), cv::Point((int)p.x, (int)p.y), cv::FONT_HERSHEY_COMPLEX, 1.1, cv::Scalar(0, 0, 255), 2); + /*circle(frame, cv::Point((int)p.x, (int)p.y), 8, Scalar(0, 255, 255), -1); + cv::putText(frame, cv::format("%d", n), cv::Point((int)p.x, (int)p.y), cv::FONT_HERSHEY_COMPLEX, 1.1, cv::Scalar(0, 0, 255), 2);*/ } points[n] = p; } - cv::putText(frame, cv::format("time taken = %.2f sec", t), cv::Point(50, 50), cv::FONT_HERSHEY_COMPLEX, .8, cv::Scalar(255, 50, 0), 2); + //cv::putText(frame, cv::format("time taken = %.2f sec", t), cv::Point(50, 50), cv::FONT_HERSHEY_COMPLEX, .8, cv::Scalar(255, 50, 0), 2); // imshow("Output-Keypoints", frameCopy); - imshow("Output-Skeleton", frame); + /*imshow("Output-Skeleton", frame);*/ std::cout << "about to call points receiving method" << std::endl; f(points); } diff --git a/src/computervision/OpenPoseVideo.h b/src/computervision/OpenPoseVideo.h index 8b9d75e..684015d 100644 --- a/src/computervision/OpenPoseVideo.h +++ b/src/computervision/OpenPoseVideo.h @@ -13,7 +13,8 @@ namespace computervision private: public: - void movementSkeleton(Mat inputImage,std::function)> f); + cv::Mat getBlobFromImage(cv::Mat inputImage); + void movementSkeleton(Mat inputImage, Mat inpBlob, std::function)> f); void setup(); }; } diff --git a/src/computervision/VideoCapture.cpp b/src/computervision/VideoCapture.cpp index f9cf0cd..bc4da8a 100644 --- a/src/computervision/VideoCapture.cpp +++ b/src/computervision/VideoCapture.cpp @@ -1,16 +1,31 @@ #include "VideoCapture.h" #include +#include namespace videocapture{ - static cv::VideoCapture cap(1); + static cv::VideoCapture cap(0); static std::mutex mtx; cv::VideoCapture getCap() { - //cap.release(); + cap.release(); return cap; } + cv::Mat readFrame() + { + std::cout << "reading frame" << std::endl; + cv::Mat camFrame, videoFrame; + + mtx.lock(); + bool res = cap.read(camFrame); + std::cout << (res ? "reading worked" : "reading failed") << std::endl; + videoFrame = camFrame.clone(); + mtx.unlock(); + + return videoFrame; + } + std::mutex* getMutex() { return &mtx; diff --git a/src/computervision/VideoCapture.h b/src/computervision/VideoCapture.h index def9b9a..fb89860 100644 --- a/src/computervision/VideoCapture.h +++ b/src/computervision/VideoCapture.h @@ -7,5 +7,6 @@ namespace videocapture { cv::VideoCapture getCap(); std::mutex* getMutex(); + cv::Mat readFrame(); } diff --git a/src/computervision/async/async_arm_detection.cpp b/src/computervision/async/async_arm_detection.cpp index 6a8c9f2..d088ebb 100644 --- a/src/computervision/async/async_arm_detection.cpp +++ b/src/computervision/async/async_arm_detection.cpp @@ -3,6 +3,9 @@ #include "../OpenPoseVideo.h" #include #include "../VideoCapture.h" +#include +#include +#include namespace computervision @@ -20,31 +23,19 @@ namespace computervision void AsyncArmDetection::start(std::function)> points_ready_func, OpenPoseVideo op) { - auto lambda = [](std::function)> f, OpenPoseVideo op) { + auto lambda = [](cv::Mat img, std::function)> f, OpenPoseVideo op, cv::Mat inpBlob) { std::cout << "STARTING THREAD LAMBDA" << std::endl; - videocapture::getMutex()->lock(); - if (!videocapture::getCap().isOpened()) - { - std::cout << "error opening video" << std::endl; - videocapture::getCap().open(1); - return; - } - videocapture::getMutex()->unlock(); - Mat img; - while (true) - { - videocapture::getMutex()->lock(); - - videocapture::getCap().read(img); - imshow("image", img); - - videocapture::getMutex()->unlock(); - op.movementSkeleton(img, f); - } + //imshow("image", img); 255, Size(368, 368), Scalar(0, 0, 0), false, false); + op.movementSkeleton(img, inpBlob, f); + //} }; + cv::Mat img = videocapture::readFrame(); std::cout << "starting function" << std::endl; - std::thread async_arm_detect_thread(lambda, points_ready_func, op); + cv::Mat inpBlob = op.getBlobFromImage(videocapture::readFrame()); + + + std::thread async_arm_detect_thread(lambda, img, points_ready_func, op, inpBlob); } } diff --git a/src/main.cpp b/src/main.cpp index f1f98ec..4ddf3be 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,11 +32,14 @@ static double UpdateDelta(); static GLFWwindow* window; +computervision::AsyncArmDetection as; +computervision::OpenPoseVideo openPoseVideo; void retrieve_points(std::vector arm_points) { std::cout << "got points!!" << std::endl; std::cout << "points: " << arm_points << std::endl; + as.start(retrieve_points, openPoseVideo); } int main(void) @@ -76,7 +79,7 @@ int main(void) // create object detection object instance computervision::ObjectDetection objDetect; //computervision::OpenPoseImage openPoseImage; - computervision::OpenPoseVideo openPoseVideo; + openPoseVideo.setup(); @@ -87,7 +90,7 @@ int main(void) //openPoseVideo.setup(); - computervision::AsyncArmDetection as; + as.start(retrieve_points, openPoseVideo);