OpenCV: Adapt to API changes in OpenCV 4.5.2+ (#639)

* 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>
This commit is contained in:
Frank Dana
2021-05-04 07:33:47 -04:00
committed by GitHub
parent aa57219eb9
commit 813c5175ca
13 changed files with 144 additions and 55 deletions

View File

@@ -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]])
@@ -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()

View File

@@ -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>

View File

@@ -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>
@@ -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;
@@ -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;
}
@@ -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;
}

View File

@@ -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){
@@ -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
@@ -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;
@@ -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;
}
}
@@ -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;
}
}
@@ -353,4 +360,3 @@ bool CVTracker::_LoadTrackedData(){
return true;
}

View File

@@ -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"
@@ -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;
@@ -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
///

View File

@@ -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){
}
@@ -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
@@ -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);
@@ -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);
@@ -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()){
@@ -112,4 +114,6 @@ bool ClipProcessingJobs::GetError(){
// get the error message
std::string ClipProcessingJobs::GetErrorMessage(){
return processingController.GetErrorMessage();
}
}
} // namespace openshot

View File

@@ -35,7 +35,7 @@
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#undef uint64
#undef int64
#undef int64
#include "CVStabilization.h"
#include "CVTracker.h"
@@ -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{
@@ -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);
@@ -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
@@ -83,6 +83,6 @@ class ClipProcessingJobs{
void CancelProcessing();
bool GetError();
std::string GetErrorMessage();
};
};
} // namespace openshot

50
src/OpenCVUtilities.h Normal file
View File

@@ -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

View File

@@ -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"
@@ -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;
}
}

View File

@@ -28,6 +28,10 @@
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <fstream>
#include <iomanip>
#include <iostream>
#include "effects/Stabilizer.h"
#include "Exceptions.h"
#include <google/protobuf/util/time_util.h>
@@ -115,14 +119,14 @@ std::shared_ptr<Frame> Stabilizer::GetFrame(std::shared_ptr<Frame> frame, int64_
// Load protobuf data file
bool Stabilizer::LoadStabilizedData(std::string inputFilePath){
using std::ios;
// Create stabilization message
pb_stabilize::Stabilization stabilizationMessage;
// Read the existing tracker message.
fstream input(inputFilePath, ios::in | ios::binary);
std::fstream input(inputFilePath, 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;
}

View File

@@ -28,8 +28,14 @@
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <memory>
#include <fstream>
#include <iostream>
#include "effects/Tracker.h"
#include "Exceptions.h"
#include <google/protobuf/util/time_util.h>
using namespace std;
@@ -104,14 +110,16 @@ std::shared_ptr<Frame> Tracker::GetFrame(std::shared_ptr<Frame> frame, int64_t f
// Load protobuf data file
bool Tracker::LoadTrackedData(std::string inputFilePath){
using std::ios;
// Create tracker message
pb_tracker::Tracker trackerMessage;
{
// Read the existing tracker message.
fstream input(inputFilePath, ios::in | ios::binary);
std::fstream input(inputFilePath, 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;
}
}
@@ -202,7 +210,7 @@ void Tracker::SetJsonValue(const Json::Value root) {
protobuf_data_path = (root["protobuf_data_path"].asString());
if(!LoadTrackedData(protobuf_data_path)){
std::cout<<"Invalid protobuf data path";
std::cerr << "Invalid protobuf data path\n";
protobuf_data_path = "";
}
}

View File

@@ -31,15 +31,15 @@
#ifndef OPENSHOT_TRACKER_EFFECT_H
#define OPENSHOT_TRACKER_EFFECT_H
#include <string>
#include <memory>
#include <map>
#include "../EffectBase.h"
#include <cmath>
#include <fstream>
#include <stdio.h>
#include <memory>
#include "../Color.h"
#include "../Json.h"
#include "../KeyFrame.h"
#include "protobuf_messages/trackerdata.pb.h"
// Tracking info struct

View File

@@ -29,13 +29,14 @@
*/
#include <catch2/catch.hpp>
#include <sstream>
#include "Point.h"
#include "Enums.h"
#include "Exceptions.h"
#include "Coordinate.h"
#include "Json.h"
TEST_CASE( "default constructor", "[libopenshot][point]" )
{
openshot::Point p;