Commit 2113818f authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

Merge pull request #12364 from dkurt:dnn_change_blob_from_image

parents 29bee6f0 58ac3e09
...@@ -46,9 +46,9 @@ ...@@ -46,9 +46,9 @@
#include <opencv2/core.hpp> #include <opencv2/core.hpp>
#if !defined CV_DOXYGEN && !defined CV_DNN_DONT_ADD_EXPERIMENTAL_NS #if !defined CV_DOXYGEN && !defined CV_DNN_DONT_ADD_EXPERIMENTAL_NS
#define CV__DNN_EXPERIMENTAL_NS_BEGIN namespace experimental_dnn_34_v8 { #define CV__DNN_EXPERIMENTAL_NS_BEGIN namespace experimental_dnn_34_v9 {
#define CV__DNN_EXPERIMENTAL_NS_END } #define CV__DNN_EXPERIMENTAL_NS_END }
namespace cv { namespace dnn { namespace experimental_dnn_34_v8 { } using namespace experimental_dnn_34_v8; }} namespace cv { namespace dnn { namespace experimental_dnn_34_v9 { } using namespace experimental_dnn_34_v9; }}
#else #else
#define CV__DNN_EXPERIMENTAL_NS_BEGIN #define CV__DNN_EXPERIMENTAL_NS_BEGIN
#define CV__DNN_EXPERIMENTAL_NS_END #define CV__DNN_EXPERIMENTAL_NS_END
...@@ -843,7 +843,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN ...@@ -843,7 +843,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
* @returns 4-dimensional Mat with NCHW dimensions order. * @returns 4-dimensional Mat with NCHW dimensions order.
*/ */
CV_EXPORTS_W Mat blobFromImage(InputArray image, double scalefactor=1.0, const Size& size = Size(), CV_EXPORTS_W Mat blobFromImage(InputArray image, double scalefactor=1.0, const Size& size = Size(),
const Scalar& mean = Scalar(), bool swapRB=true, bool crop=true, const Scalar& mean = Scalar(), bool swapRB=false, bool crop=false,
int ddepth=CV_32F); int ddepth=CV_32F);
/** @brief Creates 4-dimensional blob from image. /** @brief Creates 4-dimensional blob from image.
...@@ -852,7 +852,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN ...@@ -852,7 +852,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
*/ */
CV_EXPORTS void blobFromImage(InputArray image, OutputArray blob, double scalefactor=1.0, CV_EXPORTS void blobFromImage(InputArray image, OutputArray blob, double scalefactor=1.0,
const Size& size = Size(), const Scalar& mean = Scalar(), const Size& size = Size(), const Scalar& mean = Scalar(),
bool swapRB=true, bool crop=true, int ddepth=CV_32F); bool swapRB=false, bool crop=false, int ddepth=CV_32F);
/** @brief Creates 4-dimensional blob from series of images. Optionally resizes and /** @brief Creates 4-dimensional blob from series of images. Optionally resizes and
...@@ -873,7 +873,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN ...@@ -873,7 +873,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
* @returns 4-dimensional Mat with NCHW dimensions order. * @returns 4-dimensional Mat with NCHW dimensions order.
*/ */
CV_EXPORTS_W Mat blobFromImages(InputArrayOfArrays images, double scalefactor=1.0, CV_EXPORTS_W Mat blobFromImages(InputArrayOfArrays images, double scalefactor=1.0,
Size size = Size(), const Scalar& mean = Scalar(), bool swapRB=true, bool crop=true, Size size = Size(), const Scalar& mean = Scalar(), bool swapRB=false, bool crop=false,
int ddepth=CV_32F); int ddepth=CV_32F);
/** @brief Creates 4-dimensional blob from series of images. /** @brief Creates 4-dimensional blob from series of images.
...@@ -882,7 +882,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN ...@@ -882,7 +882,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
*/ */
CV_EXPORTS void blobFromImages(InputArrayOfArrays images, OutputArray blob, CV_EXPORTS void blobFromImages(InputArrayOfArrays images, OutputArray blob,
double scalefactor=1.0, Size size = Size(), double scalefactor=1.0, Size size = Size(),
const Scalar& mean = Scalar(), bool swapRB=true, bool crop=true, const Scalar& mean = Scalar(), bool swapRB=false, bool crop=false,
int ddepth=CV_32F); int ddepth=CV_32F);
/** @brief Parse a 4D blob and output the images it contains as 2D arrays through a simpler data structure /** @brief Parse a 4D blob and output the images it contains as 2D arrays through a simpler data structure
......
...@@ -307,7 +307,7 @@ TEST_P(Reproducibility_SqueezeNet_v1_1, Accuracy) ...@@ -307,7 +307,7 @@ TEST_P(Reproducibility_SqueezeNet_v1_1, Accuracy)
net.setPreferableBackend(DNN_BACKEND_OPENCV); net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(targetId); net.setPreferableTarget(targetId);
Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(227,227), Scalar(), false); Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(227,227), Scalar(), false, true);
ASSERT_TRUE(!input.empty()); ASSERT_TRUE(!input.empty());
Mat out; Mat out;
...@@ -403,7 +403,7 @@ TEST_P(Test_Caffe_nets, DenseNet_121) ...@@ -403,7 +403,7 @@ TEST_P(Test_Caffe_nets, DenseNet_121)
const string model = findDataFile("dnn/DenseNet_121.caffemodel", false); const string model = findDataFile("dnn/DenseNet_121.caffemodel", false);
Mat inp = imread(_tf("dog416.png")); Mat inp = imread(_tf("dog416.png"));
inp = blobFromImage(inp, 1.0 / 255, Size(224, 224)); inp = blobFromImage(inp, 1.0 / 255, Size(224, 224), Scalar(), true, true);
Mat ref = blobFromNPY(_tf("densenet_121_output.npy")); Mat ref = blobFromNPY(_tf("densenet_121_output.npy"));
Net net = readNetFromCaffe(proto, model); Net net = readNetFromCaffe(proto, model);
......
...@@ -62,8 +62,7 @@ TEST(Test_TensorFlow, inception_accuracy) ...@@ -62,8 +62,7 @@ TEST(Test_TensorFlow, inception_accuracy)
Mat sample = imread(_tf("grace_hopper_227.png")); Mat sample = imread(_tf("grace_hopper_227.png"));
ASSERT_TRUE(!sample.empty()); ASSERT_TRUE(!sample.empty());
resize(sample, sample, Size(224, 224)); Mat inputBlob = blobFromImage(sample, 1.0, Size(224, 224), Scalar(), /*swapRB*/true);
Mat inputBlob = blobFromImage(sample);
net.setInput(inputBlob, "input"); net.setInput(inputBlob, "input");
Mat out = net.forward("softmax2"); Mat out = net.forward("softmax2");
......
...@@ -278,7 +278,7 @@ TEST_P(Test_Torch_nets, OpenFace_accuracy) ...@@ -278,7 +278,7 @@ TEST_P(Test_Torch_nets, OpenFace_accuracy)
sampleF32 /= 255; sampleF32 /= 255;
resize(sampleF32, sampleF32, Size(96, 96), 0, 0, INTER_NEAREST); resize(sampleF32, sampleF32, Size(96, 96), 0, 0, INTER_NEAREST);
Mat inputBlob = blobFromImage(sampleF32); Mat inputBlob = blobFromImage(sampleF32, 1.0, Size(), Scalar(), /*swapRB*/true);
net.setInput(inputBlob); net.setInput(inputBlob);
Mat out = net.forward(); Mat out = net.forward();
...@@ -305,7 +305,7 @@ TEST_P(Test_Torch_nets, ENet_accuracy) ...@@ -305,7 +305,7 @@ TEST_P(Test_Torch_nets, ENet_accuracy)
net.setPreferableTarget(target); net.setPreferableTarget(target);
Mat sample = imread(_tf("street.png", false)); Mat sample = imread(_tf("street.png", false));
Mat inputBlob = blobFromImage(sample, 1./255); Mat inputBlob = blobFromImage(sample, 1./255, Size(), Scalar(), /*swapRB*/true);
net.setInput(inputBlob, ""); net.setInput(inputBlob, "");
Mat out = net.forward(); Mat out = net.forward();
......
...@@ -86,29 +86,13 @@ public class MainActivity extends AppCompatActivity implements CvCameraViewListe ...@@ -86,29 +86,13 @@ public class MainActivity extends AppCompatActivity implements CvCameraViewListe
// Forward image through network. // Forward image through network.
Mat blob = Dnn.blobFromImage(frame, IN_SCALE_FACTOR, Mat blob = Dnn.blobFromImage(frame, IN_SCALE_FACTOR,
new Size(IN_WIDTH, IN_HEIGHT), new Size(IN_WIDTH, IN_HEIGHT),
new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), false); new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), /*swapRB*/false, /*crop*/false);
net.setInput(blob); net.setInput(blob);
Mat detections = net.forward(); Mat detections = net.forward();
int cols = frame.cols(); int cols = frame.cols();
int rows = frame.rows(); int rows = frame.rows();
Size cropSize;
if ((float)cols / rows > WH_RATIO) {
cropSize = new Size(rows * WH_RATIO, rows);
} else {
cropSize = new Size(cols, cols / WH_RATIO);
}
int y1 = (int)(rows - cropSize.height) / 2;
int y2 = (int)(y1 + cropSize.height);
int x1 = (int)(cols - cropSize.width) / 2;
int x2 = (int)(x1 + cropSize.width);
Mat subFrame = frame.submat(y1, y2, x1, x2);
cols = subFrame.cols();
rows = subFrame.rows();
detections = detections.reshape(1, (int)detections.total() / 7); detections = detections.reshape(1, (int)detections.total() / 7);
for (int i = 0; i < detections.rows(); ++i) { for (int i = 0; i < detections.rows(); ++i) {
...@@ -116,26 +100,24 @@ public class MainActivity extends AppCompatActivity implements CvCameraViewListe ...@@ -116,26 +100,24 @@ public class MainActivity extends AppCompatActivity implements CvCameraViewListe
if (confidence > THRESHOLD) { if (confidence > THRESHOLD) {
int classId = (int)detections.get(i, 1)[0]; int classId = (int)detections.get(i, 1)[0];
int xLeftBottom = (int)(detections.get(i, 3)[0] * cols); int left = (int)(detections.get(i, 3)[0] * cols);
int yLeftBottom = (int)(detections.get(i, 4)[0] * rows); int top = (int)(detections.get(i, 4)[0] * rows);
int xRightTop = (int)(detections.get(i, 5)[0] * cols); int right = (int)(detections.get(i, 5)[0] * cols);
int yRightTop = (int)(detections.get(i, 6)[0] * rows); int bottom = (int)(detections.get(i, 6)[0] * rows);
// Draw rectangle around detected object. // Draw rectangle around detected object.
Imgproc.rectangle(subFrame, new Point(xLeftBottom, yLeftBottom), Imgproc.rectangle(frame, new Point(left, top), new Point(right, bottom),
new Point(xRightTop, yRightTop),
new Scalar(0, 255, 0)); new Scalar(0, 255, 0));
String label = classNames[classId] + ": " + confidence; String label = classNames[classId] + ": " + confidence;
int[] baseLine = new int[1]; int[] baseLine = new int[1];
Size labelSize = Imgproc.getTextSize(label, Core.FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine); Size labelSize = Imgproc.getTextSize(label, Core.FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine);
// Draw background for label. // Draw background for label.
Imgproc.rectangle(subFrame, new Point(xLeftBottom, yLeftBottom - labelSize.height), Imgproc.rectangle(frame, new Point(left, top - labelSize.height),
new Point(xLeftBottom + labelSize.width, yLeftBottom + baseLine[0]), new Point(left + labelSize.width, top + baseLine[0]),
new Scalar(255, 255, 255), Core.FILLED); new Scalar(255, 255, 255), Imgproc.FILLED);
// Write class name and confidence. // Write class name and confidence.
Imgproc.putText(subFrame, label, new Point(xLeftBottom, yLeftBottom), Imgproc.putText(frame, label, new Point(left, top),
Core.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(0, 0, 0)); Core.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(0, 0, 0));
} }
} }
......
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