68 lines
2.0 KiB
C++
Raw Normal View History

2022-10-30 17:03:37 +01:00
#include "input.h"
2022-12-08 11:42:02 +01:00
// TODO: Wenn ihr in die Zeile den Pfad zum Testvideo statt der 0 packt, benmutzt er das Testvideo.
2023-01-04 21:50:02 +01:00
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)
2022-12-14 12:30:54 +01:00
//Input::Input(int videoHeight, int videoWidth) : cap(0), videoHeight(videoHeight), videoWidth(videoWidth)
2022-10-30 17:03:37 +01:00
{
2023-01-04 21:50:02 +01:00
std::unique_lock<std::mutex> lock(mtx);
this->cap.set(CAP_PROP_FRAME_HEIGHT, videoHeight);
this->cap.set(CAP_PROP_FRAME_WIDTH, videoWidth);
2022-10-30 17:03:37 +01:00
}
Input::~Input()
{
this->freeWebcam();
2022-10-30 17:03:37 +01:00
}
Mat Input::readFile(String filePath)
{
2023-01-04 21:50:02 +01:00
std::unique_lock<std::mutex> lock(mtx);
std::srand(static_cast<unsigned int>(std::time(0)));
// Read all .jpg files from the specified folder
cv::String folder = filePath;
std::vector<cv::String> filenames;
cv::glob(folder, filenames);
// Random shuffle
2023-01-04 16:52:42 +01:00
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(filenames.begin(), filenames.end(), g);
Mat image = imread(filePath, IMREAD_COLOR);
2022-10-30 17:03:37 +01:00
if(image.empty())
{
2023-01-04 16:52:42 +01:00
stringstream sstream;
sstream << "Could not read the image: " << filePath << std::endl;
throw std::runtime_error(sstream.str());
2022-10-30 17:03:37 +01:00
}
2022-11-16 11:08:53 +01:00
resize(image, image, Size(this->videoWidth, this->videoHeight));
2022-10-30 17:03:37 +01:00
return image;
}
Mat Input::readWebcam()
2022-10-30 17:03:37 +01:00
{
2023-01-04 21:50:02 +01:00
std::unique_lock<std::mutex> lock(mtx);
Mat image;
if(!cap.isOpened()) {
2023-01-04 16:52:42 +01:00
stringstream sstream;
sstream << "Video capture not opened" << std::endl;
throw std::runtime_error(sstream.str());
}
if(!cap.grab()) {
2023-01-04 16:52:42 +01:00
stringstream sstream;
sstream << "Could not grab frame from camera" << std::endl;
throw std::runtime_error(sstream.str());
}
2022-11-30 17:35:53 +01:00
cap.retrieve(image);
return image;
}
void Input::freeWebcam()
{
2023-01-04 21:50:02 +01:00
std::unique_lock<std::mutex> lock(mtx);
this->cap.release();
}