Skip to content

Commit

Permalink
OpenCV: Adapt to API changes in OpenCV 4.5.2+ (#639)
Browse files Browse the repository at this point in the history
* CVTracker: Handle API changes in OpenCV
The former cv::Tracker API we've been using is now cv::legacy::Tracker,
starting in OpenCV 4.5.1.

* CVTracker: Move some includes, add std:: prefixes

* Move ClipProcessingJobs into openshot NS

* OpenCV 4.5.1 message and auto-disabling

* Add fstream includes, explicit std:: namespace
Work around a MacOS bug where bare fstream resolves to the wrong class.

Co-authored-by: Brenno <brenno.caldato@gmail.com>
Co-authored-by: Brenno A. C. Caldato <BrennoCaldato@users.noreply.github.com>
  • Loading branch information
3 people committed May 4, 2021
1 parent aa57219 commit 813c517
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Expand Up @@ -402,7 +402,7 @@ if(ENABLE_OPENCV)
set(ENABLE_OPENCV FALSE CACHE BOOL
"Build with OpenCV algorithms (requires Protobuf 3)" FORCE)
# If we have version 4.5.1, all hope is lost
elseif(OpenCV_VERSION VERSION_EQUAL 4.5.1)
elseif(OpenCV_VERSION VERSION_EQUAL "4.5.1")
message(WARNING [[Incompatible OpenCV version detected
OpenCV version 4.5.1 contains header errors which make it unable to be used with OpenShot. OpenCV support wil be disabled. Upgrade to OpenCV 4.5.2+ or downgrade to 4.5.0 or earlier, to enable OpenCV.
See https://github.com/opencv/opencv/issues/19260]])
Expand Down Expand Up @@ -431,7 +431,7 @@ See https://github.com/opencv/opencv/issues/19260]])
mark_as_advanced(OPENCV_VERSION_STR)

# We may need to use Tracker as opencv::legacy::Tracker
if(OpenCV_VERSION VERSION_GREATER_EQUAL 4.5.2)
if(OpenCV_VERSION VERSION_GREATER "4.5.1")
target_compile_definitions(openshot PUBLIC USE_LEGACY_TRACKER=1)
endif()

Expand Down
4 changes: 4 additions & 0 deletions src/CVObjectDetection.cpp
Expand Up @@ -28,6 +28,10 @@
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/

#include <fstream>
#include <iomanip>
#include <iostream>

#include "CVObjectDetection.h"
#include <google/protobuf/util/time_util.h>

Expand Down
13 changes: 10 additions & 3 deletions src/CVStabilization.cpp
Expand Up @@ -28,6 +28,10 @@
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/

#include <fstream>
#include <iomanip>
#include <iostream>

#include "CVStabilization.h"
#include <google/protobuf/util/time_util.h>

Expand Down Expand Up @@ -276,6 +280,8 @@ std::map<size_t,TransformParam> CVStabilization::GenNewCamPosition(std::map <siz

// Save stabilization data to protobuf file
bool CVStabilization::SaveStabilizedData(){
using std::ios;

// Create stabilization message
pb_stabilize::Stabilization stabilizationMessage;

Expand All @@ -292,7 +298,7 @@ bool CVStabilization::SaveStabilizedData(){
// Write the new message to disk.
std::fstream output(protobuf_data_path, ios::out | ios::trunc | ios::binary);
if (!stabilizationMessage.SerializeToOstream(&output)) {
cerr << "Failed to write protobuf message." << endl;
std::cerr << "Failed to write protobuf message." << std::endl;
return false;
}

Expand Down Expand Up @@ -380,12 +386,13 @@ void CVStabilization::SetJsonValue(const Json::Value root) {

// Load protobuf data file
bool CVStabilization::_LoadStabilizedData(){
using std::ios;
// Create stabilization message
pb_stabilize::Stabilization stabilizationMessage;
// Read the existing tracker message.
fstream input(protobuf_data_path, ios::in | ios::binary);
std::fstream input(protobuf_data_path, ios::in | ios::binary);
if (!stabilizationMessage.ParseFromIstream(&input)) {
cerr << "Failed to parse protobuf message." << endl;
std::cerr << "Failed to parse protobuf message." << std::endl;
return false;
}

Expand Down
40 changes: 23 additions & 17 deletions src/CVTracker.cpp
Expand Up @@ -28,14 +28,18 @@
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/

#include "CVTracker.h"
#include <fstream>
#include <iomanip>
#include <iostream>

#include <google/protobuf/util/time_util.h>

using namespace std;
#include "OpenCVUtilities.h"
#include "CVTracker.h"

using namespace openshot;
using google::protobuf::util::TimeUtil;


// Constructor
CVTracker::CVTracker(std::string processInfoJson, ProcessingController &processingController)
: processingController(&processingController), json_interval(false){
Expand All @@ -45,25 +49,24 @@ CVTracker::CVTracker(std::string processInfoJson, ProcessingController &processi
}

// Set desirable tracker method
cv::Ptr<cv::Tracker> CVTracker::selectTracker(std::string trackerType){
cv::Ptr<cv::Tracker> t;
cv::Ptr<OPENCV_TRACKER_TYPE> CVTracker::selectTracker(std::string trackerType){

if (trackerType == "BOOSTING")
t = cv::TrackerBoosting::create();
return OPENCV_TRACKER_NS::TrackerBoosting::create();
if (trackerType == "MIL")
t = cv::TrackerMIL::create();
return OPENCV_TRACKER_NS::TrackerMIL::create();
if (trackerType == "KCF")
t = cv::TrackerKCF::create();
return OPENCV_TRACKER_NS::TrackerKCF::create();
if (trackerType == "TLD")
t = cv::TrackerTLD::create();
return OPENCV_TRACKER_NS::TrackerTLD::create();
if (trackerType == "MEDIANFLOW")
t = cv::TrackerMedianFlow::create();
return OPENCV_TRACKER_NS::TrackerMedianFlow::create();
if (trackerType == "MOSSE")
t = cv::TrackerMOSSE::create();
return OPENCV_TRACKER_NS::TrackerMOSSE::create();
if (trackerType == "CSRT")
t = cv::TrackerCSRT::create();
return OPENCV_TRACKER_NS::TrackerCSRT::create();

return t;
return nullptr;
}

// Track object in the hole clip or in a given interval
Expand Down Expand Up @@ -197,6 +200,8 @@ bool CVTracker::trackFrame(cv::Mat &frame, size_t frameId){
}

bool CVTracker::SaveTrackedData(){
using std::ios;

// Create tracker message
pb_tracker::Tracker trackerMessage;

Expand All @@ -214,7 +219,7 @@ bool CVTracker::SaveTrackedData(){
// Write the new message to disk.
std::fstream output(protobuf_data_path, ios::out | ios::trunc | ios::binary);
if (!trackerMessage.SerializeToOstream(&output)) {
cerr << "Failed to write protobuf message." << endl;
std::cerr << "Failed to write protobuf message." << std::endl;
return false;
}
}
Expand Down Expand Up @@ -314,14 +319,16 @@ void CVTracker::SetJsonValue(const Json::Value root) {

// Load protobuf data file
bool CVTracker::_LoadTrackedData(){
using std::ios;

// Create tracker message
pb_tracker::Tracker trackerMessage;

{
// Read the existing tracker message.
fstream input(protobuf_data_path, ios::in | ios::binary);
std::fstream input(protobuf_data_path, ios::in | ios::binary);
if (!trackerMessage.ParseFromIstream(&input)) {
cerr << "Failed to parse protobuf message." << endl;
std::cerr << "Failed to parse protobuf message." << std::endl;
return false;
}
}
Expand Down Expand Up @@ -353,4 +360,3 @@ bool CVTracker::_LoadTrackedData(){

return true;
}

14 changes: 8 additions & 6 deletions src/CVTracker.h
Expand Up @@ -31,19 +31,21 @@
#ifndef OPENSHOT_CVTRACKER_H
#define OPENSHOT_CVTRACKER_H

#define int64 opencv_broken_int
#define uint64 opencv_broken_uint
#include "OpenCVUtilities.h"

#define int64 int64_t
#define uint64 uint64_t
#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/core.hpp>
#undef uint64
#undef int64

#include <fstream>
#include "Clip.h"
#include "KeyFrame.h"
#include "Frame.h"
#include "Json.h"

#include "ProcessingController.h"
#include "protobuf_messages/trackerdata.pb.h"

Expand Down Expand Up @@ -87,7 +89,7 @@ namespace openshot
private:
std::map<size_t, FrameData> trackedDataById; // Save tracked data
std::string trackerType; // Name of the chosen tracker
cv::Ptr<cv::Tracker> tracker; // Pointer of the selected tracker
cv::Ptr<OPENCV_TRACKER_TYPE> tracker; // Pointer of the selected tracker

cv::Rect2d bbox; // Bounding box coords
SortTracker sort;
Expand Down Expand Up @@ -116,8 +118,8 @@ namespace openshot
// Constructor
CVTracker(std::string processInfoJson, ProcessingController &processingController);

/// Set desirable tracker method
cv::Ptr<cv::Tracker> selectTracker(std::string trackerType);
// Set desirable tracker method
cv::Ptr<OPENCV_TRACKER_TYPE> selectTracker(std::string trackerType);

/// Track object in the hole clip or in a given interval
///
Expand Down
18 changes: 11 additions & 7 deletions src/ClipProcessingJobs.cpp
@@ -1,7 +1,9 @@
#include "ClipProcessingJobs.h"

namespace openshot {

// Constructor responsible to choose processing type and apply to clip
ClipProcessingJobs::ClipProcessingJobs(std::string processingType, std::string processInfoJson) :
ClipProcessingJobs::ClipProcessingJobs(std::string processingType, std::string processInfoJson) :
processingType(processingType), processInfoJson(processInfoJson){
}

Expand All @@ -20,7 +22,7 @@ void ClipProcessingJobs::processClip(Clip& clip, std::string json){
}
}

// Apply object tracking to clip
// Apply object tracking to clip
void ClipProcessingJobs::trackClip(Clip& clip, ProcessingController& controller){

// Create CVTracker object
Expand All @@ -46,7 +48,7 @@ void ClipProcessingJobs::trackClip(Clip& clip, ProcessingController& controller)
// Apply object detection to clip
void ClipProcessingJobs::detectObjectsClip(Clip& clip, ProcessingController& controller){
// create CVObjectDetection object
CVObjectDetection objDetector(processInfoJson, controller);
CVObjectDetection objDetector(processInfoJson, controller);
// Start object detection process
objDetector.detectObjectsClip(clip);

Expand All @@ -66,7 +68,7 @@ void ClipProcessingJobs::detectObjectsClip(Clip& clip, ProcessingController& con

void ClipProcessingJobs::stabilizeClip(Clip& clip, ProcessingController& controller){
// create CVStabilization object
CVStabilization stabilizer(processInfoJson, controller);
CVStabilization stabilizer(processInfoJson, controller);
// Start stabilization process
stabilizer.stabilizeClip(clip);

Expand All @@ -84,13 +86,13 @@ void ClipProcessingJobs::stabilizeClip(Clip& clip, ProcessingController& control
}
}

// Get processing progress while iterating on the clip
// Get processing progress while iterating on the clip
int ClipProcessingJobs::GetProgress(){

return (int)processingController.GetProgress();
}

// Check if processing finished
// Check if processing finished
bool ClipProcessingJobs::IsDone(){

if(processingController.GetFinished()){
Expand All @@ -112,4 +114,6 @@ bool ClipProcessingJobs::GetError(){
// get the error message
std::string ClipProcessingJobs::GetErrorMessage(){
return processingController.GetErrorMessage();
}
}

} // namespace openshot
12 changes: 6 additions & 6 deletions src/ClipProcessingJobs.h
Expand Up @@ -35,7 +35,7 @@
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#undef uint64
#undef int64
#undef int64

#include "CVStabilization.h"
#include "CVTracker.h"
Expand All @@ -46,7 +46,7 @@
#include "ProcessingController.h"
#include "Clip.h"

using namespace openshot;
namespace openshot {

// Constructor responsible to choose processing type and apply to clip
class ClipProcessingJobs{
Expand All @@ -63,7 +63,7 @@ class ClipProcessingJobs{
/// Will handle a Thread safely comutication between ClipProcessingJobs and the processing effect classes
ProcessingController processingController;

// Apply object tracking to clip
// Apply object tracking to clip
void trackClip(Clip& clip, ProcessingController& controller);
// Apply stabilization to clip
void stabilizeClip(Clip& clip, ProcessingController& controller);
Expand All @@ -74,7 +74,7 @@ class ClipProcessingJobs{
public:
// Constructor
ClipProcessingJobs(std::string processingType, std::string processInfoJson);
// Process clip accordingly to processingType
// Process clip accordingly to processingType
void processClip(Clip& clip, std::string json);

// Thread related variables and methods
Expand All @@ -83,6 +83,6 @@ class ClipProcessingJobs{
void CancelProcessing();
bool GetError();
std::string GetErrorMessage();
};


};
} // namespace openshot
50 changes: 50 additions & 0 deletions src/OpenCVUtilities.h
@@ -0,0 +1,50 @@
/**
* @file
* @brief Header file for OpenCVUtilities (set some common macros)
* @author FeRD (Frank Dana) <ferdnyc@gmail.com>
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @ref License
*/

/* LICENSE
*
* Copyright (c) 2008-2021 OpenShot Studios, LLC
* <http://www.openshotstudios.com/>. This file is part of
* OpenShot Library (libopenshot), an open-source project dedicated to
* delivering high quality video editing and animation solutions to the
* world. For more information visit <http://www.openshot.org/>.
*
* OpenShot Library (libopenshot) is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenShot Library (libopenshot) is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef OPENSHOT_OPENCV_UTILITIES_H
#define OPENSHOT_OPENCV_UTILITIES_H

#define int64 int64_t
#define uint64 uint64_t
#if USE_LEGACY_TRACKER
#include <opencv2/tracking.hpp>
#include <opencv2/tracking/tracking_legacy.hpp>
#define OPENCV_TRACKER_TYPE cv::legacy::Tracker
#define OPENCV_TRACKER_NS cv::legacy
#else
#include <opencv2/tracking.hpp>
#define OPENCV_TRACKER_TYPE cv::Tracker
#define OPENCV_TRACKER_NS cv
#endif
#undef int64
#undef uint64

#endif // OPENSHOT_OPENCV_UTILITIES_H
7 changes: 5 additions & 2 deletions src/effects/ObjectDetection.cpp
Expand Up @@ -28,6 +28,9 @@
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/

#include <fstream>
#include <iostream>

#include "effects/ObjectDetection.h"
#include "effects/Tracker.h"
#include "Exceptions.h"
Expand Down Expand Up @@ -135,9 +138,9 @@ bool ObjectDetection::LoadObjDetectdData(std::string inputFilePath){

{
// Read the existing tracker message.
fstream input(inputFilePath, ios::in | ios::binary);
std::fstream input(inputFilePath, std::ios::in | std::ios::binary);
if (!objMessage.ParseFromIstream(&input)) {
cerr << "Failed to parse protobuf message." << endl;
std::cerr << "Failed to parse protobuf message." << std::endl;
return false;
}
}
Expand Down

0 comments on commit 813c517

Please sign in to comment.