Browse Source

exception handling

master
Tim Zeuner 2 years ago
parent
commit
f55ebb6b3f

+ 12
- 8
AutonomousMode/Input/input.cpp View File

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;
return Mat();
//To do:Exception handeling
stringstream sstream;
sstream << "Could not read the image: " << filePath << std::endl;
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;
Mat image; Mat image;


if(!cap.isOpened()) { if(!cap.isOpened()) {
cout << "Video capture not opened" << std::endl;
return Mat();
stringstream sstream;
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;
return Mat();
stringstream sstream;
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;

+ 1
- 0
AutonomousMode/Input/input.h View File

#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>

+ 5
- 3
AutonomousMode/autonomous_mode_main.cpp View File

{ {
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
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());
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);

+ 5
- 1
AutonomousMode/lfr.cpp View File

} }
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)

Loading…
Cancel
Save