summaryrefslogtreecommitdiffhomepage
path: root/ftnoir_tracker_pt/camera.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2015-07-26 06:45:31 +0200
committerStanislaw Halik <sthalik@misaki.pl>2015-07-26 06:50:47 +0200
commit1b00f064f5f10692af63d786bb9d27ea1356d946 (patch)
treefe19301feda6f5d57fae58302cc6a82f054ada6a /ftnoir_tracker_pt/camera.cpp
parent24538cf3a3a91481851618791b11be81437563e4 (diff)
pt: preventing crashing on camera manipulation
Diffstat (limited to 'ftnoir_tracker_pt/camera.cpp')
-rw-r--r--ftnoir_tracker_pt/camera.cpp37
1 files changed, 19 insertions, 18 deletions
diff --git a/ftnoir_tracker_pt/camera.cpp b/ftnoir_tracker_pt/camera.cpp
index 9e11b815..78a37b71 100644
--- a/ftnoir_tracker_pt/camera.cpp
+++ b/ftnoir_tracker_pt/camera.cpp
@@ -43,14 +43,14 @@ void Camera::set_res(int x_res, int y_res)
}
}
-CamInfo Camera::get_info()
+bool Camera::get_info(CamInfo& ret)
{
if (cam_info.res_x == 0 || cam_info.res_y == 0)
{
- cv::Mat tmp;
- _get_frame(&tmp);
+ return false;
}
- return cam_info;
+ ret = cam_info;
+ return true;
}
bool Camera::get_frame(float dt, cv::Mat* frame)
@@ -65,26 +65,25 @@ bool Camera::get_frame(float dt, cv::Mat* frame)
cam_info.fps = dt_mean > 1e-3 ? 1.0 / dt_mean : 0;
dt_valid = 0;
}
+ else
+ qDebug() << "pt camera: can't get frame";
return new_frame;
}
void CVCamera::start()
{
- if (cap)
- delete cap;
+ stop();
cap = new cv::VideoCapture(desired_index);
_set_res();
_set_fps();
// extract camera info
if (cap->isOpened())
{
- active = true;
active_index = desired_index;
cam_info.res_x = 0;
cam_info.res_y = 0;
} else {
- delete cap;
- cap = nullptr;
+ stop();
}
}
@@ -92,13 +91,19 @@ void CVCamera::stop()
{
if (cap)
{
- cap->release();
+ const bool opened = cap->isOpened();
+ if (opened)
+ {
+ qDebug() << "pt: freeing camera";
+ cap->release();
+ }
delete cap;
cap = nullptr;
// give opencv time to exit camera threads, etc.
- portable::sleep(500);
+ if (opened)
+ portable::sleep(500);
+ qDebug() << "camera: assuming stopped";
}
- active = false;
}
bool CVCamera::_get_frame(cv::Mat* frame)
@@ -135,10 +140,6 @@ void CVCamera::_set_res()
}
void CVCamera::_set_device_index()
{
- if (cap)
- {
- cap->release();
- delete cap;
- }
- cap = new cv::VideoCapture(desired_index);
+ if (desired_index != active_index)
+ stop();
}