-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdet_and_track.cpp
99 lines (87 loc) · 3.02 KB
/
det_and_track.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include "det_and_track.h"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/imgproc/imgproc.hpp"
DetAndTrack::DetAndTrack():
get_new_detection_(false),
first_time_detection_(true)
{
//face_cascade_.load("../haarcascade_frontalface_alt.xml");
detection_sleep_time_ = 300;
track_sleep_time_ = 30;
object_detection_ptr_ = new ObjectDetection();
}
DetAndTrack::DetAndTrack(int _detection_sleep_time, int _track_sleep_time):
get_new_detection_(false),
first_time_detection_(true),
detection_sleep_time_(_detection_sleep_time),
track_sleep_time_(_track_sleep_time)
{
//face_cascade_.load("../haarcascade_frontalface_alt.xml");
object_detection_ptr_ = new ObjectDetection();
}
void DetAndTrack::detectionTask()
{
while(1)
{
std::cout << "detecting.." << std::endl;
mutex_.lock();
cv::Mat local_frame = current_frame_.clone();
mutex_.unlock();
std::vector<cv::Rect> local_boxes = object_detection_ptr_->detectObject(local_frame);
//face_cascade_.detectMultiScale(local_frame, local_boxes, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30,30));
std::this_thread::sleep_for(std::chrono::milliseconds(detection_sleep_time_));
std::lock_guard<std::mutex> lockGuard(mutex_);
det_boxes_ = local_boxes;
//final_boxes_ = local_boxes;
get_new_detection_ = true;
std::cout << "detecting done" << std::endl;
}
}
void DetAndTrack::trackTask()
{
while(1)
{
std::cout << "tracking..." << std::endl;
mutex_.lock();
cv::Mat local_frame = current_frame_.clone();
mutex_.unlock();
if(get_new_detection_)
{
mutex_.lock();
std::vector<cv::Rect> local_det_boxes = det_boxes_;
mutex_.unlock();
if(first_time_detection_)
{
track_manager_ptr_ = new TrackerManager(local_frame, local_det_boxes);
first_time_detection_ = false;
std::cout << "track manager initialized" << std::endl;
}
else
{
std::cout << "track start update with new detection..." << std::endl;
track_manager_ptr_ -> updateTrackersWithNewDetectionResults(local_det_boxes);
}
std::lock_guard<std::mutex> lockguard(mutex_);
get_new_detection_ = false;
}
else
{
if(!first_time_detection_)
{
std::cout << "track start update with new frame..." << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(track_sleep_time_));
track_manager_ptr_->updateTrackersWithNewFrame(local_frame);
}
else
{
std::this_thread::sleep_for(std::chrono::milliseconds(30));
}
}
std::cout << "tracking done" << std::endl;
}
}
void DetAndTrack::setFrame(const cv::Mat& _new_frame)
{
current_frame_ = _new_frame.clone();
}