Skip to content

Instantly share code, notes, and snippets.

@nariakiiwatani
Last active June 20, 2021 17:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nariakiiwatani/1b6197de62feb13b1a61508f8467c5e8 to your computer and use it in GitHub Desktop.
Save nariakiiwatani/1b6197de62feb13b1a61508f8467c5e8 to your computer and use it in GitHub Desktop.
// Copyright 2020 nariakiiwatani
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#pragma once
#include <vector>
#include <map>
#include "ofAppRunner.h"
template<typename Trackee, typename Result = Trackee>
class Tracker
{
public:
using id_type = std::size_t;
using trackee_type = Trackee;
using result_type = Result;
int getNumTrackee() const { return exists_.size(); }
void track(const std::vector<Trackee> &trackee) {
using namespace std;
if(exists_.empty()) {
for(auto &&p : trackee) {
exists_.insert(make_pair(next_id_++, newResult(p)));
}
return;
}
multimap<float, pair<id_type, id_type>> score_map;
vector<id_type> unused_trackee;
for(int i = 0; i < trackee.size(); ++i) {
for(auto &&p : exists_) {
float score = calcIdentityScore(p.second, trackee[i]);
score_map.insert(make_pair(score, make_pair(p.first, i)));
}
unused_trackee.push_back(i);
}
std::vector<id_type> exists, absents;
for(auto &&p : exists_) {
absents.push_back(p.first);
}
while(!score_map.empty()) {
auto primary = begin(score_map);
id_type id0 = primary->second.first;
id_type id1 = primary->second.second;
if(primary->first > 1) {
id0 = next_id_++;
exists_[id0] = newResult(trackee[id1]);
}
else {
updateResult(exists_[id0], trackee[id1]);
}
exists.push_back(id0);
{
auto it = find(begin(absents), end(absents), id0);
if(it != end(absents)) {
absents.erase(it);
}
}
for(auto it = begin(score_map); it != end(score_map);) {
if(it->second.first == id0 || it->second.second == id1) {
it = score_map.erase(it);
}
else {
++it;
}
}
unused_trackee.erase(find(begin(unused_trackee), end(unused_trackee), id1));
}
for(auto &&id1 : unused_trackee) {
id_type id0 = next_id_++;
exists_[id0] = newResult(trackee[id1]);
}
for(auto &&e : exists) {
absents_.erase(e);
}
for(auto &&a : absents) {
absents_.insert(make_pair(a, 0));
updateWhileAbsent(exists_[a]);
}
}
void update() {
using namespace std;
float frame_time = ofGetLastFrameTime();
for(auto it = begin(absents_); it != end(absents_);) {
if((it->second += frame_time) > persistence_) {
exists_.erase(it->first);
it = absents_.erase(it);
}
else {
++it;
}
}
}
const std::map<id_type, Result>& getExists() const { return exists_; }
const std::map<id_type, float>& getAbsents() const { return absents_; }
float getPersistence() const { return persistence_; }
void setPersistence(float persistence) { persistence_ = persistence; }
protected:
virtual float calcIdentityScore(const Result &a, const Trackee &b)=0;
virtual Result newResult(const Trackee &t)=0;
virtual void updateResult(Result &dst, const Trackee &t)=0;
virtual void updateWhileAbsent(Result &dst){}
protected:
id_type next_id_=0;
std::map<id_type, Result> exists_;
std::map<id_type, float> absents_;
float persistence_=1;
};
#include "ofRectangle.h"
class RectTracker : public Tracker<ofRectangle>
{
protected:
float calcIdentityScore(const result_type &a, const trackee_type &b) {
using namespace std;
float position_distance = glm::distance(a.getCenter(), b.getCenter());
float position_score = position_threshold_ == 0
? position_distance == 0 ? 0 : 1
: ofMap(position_distance, 0, position_threshold_, 0, 1, false);
float size_distance = max(abs(a.getWidth()-b.getWidth()), abs(a.getHeight()-b.getHeight()));
float size_score = size_threshold_ == 0
? size_distance == 0 ? 0 : 1
: ofMap(size_distance, 0, size_threshold_, 0, 1, false);
return max(position_score, size_score);
};
result_type newResult(const trackee_type &t) { return t; }
void updateResult(result_type &dst, const trackee_type &t) { dst = t; }
protected:
float position_threshold_=100;
float size_threshold_=100;
};
template<typename Trackee>
struct TrackeeInfo {
using trackee_type = Trackee;
// possible state list:=> exist(is_exist==true), lost(is_exist==false)
// additional flags:=>
// new_seen(first frame in exist but not for re-seen)
// seen(first frame in exist including re-seen)
// new_lost(first frame in lost including re-lost)
// new_dead(last frame of lost. this object is going to disappear in next frame)
bool is_exist;
bool is_new_seen;
bool is_seen;
bool is_new_lost;
bool is_dead;
// age:=> elapsed time after seen at first time. keeps counting up until it's lost.
float age;
// age_after_lost:=> elapsed time after lost. resets everytime it's lost.
float age_after_lost;
// object:=> trackee itself
Trackee object, object_prev;
TrackeeInfo() = default;
virtual void updateForNew(const Trackee &t) {
is_exist = true;
is_new_seen =
is_seen = true;
is_new_lost =
is_dead = false;
age =
age_after_lost = 0;
object =
object_prev = t;
}
virtual void updateForExist(float elapsed_time, const Trackee &t) {
is_seen = !is_exist;
is_exist = true;
is_new_seen =
is_new_lost =
is_dead = false;
age += elapsed_time;
age_after_lost = 0;
object_prev = object;
object = t;
}
virtual void updateForLost(float elapsed_time, const Trackee &t) {
is_new_lost = is_exist;
is_exist =
is_new_seen =
is_seen =
is_dead = false;
age += elapsed_time;
age_after_lost += elapsed_time;
object_prev = object;
object = t;
}
};
template<typename Tracker, typename Info = TrackeeInfo<typename Tracker::trackee_type>>
class TrackerAdditionalInfo
{
public:
using info_type = Info;
void update(const Tracker &tracker) {
using namespace std;
for(auto it = begin(info_); it != end(info_);) {
if(it->second.is_dead) {
it = info_.erase(it);
}
else {
it->second.is_dead = true;
++it;
}
}
float frame_time = ofGetLastFrameTime();
auto exists = tracker.getExists();
auto absents = tracker.getAbsents();
for(auto &&obj : exists) {
auto found = info_.find(obj.first);
if(found == end(info_)) {
Info info;
info.updateForNew(obj.second);
info_.insert(make_pair(obj.first, info));
}
else if(absents.find(obj.first) == end(absents)) {
found->second.updateForExist(frame_time, obj.second);
}
else {
found->second.updateForLost(frame_time, obj.second);
}
}
}
const std::map<typename Tracker::id_type, Info>& getInfo() const { return info_; }
private:
std::map<typename Tracker::id_type, Info> info_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment