Skip to content

Commit

Permalink
ObjectDetection: show object's icons and transform handlers per frame
Browse files Browse the repository at this point in the history
Only show the tracked object's icon (on the mini-GUI to attach a clip to it) and transform handler if the object appears on the screen (i.e. it has data for the requested frame)
  • Loading branch information
BrennoCaldato committed Jan 22, 2021
1 parent 5eb9f60 commit d6e0acb
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Fraction.cpp
Expand Up @@ -81,7 +81,7 @@ void Fraction::Reduce() {
}

// Return the reciprocal as a new Fraction
Fraction Fraction::Reciprocal()
Fraction Fraction::Reciprocal() const
{
// flip the fraction
return Fraction(den, num);
Expand Down
2 changes: 1 addition & 1 deletion src/Fraction.h
Expand Up @@ -68,7 +68,7 @@ namespace openshot {
int ToInt();

/// Return the reciprocal as a Fraction
Fraction Reciprocal();
Fraction Reciprocal() const;
};


Expand Down
24 changes: 21 additions & 3 deletions src/TrackedObjectBBox.cpp
Expand Up @@ -84,15 +84,30 @@ int64_t TrackedObjectBBox::GetLength() const
}

// Check if there is a bounding-box in the given frame
bool TrackedObjectBBox::Contains(int64_t frame_num)
bool TrackedObjectBBox::Contains(int64_t frame_num) const
{
// Get the time of given frame
double time = this->FrameNToTime(frame_num, 1.0);
// Create an iterator that points to the BoxVec pair indexed by the time of given frame (or the closest time)
auto it = BoxVec.lower_bound(time);
if (it == BoxVec.end())
if (it == BoxVec.end()){
// BoxVec pair not found
return false;
}
return true;
}

// Check if there is a bounding-box in the exact frame number
bool TrackedObjectBBox::ExactlyContains(int64_t frame_number) const
{
// Get the time of given frame
double time = FrameNToTime(frame_number, 1.0);
// Create an iterator that points to the BoxVec pair indexed by the exact time of given frame
auto it = BoxVec.find(time);
if (it == BoxVec.end()){
// BoxVec pair not found
return false;
}
return true;
}

Expand Down Expand Up @@ -210,7 +225,7 @@ Fraction TrackedObjectBBox::GetBaseFPS(){
}

// Get the time of the given frame
double TrackedObjectBBox::FrameNToTime(int64_t frame_number, double time_scale){
double TrackedObjectBBox::FrameNToTime(int64_t frame_number, double time_scale) const{
double time = ((double)frame_number) * this->BaseFps.Reciprocal().ToDouble() * (1.0 / time_scale);

return time;
Expand Down Expand Up @@ -384,6 +399,9 @@ Json::Value TrackedObjectBBox::PropertiesJSON(int64_t requested_frame) const
// Id
root["box_id"] = add_property_json("Box ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);

// Add a boolean property to inform if the object has data for the requested frame
root["visible"] = add_property_json("Visible", ExactlyContains(requested_frame), "bool", "", NULL, -1, -1, true, requested_frame);

// Add the data of given frame bounding-box to the JSON object
root["x1"] = add_property_json("X1", box.cx-(box.width/2), "float", "", NULL, 0.0, 1.0, false, requested_frame);
root["y1"] = add_property_json("Y1", box.cy-(box.height/2), "float", "", NULL, 0.0, 1.0, false, requested_frame);
Expand Down
6 changes: 4 additions & 2 deletions src/TrackedObjectBBox.h
Expand Up @@ -186,7 +186,9 @@ namespace openshot
void ScalePoints(double scale) override;

/// Check if there is a bounding-box in the given frame
bool Contains(int64_t frame_number);
bool Contains(int64_t frame_number) const;
/// Check if there is a bounding-box in the exact frame number
bool ExactlyContains(int64_t frame_number) const;

/// Get the size of BoxVec map
int64_t GetLength() const;
Expand All @@ -205,7 +207,7 @@ namespace openshot
bool LoadBoxData(std::string inputFilePath);

/// Get the time of the given frame
double FrameNToTime(int64_t frame_number, double time_scale);
double FrameNToTime(int64_t frame_number, double time_scale) const;

/// Interpolate the bouding-boxes properties
BBox InterpolateBoxes(double t1, double t2, BBox left, BBox right, double target);
Expand Down
23 changes: 14 additions & 9 deletions src/effects/ObjectDetection.cpp
Expand Up @@ -346,15 +346,20 @@ std::string ObjectDetection::PropertiesJSON(int64_t requested_frame) const {
// Save the trackedObject Id on root
Json::Value trackedObjectJSON = trackedObject.second->PropertiesJSON(requested_frame);
root["box_id-"+to_string(trackedObject.first)] = trackedObjectJSON["box_id"];
root["x1-"+to_string(trackedObject.first)] = trackedObjectJSON["x1"];
root["y1-"+to_string(trackedObject.first)] = trackedObjectJSON["y1"];
root["x2-"+to_string(trackedObject.first)] = trackedObjectJSON["x2"];
root["y2-"+to_string(trackedObject.first)] = trackedObjectJSON["y2"];
root["delta_x-"+to_string(trackedObject.first)] = trackedObjectJSON["delta_x"];
root["delta_y-"+to_string(trackedObject.first)] = trackedObjectJSON["delta_y"];
root["scale_x-"+to_string(trackedObject.first)] = trackedObjectJSON["scale_x"];
root["scale_y-"+to_string(trackedObject.first)] = trackedObjectJSON["scale_y"];
root["rotation-"+to_string(trackedObject.first)] = trackedObjectJSON["rotation"];
root["visible-"+to_string(trackedObject.first)] = trackedObjectJSON["visible"];

// Add trackedObject's properties only if it's visible in this frame (performance boost)
if (trackedObjectJSON["visible"]["value"].asBool()){
root["x1-"+to_string(trackedObject.first)] = trackedObjectJSON["x1"];
root["y1-"+to_string(trackedObject.first)] = trackedObjectJSON["y1"];
root["x2-"+to_string(trackedObject.first)] = trackedObjectJSON["x2"];
root["y2-"+to_string(trackedObject.first)] = trackedObjectJSON["y2"];
root["delta_x-"+to_string(trackedObject.first)] = trackedObjectJSON["delta_x"];
root["delta_y-"+to_string(trackedObject.first)] = trackedObjectJSON["delta_y"];
root["scale_x-"+to_string(trackedObject.first)] = trackedObjectJSON["scale_x"];
root["scale_y-"+to_string(trackedObject.first)] = trackedObjectJSON["scale_y"];
root["rotation-"+to_string(trackedObject.first)] = trackedObjectJSON["rotation"];
}
}

root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
Expand Down
1 change: 1 addition & 0 deletions src/effects/Tracker.cpp
Expand Up @@ -245,6 +245,7 @@ std::string Tracker::PropertiesJSON(int64_t requested_frame) const {
for (auto const& trackedObject : trackedObjects){
Json::Value trackedObjectJSON = trackedObject.second->PropertiesJSON(requested_frame);
root["box_id-"+to_string(trackedObject.first)] = trackedObjectJSON["box_id"];
root["visible-"+to_string(trackedObject.first)] = trackedObjectJSON["visible"];
root["x1-"+to_string(trackedObject.first)] = trackedObjectJSON["x1"];
root["y1-"+to_string(trackedObject.first)] = trackedObjectJSON["y1"];
root["x2-"+to_string(trackedObject.first)] = trackedObjectJSON["x2"];
Expand Down
4 changes: 2 additions & 2 deletions tests/CVTracker_Tests.cpp
Expand Up @@ -75,7 +75,7 @@ SUITE(CVTracker_Tests)
float y = fd.y1;
float width = fd.x2 - x;
float height = fd.y2 - y;
std::cout<<"\n\n Error: "<< processingController.GetErrorMessage() <<"\n";

// Compare if tracked data is equal to pre-tested ones
CHECK_EQUAL(259, (int)(x * 640));
CHECK_EQUAL(131, (int)(y * 360));
Expand Down Expand Up @@ -140,7 +140,7 @@ SUITE(CVTracker_Tests)
float y_2 = fd_2.y1;
float width_2 = fd_2.x2 - x_2;
float height_2 = fd_2.y2 - y_2;
std::cout<<"\n\n Error: "<< processingController.GetErrorMessage() <<"\n";

// Compare first tracker data with second tracker data
CHECK_EQUAL((int)(x_1 * 640), (int)(x_2 * 640));
CHECK_EQUAL((int)(y_1 * 360), (int)(y_2 * 360));
Expand Down

0 comments on commit d6e0acb

Please sign in to comment.