Skip to content

Commit

Permalink
Merge branch 'opencv' into keyframe-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennoCaldato committed Nov 10, 2020
2 parents 5db1c0f + 2d18143 commit a4f4561
Show file tree
Hide file tree
Showing 26 changed files with 815 additions and 123 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -9,4 +9,3 @@
tags
*~

.vscode/
7 changes: 4 additions & 3 deletions src/CMakeLists.txt
Expand Up @@ -35,7 +35,7 @@ if (POLICY CMP0057)
endif()

############### PROFILING #################
#set(PROFILER "/usr/lib/libprofiler.so.0.3.2")
#set(PROFILER "/usr/lib//usr/lib/libprofiler.so.0.4.5")
#set(PROFILER "/usr/lib/libtcmalloc.so.4")

if(CMAKE_VERSION VERSION_LESS 3.3)
Expand Down Expand Up @@ -125,6 +125,7 @@ set(EFFECTS_SOURCES
effects/Bars.cpp
effects/Blur.cpp
effects/Brightness.cpp
effects/Caption.cpp
effects/ChromaKey.cpp
effects/ColorShift.cpp
effects/Crop.cpp
Expand Down Expand Up @@ -400,8 +401,8 @@ 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 )
list(APPEND CMAKE_SWIG_FLAGS -DUSE_OPENCV=1)
target_compile_definitions(openshot PUBLIC USE_OPENCV=1)
else()
message("\nOPENCV NOT FOUND, SOME FUNCTIONALITIES WILL BE DISABLED\n")
endif()
Expand Down
42 changes: 33 additions & 9 deletions src/CVObjectDetection.cpp
Expand Up @@ -32,7 +32,6 @@

using namespace openshot;


CVObjectDetection::CVObjectDetection(std::string processInfoJson, ProcessingController &processingController)
: processingController(&processingController), processingDevice("CPU"){
SetJson(processInfoJson);
Expand All @@ -56,6 +55,12 @@ void CVObjectDetection::detectObjectsClip(openshot::Clip &video, size_t _start,

video.Open();

if(error){
return;
}

processingController->SetError(false, "");

// Load names of classes
std::ifstream ifs(classesFile.c_str());
std::string line;
Expand Down Expand Up @@ -377,17 +382,36 @@ void CVObjectDetection::SetJsonValue(const Json::Value root) {
if (!root["protobuf_data_path"].isNull()){
protobuf_data_path = (root["protobuf_data_path"].asString());
}
if (!root["processing_device"].isNull()){
processingDevice = (root["processing_device"].asString());
if (!root["processing-device"].isNull()){
processingDevice = (root["processing-device"].asString());
}
if (!root["model_configuration"].isNull()){
modelConfiguration = (root["model_configuration"].asString());
if (!root["model-config"].isNull()){
modelConfiguration = (root["model-config"].asString());
std::ifstream infile(modelConfiguration);
if(!infile.good()){
processingController->SetError(true, "Incorrect path to model config file");
error = true;
}

}
if (!root["model_weights"].isNull()){
modelWeights= (root["model_weights"].asString());
if (!root["model-weights"].isNull()){
modelWeights= (root["model-weights"].asString());
std::ifstream infile(modelWeights);
if(!infile.good()){
processingController->SetError(true, "Incorrect path to model weight file");
error = true;
}

}
if (!root["classes_file"].isNull()){
classesFile = (root["classes_file"].asString());
if (!root["class-names"].isNull()){
classesFile = (root["class-names"].asString());

std::ifstream infile(classesFile);
if(!infile.good()){
processingController->SetError(true, "Incorrect path to class name file");
error = true;
}

}
}

Expand Down
2 changes: 2 additions & 0 deletions src/CVObjectDetection.h
Expand Up @@ -91,6 +91,8 @@ namespace openshot
size_t start;
size_t end;

bool error = false;

/// Will handle a Thread safely comutication between ClipProcessingJobs and the processing effect classes
ProcessingController *processingController;

Expand Down
9 changes: 7 additions & 2 deletions src/CVStabilization.cpp
Expand Up @@ -42,6 +42,11 @@ CVStabilization::CVStabilization(std::string processInfoJson, ProcessingControll
// Process clip and store necessary stabilization data
void CVStabilization::stabilizeClip(openshot::Clip& video, size_t _start, size_t _end, bool process_interval){

if(error){
return;
}
processingController->SetError(false, "");

start = _start; end = _end;
// Compute max and average transformation parameters
avr_dx=0; avr_dy=0; avr_da=0; max_dx=0; max_dy=0; max_da=0;
Expand Down Expand Up @@ -364,8 +369,8 @@ void CVStabilization::SetJsonValue(const Json::Value root) {
if (!root["protobuf_data_path"].isNull()){
protobuf_data_path = (root["protobuf_data_path"].asString());
}
if (!root["smoothing_window"].isNull()){
smoothingWindow = (root["smoothing_window"].asInt());
if (!root["smoothing-window"].isNull()){
smoothingWindow = (root["smoothing-window"].asInt());
}
}

Expand Down
1 change: 1 addition & 0 deletions src/CVStabilization.h
Expand Up @@ -101,6 +101,7 @@ class CVStabilization {
std::string protobuf_data_path;

uint progress;
bool error = false;

/// Will handle a Thread safely comutication between ClipProcessingJobs and the processing effect classes
ProcessingController *processingController;
Expand Down
41 changes: 25 additions & 16 deletions src/CVTracker.cpp
Expand Up @@ -65,7 +65,6 @@ cv::Ptr<cv::Tracker> CVTracker::selectTracker(std::string trackerType){
void CVTracker::trackClip(openshot::Clip& video, size_t _start, size_t _end, bool process_interval){

video.Open();

if(!json_interval){
start = _start; end = _end;

Expand All @@ -79,7 +78,12 @@ void CVTracker::trackClip(openshot::Clip& video, size_t _start, size_t _end, boo
start = start + video.Start() * video.Reader()->info.fps.ToInt();
end = video.End() * video.Reader()->info.fps.ToInt();
}


if(error){
return;
}

processingController->SetError(false, "");
bool trackerInit = false;

size_t frame;
Expand All @@ -92,7 +96,6 @@ void CVTracker::trackClip(openshot::Clip& video, size_t _start, size_t _end, boo
return;
}

std::cout<<"Frame: "<<frame<<"\n";
size_t frame_number = frame;
// Get current frame
std::shared_ptr<openshot::Frame> f = video.GetFrame(frame_number);
Expand Down Expand Up @@ -271,33 +274,39 @@ void CVTracker::SetJsonValue(const Json::Value root) {
if (!root["protobuf_data_path"].isNull()){
protobuf_data_path = (root["protobuf_data_path"].asString());
}
if (!root["tracker_type"].isNull()){
trackerType = (root["tracker_type"].asString());
if (!root["tracker-type"].isNull()){
trackerType = (root["tracker-type"].asString());
}
if (!root["bbox"].isNull()){
double x = root["bbox"]["x"].asDouble();
double y = root["bbox"]["y"].asDouble();
double w = root["bbox"]["w"].asDouble();
double h = root["bbox"]["h"].asDouble();

if (!root["region"].isNull()){
double x = root["region"]["x"].asDouble();
double y = root["region"]["y"].asDouble();
double w = root["region"]["width"].asDouble();
double h = root["region"]["height"].asDouble();
cv::Rect2d prev_bbox(x,y,w,h);
bbox = prev_bbox;
}
if (!root["first_frame"].isNull()){
start = root["first_frame"].asInt64();
else{
processingController->SetError(true, "No initial bounding box selected");
error = true;
}

if (!root["region"]["first-frame"].isNull()){
start = root["region"]["first-frame"].asInt64();
json_interval = true;
}
else{
processingController->SetError(true, "No first-frame");
error = true;
}
}



/*
||||||||||||||||||||||||||||||||||||||||||||||||||
ONLY FOR MAKE TEST
||||||||||||||||||||||||||||||||||||||||||||||||||
*/



// Load protobuf data file
bool CVTracker::_LoadTrackedData(){
// Create tracker message
Expand Down
4 changes: 3 additions & 1 deletion src/CVTracker.h
Expand Up @@ -103,10 +103,12 @@ namespace openshot

/// Will handle a Thread safely comutication between ClipProcessingJobs and the processing effect classes
ProcessingController *processingController;

bool json_interval;
size_t start;
size_t end;

bool error = false;

// Initialize the tracker
bool initTracker(cv::Mat &frame, size_t frameId);
Expand Down
16 changes: 15 additions & 1 deletion src/ClipProcessingJobs.cpp
Expand Up @@ -5,7 +5,8 @@ ClipProcessingJobs::ClipProcessingJobs(std::string processingType, std::string p
processingType(processingType), processInfoJson(processInfoJson){
}

void ClipProcessingJobs::processClip(Clip& clip){
void ClipProcessingJobs::processClip(Clip& clip, std::string json){
processInfoJson = json;

// Process clip and save processed data
if(processingType == "Stabilizer"){
Expand Down Expand Up @@ -83,11 +84,13 @@ void ClipProcessingJobs::stabilizeClip(Clip& clip, ProcessingController& control
}
}

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

return (int)processingController.GetProgress();
}

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

if(processingController.GetFinished()){
Expand All @@ -96,6 +99,17 @@ bool ClipProcessingJobs::IsDone(){
return processingController.GetFinished();
}

// stop preprocessing before finishing it
void ClipProcessingJobs::CancelProcessing(){
processingController.CancelProcessing();
}

// check if there is an error with the config
bool ClipProcessingJobs::GetError(){
return processingController.GetError();
}

// get the error message
std::string ClipProcessingJobs::GetErrorMessage(){
return processingController.GetErrorMessage();
}
5 changes: 4 additions & 1 deletion src/ClipProcessingJobs.h
Expand Up @@ -75,11 +75,14 @@ class ClipProcessingJobs{
// Constructor
ClipProcessingJobs(std::string processingType, std::string processInfoJson);
// Process clip accordingly to processingType
void processClip(Clip& clip);
void processClip(Clip& clip, std::string json);

// Thread related variables and methods
int GetProgress();
bool IsDone();
void CancelProcessing();
bool GetError();
std::string GetErrorMessage();


};
4 changes: 4 additions & 0 deletions src/EffectInfo.cpp
Expand Up @@ -52,6 +52,9 @@ EffectBase* EffectInfo::CreateEffect(std::string effect_type) {
else if (effect_type == "Brightness")
return new Brightness();

else if (effect_type == "Caption")
return new Caption();

else if (effect_type == "ChromaKey")
return new ChromaKey();

Expand Down Expand Up @@ -109,6 +112,7 @@ Json::Value EffectInfo::JsonValue() {
root.append(Bars().JsonInfo());
root.append(Blur().JsonInfo());
root.append(Brightness().JsonInfo());
root.append(Caption().JsonInfo());
root.append(ChromaKey().JsonInfo());
root.append(ColorShift().JsonInfo());
root.append(Crop().JsonInfo());
Expand Down
1 change: 1 addition & 0 deletions src/Effects.h
Expand Up @@ -35,6 +35,7 @@
#include "effects/Bars.h"
#include "effects/Blur.h"
#include "effects/Brightness.h"
#include "effects/Caption.h"
#include "effects/ChromaKey.h"
#include "effects/ColorShift.h"
#include "effects/Crop.h"
Expand Down
4 changes: 2 additions & 2 deletions src/Frame.cpp
Expand Up @@ -968,14 +968,14 @@ cv::Mat Frame::GetImageCV()

std::shared_ptr<QImage> Frame::Mat2Qimage(cv::Mat img){
cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
QImage qimg((uchar*) img.data, img.cols, img.rows, img.step, QImage::Format_RGBA8888_Premultiplied);
QImage qimg((uchar*) img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);

std::shared_ptr<QImage> imgIn = std::make_shared<QImage>(qimg.copy());

// Always convert to RGBA8888 (if different)
if (imgIn->format() != QImage::Format_RGBA8888_Premultiplied)
*imgIn = imgIn->convertToFormat(QImage::Format_RGBA8888_Premultiplied);

return imgIn;
}

Expand Down
21 changes: 21 additions & 0 deletions src/ProcessingController.h
Expand Up @@ -41,10 +41,13 @@ class ProcessingController{
uint processingProgress;
bool processingFinished;
bool stopProcessing;
bool error = true;
std::string error_message;

std::mutex mtxProgress;
std::mutex mtxFinished;
std::mutex mtxStop;
std::mutex mtxerror;

public:

Expand Down Expand Up @@ -87,6 +90,24 @@ class ProcessingController{
return s;
}

void SetError(bool err, std::string message){
std::lock_guard<std::mutex> lck (mtxerror);
error = err;
error_message = message;
}

bool GetError(){
std::lock_guard<std::mutex> lck (mtxerror);
bool e = error;
return e;
}

std::string GetErrorMessage(){
std::lock_guard<std::mutex> lck (mtxerror);
std::string message = error_message;
return message;
}

};

#endif
2 changes: 1 addition & 1 deletion src/Qt/PlayerPrivate.cpp
Expand Up @@ -195,10 +195,10 @@ namespace openshot
// Stop video/audio playback
void PlayerPrivate::stopPlayback(int timeOutMilliseconds)
{
if (isThreadRunning()) stopThread(timeOutMilliseconds);
if (audioPlayback->isThreadRunning() && reader->info.has_audio) audioPlayback->stopThread(timeOutMilliseconds);
if (videoCache->isThreadRunning() && reader->info.has_video) videoCache->stopThread(timeOutMilliseconds);
if (videoPlayback->isThreadRunning() && reader->info.has_video) videoPlayback->stopThread(timeOutMilliseconds);
if (isThreadRunning()) stopThread(timeOutMilliseconds);
}

}

0 comments on commit a4f4561

Please sign in to comment.