diff --git a/src/computervision/ObjectDetection.cpp b/src/computervision/ObjectDetection.cpp index 1395183..33ade7d 100644 --- a/src/computervision/ObjectDetection.cpp +++ b/src/computervision/ObjectDetection.cpp @@ -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); diff --git a/src/computervision/VideoCapture.cpp b/src/computervision/VideoCapture.cpp index 52b6184..f9cf0cd 100644 --- a/src/computervision/VideoCapture.cpp +++ b/src/computervision/VideoCapture.cpp @@ -1,11 +1,18 @@ #include "VideoCapture.h" +#include 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; + } } diff --git a/src/computervision/VideoCapture.h b/src/computervision/VideoCapture.h index 85f5bc5..def9b9a 100644 --- a/src/computervision/VideoCapture.h +++ b/src/computervision/VideoCapture.h @@ -2,7 +2,10 @@ #include #include #include +#include namespace videocapture { cv::VideoCapture getCap(); + std::mutex* getMutex(); + } diff --git a/src/computervision/async/async_arm_detection.cpp b/src/computervision/async/async_arm_detection.cpp index 6d0dc29..6a8c9f2 100644 --- a/src/computervision/async/async_arm_detection.cpp +++ b/src/computervision/async/async_arm_detection.cpp @@ -23,17 +23,23 @@ namespace computervision auto lambda = [](std::function)> 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); } };