[EDIT] mutex with cap
This commit is contained in:
@@ -27,7 +27,11 @@ namespace computervision
|
||||
}
|
||||
|
||||
cv::Mat ObjectDetection::readCamera() {
|
||||
videocapture::getMutex()->lock();
|
||||
|
||||
videocapture::getCap().read(img);
|
||||
|
||||
videocapture::getMutex()->unlock();
|
||||
return img;
|
||||
}
|
||||
|
||||
@@ -74,8 +78,10 @@ namespace computervision
|
||||
|
||||
void ObjectDetection::calculateDifference()
|
||||
{
|
||||
videocapture::getMutex()->lock();
|
||||
videocapture::getCap().read(img);
|
||||
videocapture::getCap().read(img2);
|
||||
videocapture::getMutex()->unlock();
|
||||
|
||||
cv::cvtColor(img, imgGray, cv::COLOR_RGBA2GRAY);
|
||||
cv::cvtColor(img2, img2Gray, cv::COLOR_RGBA2GRAY);
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
#include "VideoCapture.h"
|
||||
#include <mutex>
|
||||
|
||||
namespace videocapture{
|
||||
static cv::VideoCapture cap(1);
|
||||
static std::mutex mtx;
|
||||
|
||||
cv::VideoCapture getCap() {
|
||||
cap.release();
|
||||
//cap.release();
|
||||
|
||||
return cap;
|
||||
}
|
||||
|
||||
std::mutex* getMutex()
|
||||
{
|
||||
return &mtx;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
#include <opencv2/videoio.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/video.hpp>
|
||||
#include <mutex>
|
||||
|
||||
namespace videocapture {
|
||||
cv::VideoCapture getCap();
|
||||
std::mutex* getMutex();
|
||||
|
||||
}
|
||||
|
||||
@@ -23,17 +23,23 @@ namespace computervision
|
||||
auto lambda = [](std::function<void(std::vector<Point>)> f, OpenPoseVideo op) {
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user