diff --git a/modules/core/include/opencv2/core/mat.hpp b/modules/core/include/opencv2/core/mat.hpp index 9fdc5a9929..65408d5e7c 100644 --- a/modules/core/include/opencv2/core/mat.hpp +++ b/modules/core/include/opencv2/core/mat.hpp @@ -357,6 +357,9 @@ public: void assign(const UMat& u) const; void assign(const Mat& m) const; + + void assign(const std::vector& v) const; + void assign(const std::vector& v) const; }; diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 0208a71894..a85effc4c8 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -3053,6 +3053,82 @@ void _OutputArray::assign(const Mat& m) const } +void _OutputArray::assign(const std::vector& v) const +{ + int k = kind(); + if (k == STD_VECTOR_UMAT) + { + std::vector& this_v = *(std::vector*)obj; + CV_Assert(this_v.size() == v.size()); + + for (size_t i = 0; i < v.size(); i++) + { + const UMat& m = v[i]; + UMat& this_m = this_v[i]; + if (this_m.u != NULL && this_m.u == m.u) + continue; // same object (see dnn::Layer::forward_fallback) + m.copyTo(this_m); + } + } + else if (k == STD_VECTOR_MAT) + { + std::vector& this_v = *(std::vector*)obj; + CV_Assert(this_v.size() == v.size()); + + for (size_t i = 0; i < v.size(); i++) + { + const UMat& m = v[i]; + Mat& this_m = this_v[i]; + if (this_m.u != NULL && this_m.u == m.u) + continue; // same object (see dnn::Layer::forward_fallback) + m.copyTo(this_m); + } + } + else + { + CV_Error(Error::StsNotImplemented, ""); + } +} + + +void _OutputArray::assign(const std::vector& v) const +{ + int k = kind(); + if (k == STD_VECTOR_UMAT) + { + std::vector& this_v = *(std::vector*)obj; + CV_Assert(this_v.size() == v.size()); + + for (size_t i = 0; i < v.size(); i++) + { + const Mat& m = v[i]; + UMat& this_m = this_v[i]; + if (this_m.u != NULL && this_m.u == m.u) + continue; // same object (see dnn::Layer::forward_fallback) + m.copyTo(this_m); + } + } + else if (k == STD_VECTOR_MAT) + { + std::vector& this_v = *(std::vector*)obj; + CV_Assert(this_v.size() == v.size()); + + for (size_t i = 0; i < v.size(); i++) + { + const Mat& m = v[i]; + Mat& this_m = this_v[i]; + if (this_m.u != NULL && this_m.u == m.u) + continue; // same object (see dnn::Layer::forward_fallback) + m.copyTo(this_m); + } + } + else + { + CV_Error(Error::StsNotImplemented, ""); + } +} + + static _InputOutputArray _none; InputOutputArray noArray() { return _none; } diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp index 40edf7aa1b..4faffe1439 100644 --- a/modules/dnn/src/dnn.cpp +++ b/modules/dnn/src/dnn.cpp @@ -2381,6 +2381,10 @@ void Layer::forward_fallback(InputArrayOfArrays inputs_arr, OutputArrayOfArrays inputs[i] = &inpvec[i]; this->forward(inputs, outputs, internals); + + // sync results back + outputs_arr.assign(outputs); + internals_arr.assign(internals); } void Layer::run(const std::vector &inputs, std::vector &outputs, std::vector &internals)