Skip to content

Commit

Permalink
tracker: avoiding segmentation faults
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmm committed May 31, 2024
1 parent a7ebdb5 commit 975a551
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/contents/ui/Tracker.qml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Kirigami.ScrollablePage {
Component.onCompleted: {
EoSTrackerBackend.videoSink = videoOutput.videoSink;
}
Component.onDestruction: EoSTrackerBackend.stop() // So we do not write to an invalid videoSink

MouseArea {
id: mouseArea
Expand Down
36 changes: 31 additions & 5 deletions src/tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <ios>
#include <iterator>
#include <memory>
#include <mutex>
#include <opencv2/core/cvstd_wrapper.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/core/types.hpp>
Expand Down Expand Up @@ -208,16 +209,30 @@ Backend::Backend(QObject* parent)
connect(this, &Backend::videoSinkChanged, [this]() { draw_offline_image(); });

connect(camera_video_sink.get(), &QVideoSink::videoFrameChanged, [this](const QVideoFrame& frame) {
std::lock_guard<std::mutex> trackers_lock_guard(trackers_mutex);

if (!pause_preview && !exiting) {
input_video_frame = frame;
process_frame();
QMetaObject::invokeMethod(
this,
[this, frame] {
input_video_frame = frame;
process_frame();
},
Qt::QueuedConnection);
}
});

connect(media_player_video_sink.get(), &QVideoSink::videoFrameChanged, [this](const QVideoFrame& frame) {
std::lock_guard<std::mutex> trackers_lock_guard(trackers_mutex);

if (!pause_preview && !exiting) {
input_video_frame = frame;
process_frame();
QMetaObject::invokeMethod(
this,
[this, frame] {
input_video_frame = frame;
process_frame();
},
Qt::QueuedConnection);
}
});

Expand Down Expand Up @@ -246,9 +261,14 @@ Backend::Backend(QObject* parent)
}

Backend::~Backend() {
exiting = true;
camera->stop();
media_player->stop();

std::lock_guard<std::mutex> trackers_lock_guard(trackers_mutex);

util::debug("Tracker backend exiting...");

exiting = true;
}

void Backend::start() {
Expand Down Expand Up @@ -416,6 +436,8 @@ void Backend::drawRoiSelection(const bool& state) {
}

void Backend::createNewRoi(double x, double y, double width, double height) {
std::lock_guard<std::mutex> trackers_lock_guard(trackers_mutex);

cv::Rect2d roi = {x, y, width, height}; // region of interest that is being created

cv::Ptr<cv::legacy::Tracker> tracker;
Expand Down Expand Up @@ -462,6 +484,8 @@ void Backend::newRoiSelection(double x, double y, double width, double height) {
}

int Backend::removeRoi(double x, double y) {
std::lock_guard<std::mutex> trackers_lock_guard(trackers_mutex);

initial_time = 0;

for (size_t n = 0; n < trackers.size(); n++) {
Expand All @@ -480,6 +504,8 @@ int Backend::removeRoi(double x, double y) {
}

void Backend::removeAllTrackers() {
std::lock_guard<std::mutex> trackers_lock_guard(trackers_mutex);

trackers.clear();
}

Expand Down
3 changes: 3 additions & 0 deletions src/tracker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QMediaPlayer>
#include <QVideoSink>
#include <memory>
#include <mutex>
#include <opencv2/core/cvstd_wrapper.hpp>
#include <opencv2/core/types.hpp>
#include <opencv2/tracking.hpp>
Expand Down Expand Up @@ -152,6 +153,8 @@ class Backend : public QObject {

std::vector<std::tuple<cv::Ptr<cv::legacy::Tracker>, cv::Rect2d, bool, QList<QPointF>, QList<QPointF>>> trackers;

std::mutex trackers_mutex;

void find_best_camera_resolution();
void draw_offline_image();
void process_frame();
Expand Down
11 changes: 11 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
#include <cstring>
#include <ext/string_conversions.h>
#include <filesystem>
#include <format>
#include <sstream>
#include <string>
#include <thread>

namespace util {

Expand Down Expand Up @@ -46,6 +49,14 @@ void info(const std::string& s, source_location location) {
qInfo().noquote() << prepare_debug_message(s, location);
}

void print_thread_id() {
std::ostringstream oss;

oss << std::this_thread::get_id() << '\n';

warning(std::format("thread id: {0}", oss.str()));
}

auto v4l2_find_device(const std::string& description) -> std::string {
for (const auto& entry : std::filesystem::directory_iterator("/dev/")) {
auto child_path = std::filesystem::path(entry);
Expand Down
2 changes: 2 additions & 0 deletions src/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ void warning(const std::string& s, source_location location = source_location::c

void info(const std::string& s, source_location location = source_location::current());

void print_thread_id();

auto v4l2_find_device(const std::string& description) -> std::string;

void v4l2_disable_dynamic_fps(const std::string& device_path);
Expand Down

0 comments on commit 975a551

Please sign in to comment.