Skip to content

Commit

Permalink
Opencv is an optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennoCaldato committed Aug 1, 2020
1 parent 6ca35bb commit b92eadb
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 17 deletions.
6 changes: 6 additions & 0 deletions include/Clip.h
Expand Up @@ -160,6 +160,12 @@ namespace openshot {
openshot::FrameDisplayType display; ///< The format to display the frame number (if any)
openshot::VolumeMixType mixing; ///< What strategy should be followed when mixing audio with other clips

#ifdef USE_OPENCV
bool COMPILED_WITH_CV = true;
#else
bool COMPILED_WITH_CV = false;
#endif

/// Default Constructor
Clip();

Expand Down
10 changes: 7 additions & 3 deletions include/Effects.h
Expand Up @@ -42,13 +42,17 @@
#include "effects/Hue.h"
#include "effects/Mask.h"
#include "effects/Negate.h"
#include "effects/ObjectDetection.h"
#include "effects/Pixelate.h"
#include "effects/Stabilizer.h"
#include "effects/Saturation.h"
#include "effects/Shift.h"
#include "effects/Tracker.h"
#include "effects/Wave.h"

#ifdef USE_OPENCV
#include "effects/ObjectDetection.h"
#include "effects/Tracker.h"
#include "effects/Stabilizer.h"
#endif



#endif
2 changes: 2 additions & 0 deletions include/OpenShot.h
Expand Up @@ -141,8 +141,10 @@
#include "Timeline.h"
#include "Settings.h"
#ifdef USE_OPENCV
#include "ClipProcessingJobs.h"
#include "CVStabilization.h"
#include "CVTracker.h"
#include "CVObjectDetection.h"
#endif

#endif
1 change: 0 additions & 1 deletion include/Timeline.h
Expand Up @@ -54,7 +54,6 @@
#include "OpenMPUtilities.h"
#include "ReaderBase.h"
#include "Settings.h"
#include "ClipProcessingJobs.h"

namespace openshot {

Expand Down
10 changes: 6 additions & 4 deletions src/CMakeLists.txt
Expand Up @@ -82,14 +82,15 @@ endif()
find_package( OpenCV 4 )
if (OpenCV_FOUND)
message("\nCOMPILING WITH OPENCV\n")
set(CMAKE_SWIG_FLAGS "-DUSE_OPENCV=1")
add_definitions( -DUSE_OPENCV=1 )
else()
message("\nOPENCV NOT FOUND, SOME FUNCTIONALITIES WILL BE DISABLED\n")
endif()

################ PROTOBUF ##################
if (OpenCV_FOUND)
find_package(Protobuf)
find_package(Protobuf 3)

if (NOT Protobuf_FOUND)
# Protobuf is required when compiling with opencv
Expand Down Expand Up @@ -215,6 +216,9 @@ set(OPENSHOT_CV_SOURCES
CVStabilization.cpp
ClipProcessingJobs.cpp
CVObjectDetection.cpp
effects/Stabilizer.cpp
effects/Tracker.cpp
effects/ObjectDetection.cpp
./sort_filter/sort.cpp
./sort_filter/Hungarian.cpp
./sort_filter/KalmanTracker.cpp)
Expand All @@ -238,12 +242,10 @@ set(EFFECTS_SOURCES
effects/Hue.cpp
effects/Mask.cpp
effects/Negate.cpp
effects/ObjectDetection.cpp
effects/Pixelate.cpp
effects/Saturation.cpp
effects/Shift.cpp
effects/Stabilizer.cpp
effects/Tracker.cpp

effects/Wave.cpp)

# Qt video player components
Expand Down
16 changes: 16 additions & 0 deletions src/CVStabilization.cpp
Expand Up @@ -44,6 +44,8 @@ void CVStabilization::stabilizeClip(openshot::Clip& video, size_t _start, size_t
avr_dx=0; avr_dy=0; avr_da=0; max_dx=0; max_dy=0; max_da=0;

video.Open();
// Save original video width and height
cv::Size readerDims(video.Reader()->info.width, video.Reader()->info.height);

size_t frame_number;
if(!process_interval || end == 0 || end-start <= 0){
Expand All @@ -64,6 +66,9 @@ void CVStabilization::stabilizeClip(openshot::Clip& video, size_t _start, size_t

// Grab OpenCV Mat image
cv::Mat cvimage = f->GetImageCV();
// Resize frame to original video width and height if they differ
if(cvimage.size().width != readerDims.width || cvimage.size().height != readerDims.height)
cv::resize(cvimage, cvimage, cv::Size(readerDims.width, readerDims.height));
cv::cvtColor(cvimage, cvimage, cv::COLOR_RGB2GRAY);

if(!TrackFrameFeatures(cvimage, frame_number)){
Expand All @@ -85,6 +90,17 @@ void CVStabilization::stabilizeClip(openshot::Clip& video, size_t _start, size_t

// Calculate and save transformation data
transformationData = GenNewCamPosition(trajectoryData);

// Normalize smoothed trajectory data
for(auto &dataToNormalize : trajectoryData){
dataToNormalize.second.x/=readerDims.width;
dataToNormalize.second.y/=readerDims.height;
}
// Normalize transformation data
for(auto &dataToNormalize : transformationData){
dataToNormalize.second.dx/=readerDims.width;
dataToNormalize.second.dy/=readerDims.height;
}
}

// Track current frame features and find the relative transformation
Expand Down
2 changes: 0 additions & 2 deletions src/CVTracker.cpp
Expand Up @@ -50,8 +50,6 @@ cv::Ptr<cv::Tracker> CVTracker::selectTracker(std::string trackerType){
t = cv::TrackerTLD::create();
if (trackerType == "MEDIANFLOW")
t = cv::TrackerMedianFlow::create();
if (trackerType == "GOTURN")
t = cv::TrackerGOTURN::create();
if (trackerType == "MOSSE")
t = cv::TrackerMOSSE::create();
if (trackerType == "CSRT")
Expand Down
5 changes: 5 additions & 0 deletions src/EffectInfo.cpp
Expand Up @@ -86,6 +86,7 @@ EffectBase* EffectInfo::CreateEffect(std::string effect_type) {
else if (effect_type == "Wave")
return new Wave();

#ifdef USE_OPENCV
else if(effect_type == "Stabilizer")
return new Stabilizer();

Expand All @@ -94,6 +95,7 @@ EffectBase* EffectInfo::CreateEffect(std::string effect_type) {

else if(effect_type == "Object Detector")
return new ObjectDetection();
#endif

return NULL;
}
Expand All @@ -119,9 +121,12 @@ Json::Value EffectInfo::JsonValue() {
root.append(Saturation().JsonInfo());
root.append(Shift().JsonInfo());
root.append(Wave().JsonInfo());

#ifdef USE_OPENCV
root.append(Stabilizer().JsonInfo());
root.append(Tracker().JsonInfo());
root.append(ObjectDetection().JsonInfo());
#endif

// return JsonValue
return root;
Expand Down
15 changes: 11 additions & 4 deletions src/bindings/python/openshot.i
Expand Up @@ -113,6 +113,12 @@
%}
#endif

#ifdef USE_OPENCV
%{
#include "ClipProcessingJobs.h"
%}
#endif

/* Generic language independent exception handler. */
%include "exception.i"
%exception {
Expand Down Expand Up @@ -206,7 +212,6 @@
%include "Timeline.h"
%include "ZmqLogger.h"
%include "AudioDeviceInfo.h"
%include "ClipProcessingJobs.h"

#ifdef USE_IMAGEMAGICK
%include "ImageReader.h"
Expand All @@ -229,9 +234,11 @@
%include "effects/Saturation.h"
%include "effects/Shift.h"
%include "effects/Wave.h"
%include "effects/Stabilizer.h"
%include "effects/Tracker.h"
%include "effects/ObjectDetection.h"
#ifdef USE_OPENCV
%include "effects/Stabilizer.h"
%include "effects/Tracker.h"
%include "effects/ObjectDetection.h"
#endif


/* Wrap std templates (list, vector, etc...) */
Expand Down
4 changes: 2 additions & 2 deletions src/effects/Stabilizer.cpp
Expand Up @@ -91,8 +91,8 @@ std::shared_ptr<Frame> Stabilizer::GetFrame(std::shared_ptr<Frame> frame, int64_
T.at<double>(1,0) = sin(transformationData[frame_number].da);
T.at<double>(1,1) = cos(transformationData[frame_number].da);

T.at<double>(0,2) = transformationData[frame_number].dx;
T.at<double>(1,2) = transformationData[frame_number].dy;
T.at<double>(0,2) = transformationData[frame_number].dx * frame_image.size().width;
T.at<double>(1,2) = transformationData[frame_number].dy * frame_image.size().height;

// Apply rotation matrix to image
cv::Mat frame_stabilized;
Expand Down
4 changes: 3 additions & 1 deletion tests/CMakeLists.txt
Expand Up @@ -106,11 +106,13 @@ endif()
find_package( OpenCV 4 )
if (OpenCV_FOUND)
add_definitions( -DUSE_OPENCV=1 )
else()
message("\nOPENCV NOT FOUND, OPENCV RELATED TESTS ARE DISABLED\n")
endif()

################ PROTOBUF ##################
if (OpenCV_FOUND)
find_package(Protobuf)
find_package(Protobuf 3)

if (NOT Protobuf_FOUND)
# Protobuf is required when compiling with opencv
Expand Down

0 comments on commit b92eadb

Please sign in to comment.