[EDIT] mutex with cap

This commit is contained in:
Sem van der Hoeven
2021-05-31 10:25:24 +02:00
parent 9d80cddbd1
commit 25c99abb72
4 changed files with 23 additions and 1 deletions

View File

@@ -27,7 +27,11 @@ namespace computervision
} }
cv::Mat ObjectDetection::readCamera() { cv::Mat ObjectDetection::readCamera() {
videocapture::getMutex()->lock();
videocapture::getCap().read(img); videocapture::getCap().read(img);
videocapture::getMutex()->unlock();
return img; return img;
} }
@@ -74,8 +78,10 @@ namespace computervision
void ObjectDetection::calculateDifference() void ObjectDetection::calculateDifference()
{ {
videocapture::getMutex()->lock();
videocapture::getCap().read(img); videocapture::getCap().read(img);
videocapture::getCap().read(img2); videocapture::getCap().read(img2);
videocapture::getMutex()->unlock();
cv::cvtColor(img, imgGray, cv::COLOR_RGBA2GRAY); cv::cvtColor(img, imgGray, cv::COLOR_RGBA2GRAY);
cv::cvtColor(img2, img2Gray, cv::COLOR_RGBA2GRAY); cv::cvtColor(img2, img2Gray, cv::COLOR_RGBA2GRAY);

View File

@@ -1,11 +1,18 @@
#include "VideoCapture.h" #include "VideoCapture.h"
#include <mutex>
namespace videocapture{ namespace videocapture{
static cv::VideoCapture cap(1); static cv::VideoCapture cap(1);
static std::mutex mtx;
cv::VideoCapture getCap() { cv::VideoCapture getCap() {
cap.release(); //cap.release();
return cap; return cap;
} }
std::mutex* getMutex()
{
return &mtx;
}
} }

View File

@@ -2,7 +2,10 @@
#include <opencv2/videoio.hpp> #include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp> #include <opencv2/highgui.hpp>
#include <opencv2/video.hpp> #include <opencv2/video.hpp>
#include <mutex>
namespace videocapture { namespace videocapture {
cv::VideoCapture getCap(); cv::VideoCapture getCap();
std::mutex* getMutex();
} }

View File

@@ -23,17 +23,23 @@ namespace computervision
auto lambda = [](std::function<void(std::vector<Point>)> f, OpenPoseVideo op) { auto lambda = [](std::function<void(std::vector<Point>)> f, OpenPoseVideo op) {
std::cout << "STARTING THREAD LAMBDA" << std::endl; std::cout << "STARTING THREAD LAMBDA" << std::endl;
videocapture::getMutex()->lock();
if (!videocapture::getCap().isOpened()) if (!videocapture::getCap().isOpened())
{ {
std::cout << "error opening video" << std::endl; std::cout << "error opening video" << std::endl;
videocapture::getCap().open(1); videocapture::getCap().open(1);
return; return;
} }
videocapture::getMutex()->unlock();
Mat img; Mat img;
while (true) while (true)
{ {
videocapture::getMutex()->lock();
videocapture::getCap().read(img); videocapture::getCap().read(img);
imshow("image", img); imshow("image", img);
videocapture::getMutex()->unlock();
op.movementSkeleton(img, f); op.movementSkeleton(img, f);
} }
}; };