Loading...
Searching...
No Matches
Tracker.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <string>
#include <memory>
#include <iostream>
#include <algorithm>
#include "effects/Tracker.h"
#include "Exceptions.h"
#include "Timeline.h"
#include "trackerdata.pb.h"
#include <google/protobuf/util/time_util.h>
#include <QImage>
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QRectF>
using namespace std;
using namespace openshot;
using google::protobuf::util::TimeUtil;
// Default constructor
Tracker::Tracker()
{
// Initialize effect metadata
init_effect_details();
// Create a placeholder object so we always have index 0 available
trackedData = std::make_shared<TrackedObjectBBox>();
trackedData->ParentClip(this->ParentClip());
// Seed our map with a single entry at index 0
trackedObjects.clear();
trackedObjects.emplace(0, trackedData);
// Assign ID to the placeholder object
if (trackedData)
trackedData->Id(Id() + "-0");
}
// Init effect settings
void Tracker::init_effect_details()
{
InitEffectInfo();
info.class_name = "Tracker";
info.name = "Tracker";
info.description = "Track the selected bounding box through the video.";
info.has_audio = false;
info.has_video = true;
info.has_tracked_object = true;
this->TimeScale = 1.0;
}
// This method is required for all derived classes of EffectBase, and returns a
// modified openshot::Frame object
std::shared_ptr<Frame> Tracker::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
{
// Sanity‐check
if (!frame) return frame;
auto frame_image = frame->GetImage();
if (!frame_image || frame_image->isNull()) return frame;
if (!trackedData) return frame;
// 2) Only proceed if we actually have a box and it's visible
if (!trackedData->Contains(frame_number) ||
trackedData->visible.GetValue(frame_number) != 1)
return frame;
QPainter painter(frame_image.get());
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
// Draw the box
BBox fd = trackedData->GetBox(frame_number);
QRectF boxRect(
(fd.cx - fd.width/2) * frame_image->width(),
(fd.cy - fd.height/2) * frame_image->height(),
fd.width * frame_image->width(),
fd.height * frame_image->height()
);
if (trackedData->draw_box.GetValue(frame_number) == 1)
{
auto stroke_rgba = trackedData->stroke.GetColorRGBA(frame_number);
int stroke_width = trackedData->stroke_width.GetValue(frame_number);
float stroke_alpha = trackedData->stroke_alpha.GetValue(frame_number);
auto bg_rgba = trackedData->background.GetColorRGBA(frame_number);
float bg_alpha = trackedData->background_alpha.GetValue(frame_number);
float bg_corner = trackedData->background_corner.GetValue(frame_number);
QPen pen(QColor(
stroke_rgba[0], stroke_rgba[1], stroke_rgba[2],
int(255 * stroke_alpha)
));
pen.setWidth(stroke_width);
painter.setPen(pen);
QBrush brush(QColor(
bg_rgba[0], bg_rgba[1], bg_rgba[2],
int(255 * bg_alpha)
));
painter.setBrush(brush);
painter.drawRoundedRect(boxRect, bg_corner, bg_corner);
}
painter.end();
return frame;
}
// Get the indexes and IDs of all visible objects in the given frame
std::string Tracker::GetVisibleObjects(int64_t frame_number) const
{
Json::Value root;
root["visible_objects_index"] = Json::Value(Json::arrayValue);
root["visible_objects_id"] = Json::Value(Json::arrayValue);
if (trackedObjects.empty())
return root.toStyledString();
for (auto const& kv : trackedObjects) {
auto ptr = kv.second;
if (!ptr) continue;
// Directly get the Json::Value for this object's properties
Json::Value propsJson = ptr->PropertiesJSON(frame_number);
if (propsJson["visible"]["value"].asBool()) {
root["visible_objects_index"].append(kv.first);
root["visible_objects_id"].append(ptr->Id());
}
}
return root.toStyledString();
}
// Generate JSON string of this object
std::string Tracker::Json() const {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::Value for this object
Json::Value Tracker::JsonValue() const {
// Create root json object
Json::Value root = EffectBase::JsonValue(); // get parent properties
// Save the effect's properties on root
root["type"] = info.class_name;
root["protobuf_data_path"] = protobuf_data_path;
root["BaseFPS"]["num"] = BaseFPS.num;
root["BaseFPS"]["den"] = BaseFPS.den;
root["TimeScale"] = this->TimeScale;
// Add trackedObjects IDs to JSON
Json::Value objects;
for (auto const& trackedObject : trackedObjects){
Json::Value trackedObjectJSON = trackedObject.second->JsonValue();
// add object json
objects[trackedObject.second->Id()] = trackedObjectJSON;
}
root["objects"] = objects;
// return JsonValue
return root;
}
// Load JSON string into this object
void Tracker::SetJson(const std::string value) {
// Parse JSON string into JSON objects
try
{
const Json::Value root = openshot::stringToJson(value);
// Set all values that match
SetJsonValue(root);
}
catch (const std::exception& e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
}
return;
}
// Load Json::Value into this object
void Tracker::SetJsonValue(const Json::Value root) {
// Set parent data
EffectBase::SetJsonValue(root);
if (!root["BaseFPS"].isNull()) {
if (!root["BaseFPS"]["num"].isNull())
BaseFPS.num = root["BaseFPS"]["num"].asInt();
if (!root["BaseFPS"]["den"].isNull())
BaseFPS.den = root["BaseFPS"]["den"].asInt();
}
if (!root["TimeScale"].isNull()) {
TimeScale = root["TimeScale"].asDouble();
}
if (!root["protobuf_data_path"].isNull()) {
std::string new_path = root["protobuf_data_path"].asString();
if (protobuf_data_path != new_path || trackedData->GetLength() == 0) {
protobuf_data_path = new_path;
if (!trackedData->LoadBoxData(protobuf_data_path)) {
std::clog << "Invalid protobuf data path " << protobuf_data_path << '\n';
protobuf_data_path.clear();
}
else {
// prefix "<effectUUID>-<index>" for each entry
for (auto& kv : trackedObjects) {
auto idx = kv.first;
auto ptr = kv.second;
if (ptr) {
std::string prefix = this->Id();
if (!prefix.empty())
prefix += "-";
ptr->Id(prefix + std::to_string(idx));
}
}
}
}
}
// then any per-object JSON overrides...
if (!root["objects"].isNull()) {
// Iterate over the supplied objects (indexed by id or position)
const auto memberNames = root["objects"].getMemberNames();
for (const auto& name : memberNames)
{
// Determine the numeric index of this object
int index = -1;
bool numeric_key = std::all_of(name.begin(), name.end(), ::isdigit);
if (numeric_key) {
index = std::stoi(name);
}
else
{
size_t pos = name.find_last_of('-');
if (pos != std::string::npos) {
try {
index = std::stoi(name.substr(pos + 1));
} catch (...) {
index = -1;
}
}
}
auto obj_it = trackedObjects.find(index);
if (obj_it != trackedObjects.end() && obj_it->second) {
// Update object id if provided as a non-numeric key
if (!numeric_key)
obj_it->second->Id(name);
obj_it->second->SetJsonValue(root["objects"][name]);
}
}
}
// Set the tracked object's ids (legacy format)
if (!root["objects_id"].isNull()) {
for (auto& kv : trackedObjects) {
if (!root["objects_id"][kv.first].isNull())
kv.second->Id(root["objects_id"][kv.first].asString());
}
}
}
// Get all properties for a specific frame
std::string Tracker::PropertiesJSON(int64_t requested_frame) const {
// Generate JSON properties list
Json::Value root = BasePropertiesJSON(requested_frame);
// Add trackedObject properties to JSON
Json::Value objects;
for (auto const& trackedObject : trackedObjects){
Json::Value trackedObjectJSON = trackedObject.second->PropertiesJSON(requested_frame);
// add object json
objects[trackedObject.second->Id()] = trackedObjectJSON;
}
root["objects"] = objects;
// Return formatted string
return root.toStyledString();
}
Exceptions.h
Header file for all Exception classes.
Timeline.h
Header file for Timeline class.
Tracker.h
Header file for Tracker effect class.
openshot::ClipBase::Id
std::string Id() const
Get the Id of this clip object.
Definition ClipBase.h:85
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition EffectBase.cpp:79
openshot::EffectBase::ParentClip
openshot::ClipBase * ParentClip()
Parent clip object of this effect (which can be unparented and NULL).
Definition EffectBase.cpp:201
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition EffectBase.cpp:24
openshot::EffectBase::BasePropertiesJSON
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects).
Definition EffectBase.cpp:179
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition EffectBase.cpp:115
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition EffectBase.h:69
openshot::EffectBase::trackedObjects
std::map< int, std::shared_ptr< openshot::TrackedObjectBase > > trackedObjects
Map of Tracked Object's by their indices (used by Effects that track objects on clips).
Definition EffectBase.h:66
openshot::InvalidJSON
Exception for invalid JSON.
Definition Exceptions.h:218
openshot::Tracker::Json
std::string Json() const override
Generate JSON string of this object.
Definition Tracker.cpp:156
openshot::Tracker::GetVisibleObjects
std::string GetVisibleObjects(int64_t frame_number) const override
Get the indexes and IDs of all visible objects in the given frame.
Definition Tracker.cpp:130
openshot::Tracker::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition Tracker.cpp:163
openshot::Tracker::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition Tracker.cpp:189
openshot::Tracker::GetFrame
std::shared_ptr< Frame > GetFrame(std::shared_ptr< Frame > frame, int64_t frame_number) override
Apply this effect to an openshot::Frame.
Definition Tracker.cpp:75
openshot::Tracker::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition Tracker.cpp:291
openshot::Tracker::trackedData
std::shared_ptr< TrackedObjectBBox > trackedData
Pointer to an object that holds the bounding-box data and it's Keyframes.
Definition Tracker.h:52
openshot::Tracker::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition Tracker.cpp:207
openshot::Tracker::Tracker
Tracker()
Default constructor.
Definition Tracker.cpp:38
openshot::Tracker::protobuf_data_path
std::string protobuf_data_path
Path to the protobuf file that holds the bounding-box data.
Definition Tracker.h:51
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
openshot::BBox
This struct holds the information of a bounding-box.
Definition TrackedObjectBBox.h:38
openshot::BBox::cy
float cy
y-coordinate of the bounding box center
Definition TrackedObjectBBox.h:40
openshot::BBox::height
float height
bounding box height
Definition TrackedObjectBBox.h:42
openshot::BBox::cx
float cx
x-coordinate of the bounding box center
Definition TrackedObjectBBox.h:39
openshot::BBox::width
float width
bounding box width
Definition TrackedObjectBBox.h:41
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition EffectBase.h:40
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition EffectBase.h:41
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition EffectBase.h:36
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition EffectBase.h:37
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition EffectBase.h:38
openshot::EffectInfoStruct::has_tracked_object
bool has_tracked_object
Determines if this effect track objects through the clip.
Definition EffectBase.h:42