exception handling

This commit is contained in:
Tim Zeuner 2023-01-04 16:52:42 +01:00
parent 7fe6850da4
commit f55ebb6b3f
4 changed files with 23 additions and 12 deletions

View File

@ -22,15 +22,17 @@ Mat Input::readFile(String filePath)
cv::glob(folder, filenames); cv::glob(folder, filenames);
// Random shuffle // Random shuffle
std::random_shuffle(filenames.begin(), filenames.end()); std::random_device rd;
std::mt19937 g(rd());
std::shuffle(filenames.begin(), filenames.end(), g);
Mat image = imread(filePath, IMREAD_COLOR); Mat image = imread(filePath, IMREAD_COLOR);
if(image.empty()) if(image.empty())
{ {
std::cout << "Could not read the image: " << filePath << std::endl; stringstream sstream;
return Mat(); sstream << "Could not read the image: " << filePath << std::endl;
//To do:Exception handeling throw std::runtime_error(sstream.str());
} }
resize(image, image, Size(this->videoWidth, this->videoHeight)); resize(image, image, Size(this->videoWidth, this->videoHeight));
return image; return image;
@ -41,13 +43,15 @@ Mat Input::readWebcam()
Mat image; Mat image;
if(!cap.isOpened()) { if(!cap.isOpened()) {
cout << "Video capture not opened" << std::endl; stringstream sstream;
return Mat(); sstream << "Video capture not opened" << std::endl;
throw std::runtime_error(sstream.str());
} }
if(!cap.grab()) { if(!cap.grab()) {
cout << "Could not grab frame from camera" << std::endl; stringstream sstream;
return Mat(); sstream << "Could not grab frame from camera" << std::endl;
throw std::runtime_error(sstream.str());
} }
cap.retrieve(image); cap.retrieve(image);
return image; return image;

View File

@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include <random>
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
#include <opencv2/core/utils/logger.hpp> #include <opencv2/core/utils/logger.hpp>

View File

@ -17,7 +17,7 @@ int main(void)
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
std::cerr<<"camera exception:"<<ex.what()<<std::endl; std::cerr<<"camera exception:"<<ex.what()<<std::endl;
return true; return false;
}); });
//To calculate the frame rate //To calculate the frame rate
@ -30,7 +30,9 @@ int main(void)
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
if (!result.rawImage.empty()) if (!result.rawImage.empty())
{ {
cv::resize(result.rawImage, img, cv::Size(128, 64+32)); img = result.rawImage;
//Resize to minimize latency using raspi over ssh
//cv::resize(result.rawImage, img, cv::Size(128, 64+32));
//Calculate frame rate //Calculate frame rate
now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()); now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
@ -46,7 +48,7 @@ int main(void)
lfr.startLoop(); lfr.startLoop();
for(int finished = false; finished != 'q';){ for(int finished = false; finished != 'q';){
finished = std::tolower(cv::waitKey(66)); finished = std::tolower(cv::waitKey(1));
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
if(!img.empty()){ if(!img.empty()){
cv::imshow("frame", img); cv::imshow("frame", img);

View File

@ -76,7 +76,11 @@ void LFR::createThread()
} }
catch(std::exception const &ex) catch(std::exception const &ex)
{ {
cb(ex); if(!cb(ex))
{
//callback returned false -> exception not handled -> exit
exit(EXIT_FAILURE);
}
} }
//Invoke the callback method (ListenerPair second -> ListenerCallback) //Invoke the callback method (ListenerPair second -> ListenerCallback)