diff --git a/modules/dnn/include/opencv2/dnn/dnn.hpp b/modules/dnn/include/opencv2/dnn/dnn.hpp index 680e7d54d195bb0da8a8aa787f7cb8cf371ed614..d11b3a5c460ed2a40fc68623745af93ad469cd55 100644 --- a/modules/dnn/include/opencv2/dnn/dnn.hpp +++ b/modules/dnn/include/opencv2/dnn/dnn.hpp @@ -535,6 +535,11 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN /** @brief Returns indexes of layers with unconnected outputs. */ CV_WRAP std::vector getUnconnectedOutLayers() const; + + /** @brief Returns names of layers with unconnected outputs. + */ + CV_WRAP std::vector getUnconnectedOutLayersNames() const; + /** @brief Returns input and output shapes for all layers in loaded model; * preliminary inferencing isn't necessary. * @param netInputShapes shapes for all input blobs in net input layer. diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp index e48659e31c9cbdc1f97143c3aece5da7725a445d..16fb06cca551e92e5ba810f074a21e9c110d34f7 100644 --- a/modules/dnn/src/dnn.cpp +++ b/modules/dnn/src/dnn.cpp @@ -2789,6 +2789,18 @@ std::vector Net::getUnconnectedOutLayers() const return layersIds; } +std::vector Net::getUnconnectedOutLayersNames() const +{ + std::vector ids = getUnconnectedOutLayers(); + const size_t n = ids.size(); + std::vector names(n); + for (size_t i = 0; i < n; ++i) + { + names[i] = impl->layers[ids[i]].name; + } + return names; +} + void Net::getLayersShapes(const ShapesVec& netInputShapes, std::vector& layersIds, std::vector& inLayersShapes, diff --git a/samples/dnn/object_detection.cpp b/samples/dnn/object_detection.cpp index 161f7434f8cebdcea56eb043e3daac4c68cfffb4..eddac3a14344ac6bdcc126a81ea2adae2c85834c 100644 --- a/samples/dnn/object_detection.cpp +++ b/samples/dnn/object_detection.cpp @@ -86,6 +86,7 @@ int main(int argc, char** argv) Net net = readNet(parser.get("model"), parser.get("config"), parser.get("framework")); net.setPreferableBackend(parser.get("backend")); net.setPreferableTarget(parser.get("target")); + std::vector outNames = net.getUnconnectedOutLayersNames(); // Create a window static const std::string kWinName = "Deep learning object detection in OpenCV"; @@ -125,7 +126,7 @@ int main(int argc, char** argv) net.setInput(imInfo, "im_info"); } std::vector outs; - net.forward(outs, getOutputsNames(net)); + net.forward(outs, outNames); postprocess(frame, outs, net); @@ -265,17 +266,3 @@ void callback(int pos, void*) { confThreshold = pos * 0.01f; } - -std::vector getOutputsNames(const Net& net) -{ - static std::vector names; - if (names.empty()) - { - std::vector outLayers = net.getUnconnectedOutLayers(); - std::vector layersNames = net.getLayerNames(); - names.resize(outLayers.size()); - for (size_t i = 0; i < outLayers.size(); ++i) - names[i] = layersNames[outLayers[i] - 1]; - } - return names; -} diff --git a/samples/dnn/object_detection.py b/samples/dnn/object_detection.py index 76c33f8e3b62140bd8ae1d2f017bb1f32a0ca6f1..b97d8df66946f3d90b549ee235c914c6c7c15b85 100644 --- a/samples/dnn/object_detection.py +++ b/samples/dnn/object_detection.py @@ -78,14 +78,11 @@ if args.classes: net = cv.dnn.readNet(args.model, args.config, args.framework) net.setPreferableBackend(args.backend) net.setPreferableTarget(args.target) +outNames = net.getUnconnectedOutLayersNames() confThreshold = args.thr nmsThreshold = args.nms -def getOutputsNames(net): - layersNames = net.getLayerNames() - return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()] - def postprocess(frame, outs): frameHeight = frame.shape[0] frameWidth = frame.shape[1] @@ -213,7 +210,7 @@ while cv.waitKey(1) < 0: if net.getLayer(0).outputNameToIndex('im_info') != -1: # Faster-RCNN or R-FCN frame = cv.resize(frame, (inpWidth, inpHeight)) net.setInput(np.array([[inpHeight, inpWidth, 1.6]], dtype=np.float32), 'im_info') - outs = net.forward(getOutputsNames(net)) + outs = net.forward(outNames) postprocess(frame, outs)