Skip to content

Commit 2f065b8

Browse files
committed
Merge pull request opencv#18509 from alalek:issue_18392
2 parents aece3e7 + a00fe15 commit 2f065b8

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

modules/dnn/src/dnn.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3486,11 +3486,16 @@ void Net::connect(String _outPin, String _inPin)
34863486
Mat Net::forward(const String& outputName)
34873487
{
34883488
CV_TRACE_FUNCTION();
3489+
CV_Assert(!empty());
34893490

34903491
String layerName = outputName;
34913492

34923493
if (layerName.empty())
3493-
layerName = getLayerNames().back();
3494+
{
3495+
std::vector<String> layerNames = getLayerNames();
3496+
CV_Assert(!layerNames.empty());
3497+
layerName = layerNames.back();
3498+
}
34943499

34953500
std::vector<LayerPin> pins(1, impl->getPinByAlias(layerName));
34963501
impl->setUpNet(pins);
@@ -3502,11 +3507,17 @@ Mat Net::forward(const String& outputName)
35023507
AsyncArray Net::forwardAsync(const String& outputName)
35033508
{
35043509
CV_TRACE_FUNCTION();
3510+
CV_Assert(!empty());
3511+
35053512
#ifdef CV_CXX11
35063513
String layerName = outputName;
35073514

35083515
if (layerName.empty())
3509-
layerName = getLayerNames().back();
3516+
{
3517+
std::vector<String> layerNames = getLayerNames();
3518+
CV_Assert(!layerNames.empty());
3519+
layerName = layerNames.back();
3520+
}
35103521

35113522
std::vector<LayerPin> pins(1, impl->getPinByAlias(layerName));
35123523
impl->setUpNet(pins);
@@ -3527,11 +3538,16 @@ AsyncArray Net::forwardAsync(const String& outputName)
35273538
void Net::forward(OutputArrayOfArrays outputBlobs, const String& outputName)
35283539
{
35293540
CV_TRACE_FUNCTION();
3541+
CV_Assert(!empty());
35303542

35313543
String layerName = outputName;
35323544

35333545
if (layerName.empty())
3534-
layerName = getLayerNames().back();
3546+
{
3547+
std::vector<String> layerNames = getLayerNames();
3548+
CV_Assert(!layerNames.empty());
3549+
layerName = layerNames.back();
3550+
}
35353551

35363552
std::vector<LayerPin> pins(1, impl->getPinByAlias(layerName));
35373553
impl->setUpNet(pins);
@@ -4118,6 +4134,8 @@ std::vector<Ptr<Layer> > Net::getLayerInputs(LayerId layerId)
41184134

41194135
std::vector<String> Net::getLayerNames() const
41204136
{
4137+
CV_TRACE_FUNCTION();
4138+
41214139
std::vector<String> res;
41224140
res.reserve(impl->layers.size());
41234141

modules/dnn/test/test_misc.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ TEST(readNet, do_not_call_setInput) // https://github.com/opencv/opencv/issues/
9999
EXPECT_TRUE(res.empty()) << res.size;
100100
}
101101

102+
TEST(Net, empty_forward_18392)
103+
{
104+
cv::dnn::Net net;
105+
Mat image(Size(512, 512), CV_8UC3, Scalar::all(0));
106+
Mat inputBlob = cv::dnn::blobFromImage(image, 1.0, Size(512, 512), Scalar(0,0,0), true, false);
107+
net.setInput(inputBlob);
108+
EXPECT_ANY_THROW(Mat output = net.forward());
109+
}
110+
102111
#ifdef HAVE_INF_ENGINE
103112
static
104113
void test_readNet_IE_do_not_call_setInput(Backend backendId)

0 commit comments

Comments
 (0)