Unverified Commit 8ab145c4 authored by Xavier Weber's avatar Xavier Weber Committed by GitHub

Merge pull request #2480 from Saafke:dnn_superres_pythonwrap

* Add a python wrap to the dnn_superres module

Adds a python wrap with approriate python bindings and tutorial

Also fixes some minor errors in the tutorials

* Remove trailing whitespaces

* Remove erring python binding

try to fix a windows build error

* Add python tests

Tests model that upscales an image x2

Test getter functions

* Add one more subdirectory

add 'test' subdir
parent 96b38d4a
set(the_description "Super Resolution using CNNs") set(the_description "Super Resolution using CNNs")
ocv_define_module(dnn_superres opencv_core opencv_imgproc opencv_dnn ocv_define_module(
OPTIONAL opencv_quality # samples dnn_superres
) opencv_core
opencv_imgproc
opencv_dnn
OPTIONAL opencv_quality
WRAP python
)
\ No newline at end of file
...@@ -37,7 +37,7 @@ The following four models are implemented: ...@@ -37,7 +37,7 @@ The following four models are implemented:
- lapsrn - lapsrn
*/ */
class CV_EXPORTS DnnSuperResImpl class CV_EXPORTS_W DnnSuperResImpl
{ {
private: private:
...@@ -55,8 +55,12 @@ private: ...@@ -55,8 +55,12 @@ private:
public: public:
/** @brief Empty constructor /** @brief Empty constructor for python
*/ */
CV_WRAP static Ptr<DnnSuperResImpl> create();
// /** @brief Empty constructor
// */
DnnSuperResImpl(); DnnSuperResImpl();
/** @brief Constructor which immediately sets the desired model /** @brief Constructor which immediately sets the desired model
...@@ -67,18 +71,18 @@ public: ...@@ -67,18 +71,18 @@ public:
- __lapsrn__ - __lapsrn__
@param scale Integer specifying the upscale factor @param scale Integer specifying the upscale factor
*/ */
DnnSuperResImpl(const std::string& algo, int scale); DnnSuperResImpl(const String& algo, int scale);
/** @brief Read the model from the given path /** @brief Read the model from the given path
@param path Path to the model file. @param path Path to the model file.
*/ */
void readModel(const std::string& path); CV_WRAP void readModel(const String& path);
/** @brief Read the model from the given path /** @brief Read the model from the given path
@param weights Path to the model weights file. @param weights Path to the model weights file.
@param definition Path to the model definition file. @param definition Path to the model definition file.
*/ */
void readModel(const std::string& weights, const std::string& definition); void readModel(const String& weights, const String& definition);
/** @brief Set desired model /** @brief Set desired model
@param algo String containing one of the desired models: @param algo String containing one of the desired models:
...@@ -88,13 +92,13 @@ public: ...@@ -88,13 +92,13 @@ public:
- __lapsrn__ - __lapsrn__
@param scale Integer specifying the upscale factor @param scale Integer specifying the upscale factor
*/ */
void setModel(const std::string& algo, int scale); CV_WRAP void setModel(const String& algo, int scale);
/** @brief Upsample via neural network /** @brief Upsample via neural network
@param img Image to upscale @param img Image to upscale
@param result Destination upscaled image @param result Destination upscaled image
*/ */
void upsample(InputArray img, OutputArray result); CV_WRAP void upsample(InputArray img, OutputArray result);
/** @brief Upsample via neural network of multiple outputs /** @brief Upsample via neural network of multiple outputs
@param img Image to upscale @param img Image to upscale
...@@ -102,17 +106,17 @@ public: ...@@ -102,17 +106,17 @@ public:
@param scale_factors Scaling factors of the output nodes @param scale_factors Scaling factors of the output nodes
@param node_names Names of the output nodes in the neural network @param node_names Names of the output nodes in the neural network
*/ */
void upsampleMultioutput(InputArray img, std::vector<Mat> &imgs_new, const std::vector<int>& scale_factors, const std::vector<String>& node_names); CV_WRAP void upsampleMultioutput(InputArray img, std::vector<Mat> &imgs_new, const std::vector<int>& scale_factors, const std::vector<String>& node_names);
/** @brief Returns the scale factor of the model: /** @brief Returns the scale factor of the model:
@return Current scale factor. @return Current scale factor.
*/ */
int getScale(); CV_WRAP int getScale();
/** @brief Returns the scale factor of the model: /** @brief Returns the scale factor of the model:
@return Current algorithm. @return Current algorithm.
*/ */
std::string getAlgorithm(); CV_WRAP String getAlgorithm();
}; };
//! @} dnn_superres //! @} dnn_superres
......
#!/usr/bin/env python
import os
import cv2 as cv
from tests_common import NewOpenCVTests, unittest
class test_dnn_superres(NewOpenCVTests):
@unittest.skipIf('OPENCV_TEST_DATA_PATH' not in os.environ,
"OPENCV_TEST_DATA_PATH is not defined")
def test_single_output(self):
# Get test data paths
dnn_superres_test_path = os.environ['OPENCV_TEST_DATA_PATH'] + "/cv/dnn_superres/"
img_path = dnn_superres_test_path + "butterfly.png"
espcn_path = dnn_superres_test_path + "ESPCN_x2.pb"
# Create an SR object
sr = cv.dnn_superres.DnnSuperResImpl_create()
# Read image
image = cv.imread(img_path)
inp_h, inp_w, inp_c = image.shape
# Read the desired model
sr.readModel(espcn_path)
# Set the desired model and scale to get correct pre- and post-processing
sr.setModel("espcn", 2)
# Upscale the image
result = sr.upsample(image)
out_h, out_w, out_c = result.shape
# CHECK...
# if result is not empty
self.assertFalse(result is None)
# upsampled image is correct size
self.assertEqual(out_h, inp_h*2)
self.assertEqual(out_w, inp_w*2)
self.assertEqual(out_c, inp_c)
# get functions work
self.assertEqual(sr.getScale(), 2)
self.assertEqual(sr.getAlgorithm(), "espcn")
if __name__ == '__main__':
NewOpenCVTests.bootstrap()
\ No newline at end of file
...@@ -44,19 +44,22 @@ public: ...@@ -44,19 +44,22 @@ public:
} }
}; };
Ptr<DnnSuperResImpl> DnnSuperResImpl::create(){
return Ptr<DnnSuperResImpl>(new DnnSuperResImpl());
}
DnnSuperResImpl::DnnSuperResImpl() DnnSuperResImpl::DnnSuperResImpl()
{ {
DepthToSpace::registerLayer(); DepthToSpace::registerLayer();
} }
DnnSuperResImpl::DnnSuperResImpl(const std::string& algo, int scale) DnnSuperResImpl::DnnSuperResImpl(const String& algo, int scale)
: alg(algo), sc(scale) : alg(algo), sc(scale)
{ {
DepthToSpace::registerLayer(); DepthToSpace::registerLayer();
} }
void DnnSuperResImpl::readModel(const std::string& path) void DnnSuperResImpl::readModel(const String& path)
{ {
if ( path.size() ) if ( path.size() )
{ {
...@@ -65,11 +68,11 @@ void DnnSuperResImpl::readModel(const std::string& path) ...@@ -65,11 +68,11 @@ void DnnSuperResImpl::readModel(const std::string& path)
} }
else else
{ {
CV_Error(Error::StsBadArg, std::string("Could not load model: ") + path); CV_Error(Error::StsBadArg, String("Could not load model: ") + path);
} }
} }
void DnnSuperResImpl::readModel(const std::string& weights, const std::string& definition) void DnnSuperResImpl::readModel(const String& weights, const String& definition)
{ {
if ( weights.size() && definition.size() ) if ( weights.size() && definition.size() )
{ {
...@@ -78,11 +81,11 @@ void DnnSuperResImpl::readModel(const std::string& weights, const std::string& d ...@@ -78,11 +81,11 @@ void DnnSuperResImpl::readModel(const std::string& weights, const std::string& d
} }
else else
{ {
CV_Error(Error::StsBadArg, std::string("Could not load model: ") + weights + " " + definition); CV_Error(Error::StsBadArg, String("Could not load model: ") + weights + " " + definition);
} }
} }
void DnnSuperResImpl::setModel(const std::string& algo, int scale) void DnnSuperResImpl::setModel(const String& algo, int scale)
{ {
this->sc = scale; this->sc = scale;
this->alg = algo; this->alg = algo;
...@@ -148,7 +151,7 @@ void DnnSuperResImpl::upsample(InputArray img, OutputArray result) ...@@ -148,7 +151,7 @@ void DnnSuperResImpl::upsample(InputArray img, OutputArray result)
} }
else else
{ {
CV_Error(cv::Error::StsNotImplemented, std::string("Unknown/unsupported superres algorithm: ") + this->alg); CV_Error(cv::Error::StsNotImplemented, String("Unknown/unsupported superres algorithm: ") + this->alg);
} }
} }
...@@ -210,7 +213,7 @@ int DnnSuperResImpl::getScale() ...@@ -210,7 +213,7 @@ int DnnSuperResImpl::getScale()
return this->sc; return this->sc;
} }
std::string DnnSuperResImpl::getAlgorithm() String DnnSuperResImpl::getAlgorithm()
{ {
return this->alg; return this->alg;
} }
...@@ -239,7 +242,7 @@ void DnnSuperResImpl::preprocess_YCrCb(InputArray inpImg, OutputArray outImg) ...@@ -239,7 +242,7 @@ void DnnSuperResImpl::preprocess_YCrCb(InputArray inpImg, OutputArray outImg)
} }
else else
{ {
CV_Error(Error::StsBadArg, std::string("Not supported image type: ") + typeToString(inpImg.type())); CV_Error(Error::StsBadArg, String("Not supported image type: ") + typeToString(inpImg.type()));
} }
} }
...@@ -273,7 +276,7 @@ void DnnSuperResImpl::reconstruct_YCrCb(InputArray inpImg, InputArray origImg, O ...@@ -273,7 +276,7 @@ void DnnSuperResImpl::reconstruct_YCrCb(InputArray inpImg, InputArray origImg, O
} }
else else
{ {
CV_Error(Error::StsBadArg, std::string("Not supported image type: ") + typeToString(origImg.type())); CV_Error(Error::StsBadArg, String("Not supported image type: ") + typeToString(origImg.type()));
} }
} }
......
...@@ -21,6 +21,6 @@ Super Resolution using CNNs {#tutorial_table_of_content_dnn_superres} ...@@ -21,6 +21,6 @@ Super Resolution using CNNs {#tutorial_table_of_content_dnn_superres}
- @subpage tutorial_dnn_superres_benchmark - @subpage tutorial_dnn_superres_benchmark
Authors:* Fanny Monori & Xavier Weber *Authors:* Fanny Monori & Xavier Weber
Benchmarking of the algorithms. Benchmarking of the algorithms.
...@@ -13,10 +13,16 @@ The uploaded trained model files have the following output node names: ...@@ -13,10 +13,16 @@ The uploaded trained model files have the following output node names:
Building Building
---- ----
When building OpenCV, run the following command to build the 'dnn_superres' module: When building OpenCV, run the following command to build all the contrib module:
```make ```make
cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules -Dopencv_dnn_superres=ON <opencv_source_dir> cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules/
```
Or only build the dnn_superres module:
```make
cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules/dnn_superres
``` ```
Or make sure you check the dnn_superres module in the GUI version of CMake: cmake-gui. Or make sure you check the dnn_superres module in the GUI version of CMake: cmake-gui.
......
Upscaling images: single-output {#tutorial_dnn_superres_upscale_image_single} Upscaling images: single-output {#tutorial_dnn_superres_upscale_image_single}
=========================== ===========================
In this tutorial you will learn how to use the 'dnn_superres' interface to upscale an image via pre-trained neural networks. In this tutorial you will learn how to use the 'dnn_superres' interface to upscale an image via pre-trained neural networks. It works in C++ and Python.
Building Building
---- ----
When building OpenCV, run the following command to build the 'dnn_superres' module: When building OpenCV, run the following command to build all the contrib module:
```make ```make
cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules -Dopencv_dnn_superres=ON <opencv_source_dir> cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules/
```
Or only build the dnn_superres module:
```make
cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules/dnn_superres
``` ```
Or make sure you check the dnn_superres module in the GUI version of CMake: cmake-gui. Or make sure you check the dnn_superres module in the GUI version of CMake: cmake-gui.
...@@ -17,6 +23,18 @@ Or make sure you check the dnn_superres module in the GUI version of CMake: cmak ...@@ -17,6 +23,18 @@ Or make sure you check the dnn_superres module in the GUI version of CMake: cmak
Source Code of the sample Source Code of the sample
----------- -----------
You can run the sample code by doing
```run
<path_of_your_opencv_build_directory>/bin/example_dnn_superres_dnn_superres <path_to_image.png> <algo_string> <upscale_int> <model_path.pb>
```
Example:
```run
/home/opencv/build/bin/example_dnn_superres_dnn_superres /home/image.png edsr 2 /home/EDSR_x2.pb
```
@includelineno dnn_superres/samples/dnn_superres.cpp @includelineno dnn_superres/samples/dnn_superres.cpp
Explanation Explanation
...@@ -61,6 +79,33 @@ Explanation ...@@ -61,6 +79,33 @@ Explanation
Now we can upscale any image. Load an image via the standard 'imread' function and create a new Mat for the destination image. Then simple Now we can upscale any image. Load an image via the standard 'imread' function and create a new Mat for the destination image. Then simple
upscale. Your upscaled image is located in 'img_new'. upscale. Your upscaled image is located in 'img_new'.
An example in python
-----------
@code{.py}
import cv2
from cv2 import dnn_superres
# Create an SR object - only function that differs from c++ code
sr = dnn_superres.DnnSuperResImpl_create()
# Read image
image = cv2.imread('./image.png')
# Read the desired model
path = "EDSR_x4.pb"
sr.readModel(path)
# Set the desired model and scale to get correct pre- and post-processing
sr.setModel("edsr", 4)
# Upscale the image
result = sr.upsample(image)
# Save the image
cv2.imwrite("./upscaled.png", result)
@endcode
Original: ![](images/input.jpg) Original: ![](images/input.jpg)
Upscaled Image via FSRCNN: ![](images/fsrcnnOutput.jpg) Upscaled Image via FSRCNN: ![](images/fsrcnnOutput.jpg)
Upscaled Image via Bicubic Interpolation: ![](images/bicubicOutput.jpg) Upscaled Image via Bicubic Interpolation: ![](images/bicubicOutput.jpg)
\ No newline at end of file
...@@ -6,10 +6,16 @@ In this tutorial you will learn how to use the 'dnn_superres' interface to upsca ...@@ -6,10 +6,16 @@ In this tutorial you will learn how to use the 'dnn_superres' interface to upsca
Building Building
---- ----
When building OpenCV, run the following command to build the 'dnn_superres' module: When building OpenCV, run the following command to build all the contrib module:
```make ```make
cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules -Dopencv_dnn_superres=ON <opencv_source_dir> cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules/
```
Or only build the dnn_superres module:
```make
cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules/dnn_superres
``` ```
Or make sure you check the dnn_superres module in the GUI version of CMake: cmake-gui. Or make sure you check the dnn_superres module in the GUI version of CMake: cmake-gui.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment