21#include "trackerdata.pb.h"
23#include <google/protobuf/util/time_util.h>
33using google::protobuf::util::TimeUtil;
39 init_effect_details();
42 trackedData = std::make_shared<TrackedObjectBBox>(trackedDataObject);
56 init_effect_details();
59 trackedData = std::make_shared<TrackedObjectBBox>(trackedDataObject);
69void Tracker::init_effect_details()
77 info.
description =
"Track the selected bounding box through the video.";
82 this->TimeScale = 1.0;
87std::shared_ptr<Frame>
Tracker::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number) {
89 std::shared_ptr<QImage> frame_image = frame->GetImage();
92 if(frame_image && !frame_image->isNull() &&
95 QPainter painter(frame_image.get());
101 QRectF boxRect((fd.
cx - fd.
width / 2) * frame_image->width(),
102 (fd.
cy - fd.
height / 2) * frame_image->height(),
103 fd.
width * frame_image->width(),
104 fd.
height * frame_image->height());
107 if (
trackedData->draw_box.GetValue(frame_number) == 1) {
108 painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
111 std::vector<int> stroke_rgba =
trackedData->stroke.GetColorRGBA(frame_number);
112 int stroke_width =
trackedData->stroke_width.GetValue(frame_number);
113 float stroke_alpha =
trackedData->stroke_alpha.GetValue(frame_number);
114 std::vector<int> bg_rgba =
trackedData->background.GetColorRGBA(frame_number);
115 float bg_alpha =
trackedData->background_alpha.GetValue(frame_number);
116 float bg_corner =
trackedData->background_corner.GetValue(frame_number);
119 QPen pen(QColor(stroke_rgba[0], stroke_rgba[1], stroke_rgba[2], 255 * stroke_alpha));
120 pen.setWidth(stroke_width);
124 QBrush brush(QColor(bg_rgba[0], bg_rgba[1], bg_rgba[2], 255 * bg_alpha));
125 painter.setBrush(brush);
128 painter.drawRoundedRect(boxRect, bg_corner, bg_corner);
143 root[
"visible_objects_index"] = Json::Value(Json::arrayValue);
144 root[
"visible_objects_id"] = Json::Value(Json::arrayValue);
149 Json::Value trackedObjectJSON = trackedObject.second->PropertiesJSON(frame_number);
150 if (trackedObjectJSON[
"visible"][
"value"].asBool()){
152 root[
"visible_objects_index"].append(trackedObject.first);
153 root[
"visible_objects_id"].append(trackedObject.second->Id());
157 return root.toStyledString();
176 root[
"BaseFPS"][
"num"] = BaseFPS.
num;
177 root[
"BaseFPS"][
"den"] = BaseFPS.
den;
178 root[
"TimeScale"] = this->TimeScale;
183 Json::Value trackedObjectJSON = trackedObject.second->JsonValue();
185 objects[trackedObject.second->Id()] = trackedObjectJSON;
187 root[
"objects"] = objects;
203 catch (
const std::exception& e)
206 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
217 if (!root[
"BaseFPS"].isNull() && root[
"BaseFPS"].isObject())
219 if (!root[
"BaseFPS"][
"num"].isNull())
221 BaseFPS.
num = (int) root[
"BaseFPS"][
"num"].asInt();
223 if (!root[
"BaseFPS"][
"den"].isNull())
225 BaseFPS.
den = (int) root[
"BaseFPS"][
"den"].asInt();
229 if (!root[
"TimeScale"].isNull())
230 TimeScale = (
double) root[
"TimeScale"].asDouble();
243 if (!root[
"objects"].isNull()){
245 std::string obj_id = std::to_string(trackedObject.first);
246 if(!root[
"objects"][obj_id].isNull()){
247 trackedObject.second->SetJsonValue(root[
"objects"][obj_id]);
253 if (!root[
"objects_id"].isNull()){
255 Json::Value trackedObjectJSON;
256 trackedObjectJSON[
"box_id"] = root[
"objects_id"][trackedObject.first].asString();
257 trackedObject.second->SetJsonValue(trackedObjectJSON);
273 Json::Value trackedObjectJSON = trackedObject.second->PropertiesJSON(requested_frame);
275 objects[trackedObject.second->Id()] = trackedObjectJSON;
277 root[
"objects"] = objects;
280 return root.toStyledString();
Header file for all Exception classes.
Header file for Timeline class.
Header file for Tracker effect class.
This abstract class is the base class, used by all clips in libopenshot.
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
openshot::ClipBase * ParentClip()
Parent clip object of this effect (which can be unparented and NULL)
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects)
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
EffectInfoStruct info
Information about the current effect.
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)
int num
Numerator for the fraction.
int den
Denominator for the fraction.
Exception for invalid JSON.
This class contains the properties of a tracked object and functions to manipulate it.
std::string Json() const override
Generate JSON string of this object.
std::string GetVisibleObjects(int64_t frame_number) const override
Get the indexes and IDs of all visible objects in the given frame.
Json::Value JsonValue() const override
Generate Json::Value for this object.
void SetJson(const std::string value) override
Load JSON string into this object.
std::shared_ptr< Frame > GetFrame(std::shared_ptr< Frame > frame, int64_t frame_number) override
Apply this effect to an openshot::Frame.
std::string PropertiesJSON(int64_t requested_frame) const override
std::shared_ptr< TrackedObjectBBox > trackedData
Pointer to an object that holds the bounding-box data and it's Keyframes.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Tracker()
Default constructor.
std::string protobuf_data_path
Path to the protobuf file that holds the bounding-box data.
This namespace is the default namespace for all code in the openshot library.
const Json::Value stringToJson(const std::string value)
This struct holds the information of a bounding-box.
float cy
y-coordinate of the bounding box center
float height
bounding box height
float cx
x-coordinate of the bounding box center
float width
bounding box width
bool has_video
Determines if this effect manipulates the image of a frame.
bool has_audio
Determines if this effect manipulates the audio of a frame.
std::string class_name
The class name of the effect.
std::string name
The name of the effect.
std::string description
The description of this effect and what it does.
bool has_tracked_object
Determines if this effect track objects through the clip.