forked from edman007/chiton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion_cvdetect.cpp
163 lines (145 loc) · 5.36 KB
/
motion_cvdetect.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**************************************************************************
*
* This file is part of Chiton.
*
* Chiton is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiton is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiton. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright 2022 Ed Martin <[email protected]>
*
**************************************************************************
*/
#include "motion_cvdetect.hpp"
#ifdef HAVE_OPENCV
#include "util.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/video/tracking.hpp>
static const std::string algo_name = "cvdetect";
MotionCVDetect::MotionCVDetect(Config &cfg, Database &db, MotionController &controller) : MotionAlgo(cfg, db, controller, algo_name) {
masked_objects = NULL;
ocv = NULL;
min_dist = cfg.get_value_double("motion-cvdetect-dist");
if (min_dist <= 0 || min_dist > 10000){
min_dist = 150.0;
}
min_dist *= min_dist;//multiply it here to skip the sq root later
min_area = cfg.get_value_double("motion-cvdetect-area");
if (min_area < 0){
min_area = 150.0;
}
tracking_time_send = cfg.get_value_int("motion-cvdetect-tracking");
if (tracking_time_send < 1){
tracking_time_send = 1;
}
}
MotionCVDetect::~MotionCVDetect(){
masked_objects = NULL;
}
bool MotionCVDetect::process_frame(const AVFrame *frame, bool video){
if (!video || !masked_objects){
return true;
}
//find contours
//float thresh = 150.0;
//cv::Canny(masked_objects->get_masked(), canny, thresh, thresh*2 );
//cv::findContours(canny, contours, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
cv::findContours(masked_objects->get_masked(), contours, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
std::vector<TargetRect> new_targets;
//compute the bounding rect for all
for (auto ctr : contours){
auto new_rect = cv::minAreaRect(ctr);
if (new_rect.size.area() < min_area){
continue;//toss the small spots
}
bool found_new = false;
for (auto &old_target : targets){
if (!old_target.is_valid()){
continue;
}
float x = old_target.get_rect().center.x - new_rect.center.x;
float y = old_target.get_rect().center.y - new_rect.center.y;
x *= x;
y *= y;
float dist = x + y;
if (dist < min_dist){
//too close, just replace this target with the new contour
new_targets.push_back(TargetRect(old_target, new_rect, frame));
old_target.mark_invalid();
found_new = true;
break;
}
}
if (!found_new){
new_targets.push_back(TargetRect(new_rect));
}
}
targets = std::move(new_targets);
if (targets.size() > 0){
LDEBUG("Tracking " + std::to_string(targets.size()) + " targets");
}
send_events();
display_objects();
return true;
}
bool MotionCVDetect::set_video_stream(const AVStream *stream, const AVCodecContext *codec) {
return true;
}
const std::string& MotionCVDetect::get_mod_name(void) {
return algo_name;
}
bool MotionCVDetect::init(void) {
ocv = static_cast<MotionOpenCV*>(controller.get_module_before("opencv", this));
masked_objects = static_cast<MotionCVMask*>(controller.get_module_before("cvmask", this));
return true;
}
void MotionCVDetect::display_objects(void){
//ocv->get_UMat().copyTo(debug_view);
masked_objects->get_masked().copyTo(debug_view);
cv::RNG rng(12345);
for (auto &target : targets){
if (target.get_count() < 10){
continue;
}
cv::Scalar color = cv::Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
cv::Point2f vertices[4];
target.get_rect().points(vertices);
for ( int j = 0; j < 4; j++ ){
line(debug_view, vertices[j], vertices[(j+1)%4], color);
}
LDEBUG("Size: " + std::to_string(target.get_rect().size.area()));
}
}
const cv::UMat &MotionCVDetect::get_debug_view(void){
return debug_view;
}
void MotionCVDetect::send_events(void){
for (auto &target : targets){
if (target.get_count() == tracking_time_send){
Event &e = controller.get_event_controller().get_new_event();
e.set_position(target);
struct timeval time;
controller.get_frame_timestamp(target.get_best_frame(), true, time);
e.set_frame(target.get_best_frame());
e.set_timestamp(time);
e.set_source(algo_name);
float score = target.get_best_rect().size.area();
float max_size = masked_objects->get_masked().cols*masked_objects->get_masked().rows;
score /= max_size;
score *= 100.0;
e.set_score(score);
controller.get_event_controller().send_event(e);
}
}
}
//HAVE_OPENCV
#endif