input threadsafe

This commit is contained in:
Tim Zeuner 2023-01-04 21:50:02 +01:00
parent 8150850bb6
commit 747053f132
2 changed files with 7 additions and 3 deletions

View File

@ -1,9 +1,10 @@
#include "input.h" #include "input.h"
// TODO: Wenn ihr in die Zeile den Pfad zum Testvideo statt der 0 packt, benmutzt er das Testvideo. // TODO: Wenn ihr in die Zeile den Pfad zum Testvideo statt der 0 packt, benmutzt er das Testvideo.
Input::Input(int videoHeight, int videoWidth) : cap("C:\\Line-Following-Robot\\AutonomousMode\\Test_data\\video1.h264"), videoHeight(videoHeight), videoWidth(videoWidth) Input::Input(int videoHeight, int videoWidth) : cap("C:\\Line-Following-Robot\\AutonomousMode\\Test_data\\video1.h264"), videoHeight(videoHeight), videoWidth(videoWidth)//Input::Input(int videoHeight, int videoWidth) : cap(0), videoHeight(videoHeight), videoWidth(videoWidth)
//Input::Input(int videoHeight, int videoWidth) : cap(0), videoHeight(videoHeight), videoWidth(videoWidth) //Input::Input(int videoHeight, int videoWidth) : cap(0), videoHeight(videoHeight), videoWidth(videoWidth)
{ {
std::unique_lock<std::mutex> lock(mtx);
this->cap.set(CAP_PROP_FRAME_HEIGHT, videoHeight); this->cap.set(CAP_PROP_FRAME_HEIGHT, videoHeight);
this->cap.set(CAP_PROP_FRAME_WIDTH, videoWidth); this->cap.set(CAP_PROP_FRAME_WIDTH, videoWidth);
} }
@ -15,7 +16,8 @@ Input::~Input()
Mat Input::readFile(String filePath) Mat Input::readFile(String filePath)
{ {
std::srand(std::time(0)); std::unique_lock<std::mutex> lock(mtx);
std::srand(static_cast<unsigned int>(std::time(0)));
// Read all .jpg files from the specified folder // Read all .jpg files from the specified folder
cv::String folder = filePath; cv::String folder = filePath;
std::vector<cv::String> filenames; std::vector<cv::String> filenames;
@ -40,6 +42,7 @@ Mat Input::readFile(String filePath)
Mat Input::readWebcam() Mat Input::readWebcam()
{ {
std::unique_lock<std::mutex> lock(mtx);
Mat image; Mat image;
if(!cap.isOpened()) { if(!cap.isOpened()) {
@ -59,5 +62,6 @@ Mat Input::readWebcam()
void Input::freeWebcam() void Input::freeWebcam()
{ {
std::unique_lock<std::mutex> lock(mtx);
this->cap.release(); this->cap.release();
} }

View File

@ -16,7 +16,7 @@ class Input
{ {
private: private:
VideoCapture cap; VideoCapture cap;
mutable std::mutex mtx;
public: public:
int videoHeight; int videoHeight;
int videoWidth; int videoWidth;