OpenShot Library | libopenshot 0.6.0
Loading...
Searching...
No Matches
CVTracker.cpp
Go to the documentation of this file.
1
10// Copyright (c) 2008-2019 OpenShot Studios, LLC
11//
12// SPDX-License-Identifier: LGPL-3.0-or-later
13
14#include <fstream>
15#include <iomanip>
16#include <iostream>
17#include <cmath>
18#include <algorithm>
19
20#include <google/protobuf/util/time_util.h>
21
22#include "OpenCVUtilities.h"
23#include "CVTracker.h"
24#include "trackerdata.pb.h"
25#include "Exceptions.h"
26
27using namespace openshot;
28using google::protobuf::util::TimeUtil;
29
30// Clamp a rectangle to image bounds and ensure a minimal size
31static inline void clampRect(cv::Rect2d &r, int width, int height)
32{
33 r.x = std::clamp(r.x, 0.0, double(width - 1));
34 r.y = std::clamp(r.y, 0.0, double(height - 1));
35 r.width = std::clamp(r.width, 1.0, double(width - r.x));
36 r.height = std::clamp(r.height, 1.0, double(height - r.y));
37}
38
39// Constructor
40CVTracker::CVTracker(std::string processInfoJson, ProcessingController &processingController)
41: processingController(&processingController), json_interval(false){
42 SetJson(processInfoJson);
43 start = 1;
44 end = 1;
45 lostCount = 0;
46}
47
48// Set desirable tracker method
49cv::Ptr<OPENCV_TRACKER_TYPE> CVTracker::selectTracker(std::string trackerType){
50
51 if (trackerType == "BOOSTING")
52 return OPENCV_TRACKER_NS::TrackerBoosting::create();
53 if (trackerType == "MIL")
54 return OPENCV_TRACKER_NS::TrackerMIL::create();
55 if (trackerType == "KCF")
56 return OPENCV_TRACKER_NS::TrackerKCF::create();
57 if (trackerType == "TLD")
58 return OPENCV_TRACKER_NS::TrackerTLD::create();
59 if (trackerType == "MEDIANFLOW")
60 return OPENCV_TRACKER_NS::TrackerMedianFlow::create();
61 if (trackerType == "MOSSE")
62 return OPENCV_TRACKER_NS::TrackerMOSSE::create();
63 if (trackerType == "CSRT")
64 return OPENCV_TRACKER_NS::TrackerCSRT::create();
65
66 return nullptr;
67}
68
69// Track object in the whole clip or in a given interval
71 size_t _start,
72 size_t _end,
73 bool process_interval)
74{
75 video.Open();
76 if (!json_interval) {
77 start = _start; end = _end;
78 if (!process_interval || end <= 1 || end - start == 0) {
79 start = int(video.Start() * video.Reader()->info.fps.ToFloat()) + 1;
80 end = int(video.End() * video.Reader()->info.fps.ToFloat()) + 1;
81 }
82 } else {
83 start = int(start + video.Start() * video.Reader()->info.fps.ToFloat()) + 1;
84 end = int(video.End() * video.Reader()->info.fps.ToFloat()) + 1;
85 }
86 if (error) return;
87 processingController->SetError(false, "");
88
89 bool trackerInit = false;
90 lostCount = 0; // reset lost counter once at the start
91
92 for (size_t frame = start; frame <= end; ++frame) {
93 if (processingController->ShouldStop()) return;
94
95 auto f = video.GetFrame(frame);
96 cv::Mat img = f->GetImageCV();
97
98 if (frame == start) {
99 bbox = cv::Rect2d(
100 int(bbox.x * img.cols),
101 int(bbox.y * img.rows),
102 int(bbox.width * img.cols),
103 int(bbox.height * img.rows)
104 );
105 }
106
107 if (!trackerInit) {
108 initTracker(img, frame);
109 trackerInit = true;
110 lostCount = 0;
111 }
112 else {
113 // trackFrame now manages lostCount and will re-init internally
114 trackFrame(img, frame);
115
116 // record whatever bbox we have now
117 FrameData fd = GetTrackedData(frame);
118 }
119
120 processingController->SetProgress(
121 uint(100 * (frame - start) / (end - start))
122 );
123 }
124}
125
126// Initialize the tracker
127bool CVTracker::initTracker(cv::Mat &frame, size_t frameId)
128{
129 // Create new tracker object
130 tracker = selectTracker(trackerType);
131
132 // Correct negative width/height
133 if (bbox.width < 0) {
134 bbox.x -= bbox.width;
135 bbox.width = -bbox.width;
136 }
137 if (bbox.height < 0) {
138 bbox.y -= bbox.height;
139 bbox.height = -bbox.height;
140 }
141
142 // Clamp to frame bounds
143 clampRect(bbox, frame.cols, frame.rows);
144
145 // Initialize tracker
146 tracker->init(frame, bbox);
147
148 float fw = float(frame.cols), fh = float(frame.rows);
149
150 // record original pixel size
151 origWidth = bbox.width;
152 origHeight = bbox.height;
153
154 // initialize sub-pixel smoother at true center
155 smoothC_x = bbox.x + bbox.width * 0.5;
156 smoothC_y = bbox.y + bbox.height * 0.5;
157
158 // Add new frame data
159 trackedDataById[frameId] = FrameData(
160 frameId, 0,
161 bbox.x / fw,
162 bbox.y / fh,
163 (bbox.x + bbox.width) / fw,
164 (bbox.y + bbox.height) / fh
165 );
166
167 return true;
168}
169
170// Update the object tracker according to frame
171// returns true if KLT succeeded, false otherwise
172bool CVTracker::trackFrame(cv::Mat &frame, size_t frameId)
173{
174 const int W = frame.cols, H = frame.rows;
175 const auto& prev = trackedDataById[frameId - 1];
176
177 // Reconstruct last-known box in pixel coords
178 cv::Rect2d lastBox(
179 prev.x1 * W, prev.y1 * H,
180 (prev.x2 - prev.x1) * W,
181 (prev.y2 - prev.y1) * H
182 );
183
184 // Convert to grayscale
185 cv::Mat gray;
186 cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
187
188 const bool prevGrayMatches =
189 !prevGray.empty() &&
190 prevGray.size() == gray.size() &&
191 prevGray.type() == gray.type();
192 const bool fullPrevGrayMatches =
193 !fullPrevGray.empty() &&
194 fullPrevGray.size() == gray.size() &&
195 fullPrevGray.type() == gray.type();
196
197 if (!prevGray.empty() && !prevGrayMatches) {
198 prevPts.clear();
199 lostCount = 0;
200 }
201
202 cv::Rect2d cand;
203 bool didKLT = false;
204
205 // Try KLT-based drift
206 if (prevGrayMatches && !prevPts.empty()) {
207 std::vector<cv::Point2f> currPts;
208 std::vector<uchar> status;
209 std::vector<float> err;
210 cv::calcOpticalFlowPyrLK(
211 prevGray, gray,
212 prevPts, currPts,
213 status, err,
214 cv::Size(21,21), 3,
215 cv::TermCriteria{cv::TermCriteria::COUNT|cv::TermCriteria::EPS,30,0.01},
216 cv::OPTFLOW_LK_GET_MIN_EIGENVALS, 1e-4
217 );
218
219 // collect per-point displacements
220 std::vector<double> dx, dy;
221 for (size_t i = 0; i < status.size(); ++i) {
222 if (status[i] && err[i] < 12.0) {
223 dx.push_back(currPts[i].x - prevPts[i].x);
224 dy.push_back(currPts[i].y - prevPts[i].y);
225 }
226 }
227
228 if ((int)dx.size() >= minKltPts) {
229 auto median = [&](auto &v){
230 std::nth_element(v.begin(), v.begin()+v.size()/2, v.end());
231 return v[v.size()/2];
232 };
233 double mdx = median(dx), mdy = median(dy);
234
235 cand = lastBox;
236 cand.x += mdx;
237 cand.y += mdy;
238 cand.width = origWidth;
239 cand.height = origHeight;
240
241 lostCount = 0;
242 didKLT = true;
243 }
244 }
245
246 // Fallback to whole-frame flow if KLT failed
247 if (!didKLT) {
248 ++lostCount;
249 cand = lastBox;
250 if (fullPrevGrayMatches) {
251 cv::Mat flow;
252 cv::calcOpticalFlowFarneback(
253 fullPrevGray, gray, flow,
254 0.5,3,15,3,5,1.2,0
255 );
256 cv::Scalar avg = cv::mean(flow);
257 cand.x += avg[0];
258 cand.y += avg[1];
259 }
260 cand.width = origWidth;
261 cand.height = origHeight;
262
263 if (lostCount >= 10) {
264 initTracker(frame, frameId);
265 cand = bbox;
266 lostCount = 0;
267 }
268 }
269
270 // Dead-zone sub-pixel smoothing
271 {
272 constexpr double JITTER_THRESH = 1.0;
273 double measCx = cand.x + cand.width * 0.5;
274 double measCy = cand.y + cand.height * 0.5;
275 double dx = measCx - smoothC_x;
276 double dy = measCy - smoothC_y;
277
278 if (std::abs(dx) > JITTER_THRESH || std::abs(dy) > JITTER_THRESH) {
279 smoothC_x = measCx;
280 smoothC_y = measCy;
281 }
282
283 cand.x = smoothC_x - cand.width * 0.5;
284 cand.y = smoothC_y - cand.height * 0.5;
285 }
286
287
288 // Candidate box may now lie outside frame; ROI for KLT is clamped below
289 // Re-seed KLT features
290 {
291 // Clamp ROI to frame bounds and avoid negative width/height
292 int roiX = int(std::clamp(cand.x, 0.0, double(W - 1)));
293 int roiY = int(std::clamp(cand.y, 0.0, double(H - 1)));
294 int roiW = int(std::min(cand.width, double(W - roiX)));
295 int roiH = int(std::min(cand.height, double(H - roiY)));
296 roiW = std::max(0, roiW);
297 roiH = std::max(0, roiH);
298
299 if (roiW > 0 && roiH > 0) {
300 cv::Rect roi(roiX, roiY, roiW, roiH);
301 cv::goodFeaturesToTrack(
302 gray(roi), prevPts,
303 kltMaxCorners, kltQualityLevel,
304 kltMinDist, cv::Mat(), kltBlockSize
305 );
306 for (auto &pt : prevPts)
307 pt += cv::Point2f(float(roi.x), float(roi.y));
308 } else {
309 prevPts.clear();
310 }
311 }
312
313 // Commit state
314 fullPrevGray = gray.clone();
315 prevGray = gray.clone();
316 bbox = cand;
317 float fw = float(W), fh = float(H);
318 trackedDataById[frameId] = FrameData(
319 frameId, 0,
320 cand.x / fw,
321 cand.y / fh,
322 (cand.x + cand.width) / fw,
323 (cand.y + cand.height) / fh
324 );
325
326 return didKLT;
327}
328
330 using std::ios;
331
332 // Create tracker message
333 pb_tracker::Tracker trackerMessage;
334
335 // Iterate over all frames data and save in protobuf message
336 for(std::map<size_t,FrameData>::iterator it=trackedDataById.begin(); it!=trackedDataById.end(); ++it){
337 FrameData fData = it->second;
338 pb_tracker::Frame* pbFrameData;
339 AddFrameDataToProto(trackerMessage.add_frame(), fData);
340 }
341
342 // Add timestamp
343 *trackerMessage.mutable_last_updated() = TimeUtil::SecondsToTimestamp(time(NULL));
344
345 {
346 // Write the new message to disk.
347 std::fstream output(protobuf_data_path, ios::out | ios::trunc | ios::binary);
348 if (!trackerMessage.SerializeToOstream(&output)) {
349 std::cerr << "Failed to write protobuf message." << std::endl;
350 return false;
351 }
352 }
353
354 // Delete all global objects allocated by libprotobuf.
355 google::protobuf::ShutdownProtobufLibrary();
356
357 return true;
358
359}
360
361// Add frame tracked data into protobuf message.
362void CVTracker::AddFrameDataToProto(pb_tracker::Frame* pbFrameData, FrameData& fData) {
363
364 // Save frame number and rotation
365 pbFrameData->set_id(fData.frame_id);
366 pbFrameData->set_rotation(0);
367
368 pb_tracker::Frame::Box* box = pbFrameData->mutable_bounding_box();
369 // Save bounding box data
370 box->set_x1(fData.x1);
371 box->set_y1(fData.y1);
372 box->set_x2(fData.x2);
373 box->set_y2(fData.y2);
374}
375
376// Get tracker info for the desired frame
378
379 // Check if the tracker info for the requested frame exists
380 if ( trackedDataById.find(frameId) == trackedDataById.end() ) {
381
382 return FrameData();
383 } else {
384
385 return trackedDataById[frameId];
386 }
387
388}
389
390// Load JSON string into this object
391void CVTracker::SetJson(const std::string value) {
392 // Parse JSON string into JSON objects
393 try
394 {
395 const Json::Value root = openshot::stringToJson(value);
396 // Set all values that match
397
398 SetJsonValue(root);
399 }
400 catch (const std::exception& e)
401 {
402 // Error parsing JSON (or missing keys)
403 throw openshot::InvalidJSON("JSON is invalid (missing keys or invalid data types)");
404 }
405}
406
407// Load Json::Value into this object
408void CVTracker::SetJsonValue(const Json::Value root) {
409
410 // Set data from Json (if key is found)
411 if (!root["protobuf_data_path"].isNull()){
412 protobuf_data_path = (root["protobuf_data_path"].asString());
413 }
414 if (!root["tracker-type"].isNull()){
415 trackerType = (root["tracker-type"].asString());
416 }
417
418 if (!root["region"].isNull()){
419 double x = root["region"]["normalized_x"].asDouble();
420 double y = root["region"]["normalized_y"].asDouble();
421 double w = root["region"]["normalized_width"].asDouble();
422 double h = root["region"]["normalized_height"].asDouble();
423 cv::Rect2d prev_bbox(x,y,w,h);
424 bbox = prev_bbox;
425
426 if (!root["region"]["first-frame"].isNull()){
427 start = root["region"]["first-frame"].asInt64();
428 json_interval = true;
429 }
430 else{
431 processingController->SetError(true, "No first-frame");
432 error = true;
433 }
434
435 }
436 else{
437 processingController->SetError(true, "No initial bounding box selected");
438 error = true;
439 }
440
441}
442
443/*
444||||||||||||||||||||||||||||||||||||||||||||||||||
445 ONLY FOR MAKE TEST
446||||||||||||||||||||||||||||||||||||||||||||||||||
447*/
448
449// Load protobuf data file
451 using std::ios;
452
453 // Create tracker message
454 pb_tracker::Tracker trackerMessage;
455
456 {
457 // Read the existing tracker message.
458 std::fstream input(protobuf_data_path, ios::in | ios::binary);
459 if (!trackerMessage.ParseFromIstream(&input)) {
460 std::cerr << "Failed to parse protobuf message." << std::endl;
461 return false;
462 }
463 }
464
465 // Make sure the trackedData is empty
466 trackedDataById.clear();
467
468 // Iterate over all frames of the saved message
469 for (size_t i = 0; i < trackerMessage.frame_size(); i++) {
470 const pb_tracker::Frame& pbFrameData = trackerMessage.frame(i);
471
472 // Load frame and rotation data
473 size_t id = pbFrameData.id();
474 float rotation = pbFrameData.rotation();
475
476 // Load bounding box data
477 const pb_tracker::Frame::Box& box = pbFrameData.bounding_box();
478 float x1 = box.x1();
479 float y1 = box.y1();
480 float x2 = box.x2();
481 float y2 = box.y2();
482
483 // Assign data to tracker map
484 trackedDataById[id] = FrameData(id, rotation, x1, y1, x2, y2);
485 }
486
487 // Delete all global objects allocated by libprotobuf.
488 google::protobuf::ShutdownProtobufLibrary();
489
490 return true;
491}
Track an object selected by the user.
Header file for all Exception classes.
Header file for OpenCVUtilities (set some common macros)
void SetError(bool err, std::string message)
void trackClip(openshot::Clip &video, size_t _start=0, size_t _end=0, bool process_interval=false)
Definition CVTracker.cpp:70
CVTracker(std::string processInfoJson, ProcessingController &processingController)
Definition CVTracker.cpp:40
bool SaveTrackedData()
Save protobuf file.
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
void SetJson(const std::string value)
Load JSON string into this object.
bool trackFrame(cv::Mat &frame, size_t frameId)
void AddFrameDataToProto(pb_tracker::Frame *pbFrameData, FrameData &fData)
Add frame tracked data into protobuf message.
FrameData GetTrackedData(size_t frameId)
Get tracked data for a given frame.
cv::Ptr< OPENCV_TRACKER_TYPE > selectTracker(std::string trackerType)
Definition CVTracker.cpp:49
bool initTracker(cv::Mat &frame, size_t frameId)
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition ClipBase.h:88
This class represents a clip (used to arrange readers on the timeline)
Definition Clip.h:89
void Open() override
Open the internal reader.
Definition Clip.cpp:384
float End() const override
Get end position (in seconds) of clip (trim end of video), which can be affected by the time curve.
Definition Clip.cpp:420
std::shared_ptr< openshot::Frame > GetFrame(int64_t clip_frame_number) override
Get an openshot::Frame object for a specific frame number of this clip. The image size and number of ...
Definition Clip.cpp:455
void Reader(openshot::ReaderBase *new_reader)
Set the current reader.
Definition Clip.cpp:338
Exception for invalid JSON.
Definition Exceptions.h:224
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16