18 object_detection::object_detection(
20 double detectionThreshold,
22 int maxBatch,
bool isBatchDynamic,
23 bool doRawOutputMessages
25 :
base_detection(
"object detection", pathToModel, maxBatch, isBatchDynamic, isAsync, doRawOutputMessages )
26 , _detection_threshold( detectionThreshold )
28 , _n_enqued_frames( 0 ), _width( 0 ), _height( 0 )
50 _width =
static_cast<float>(frame.cols);
51 _height =
static_cast<float>(frame.rows);
54 matU8ToBlob<uint8_t>( frame, inputBlob );
61 float *
p = infoBlob->buffer().as< PrecisionTrait< Precision::FP32 >::value_type * >();
76 CNNNetReader netReader;
78 netReader.ReadNetwork( pathToModel );
81 netReader.getNetwork().setBatchSize(
maxBatch );
85 netReader.ReadWeights( binFileName );
88 InputsDataMap inputInfo( netReader.getNetwork().getInputsInfo() );
89 if( inputInfo.size() != 1 && inputInfo.size() != 2 )
90 throw std::logic_error(
"Object detection network should have only one or two inputs" );
91 for(
auto & item : inputInfo )
93 if( item.second->getInputData()->getTensorDesc().getDims().size() == 4 )
97 _input_width = item.second->getTensorDesc().getDims()[2];
99 item.second->setPrecision( Precision::U8 );
101 else if( item.second->getInputData()->getTensorDesc().getDims().size() == 2 )
105 auto const & dims = item.second->getTensorDesc().getDims();
107 throw std::logic_error(
"Invalid input info: layer \"" +
_im_info_name +
"\" should be 1x3 or 1x6" );
109 item.second->setPrecision( Precision::FP32 );
111 throw std::logic_error(
"Invalid input info: layer \"" +
_im_info_name +
"\" should be 1x3 or 1x6" );
115 throw std::logic_error(
"Could not find input \"data\" layer in network" );
118 OutputsDataMap outputInfo( netReader.getNetwork().getOutputsInfo() );
119 if( outputInfo.size() != 1 )
120 throw std::logic_error(
121 "Object detection network should have only one output" );
123 DataPtr & outputDataPtr = outputInfo.begin()->second;
124 const CNNLayerPtr outputLayer = netReader.getNetwork().getLayerByName(
_output_layer_name.c_str() );
125 if( outputLayer->type !=
"DetectionOutput" )
126 throw std::logic_error(
127 "Object detection network output layer(" + outputLayer->name +
128 ") should be DetectionOutput, but was " + outputLayer->type );
129 if( outputLayer->params.find(
"num_classes" ) == outputLayer->params.end() )
130 throw std::logic_error(
131 "Object detection network output layer (" +
143 const SizeVector & outputDims = outputDataPtr->getTensorDesc().getDims();
144 if( outputDims.size() != 4 )
145 throw std::logic_error(
146 "Object detection network output dimensions should be 4, but was " +
std::to_string( outputDims.size() ) );
147 size_t objectSize = outputDims[3];
148 if( objectSize != DETECTED_OBJECT_SIZE )
149 throw std::logic_error(
150 "Object detection network output layer last dimension should be " +
153 outputDataPtr->setPrecision( Precision::FP32 );
155 return netReader.getNetwork();
161 std::vector< Result > results;
166 float image_id = detections[
i * DETECTED_OBJECT_SIZE + 0];
172 r.
label =
static_cast<int>(detections[
i * DETECTED_OBJECT_SIZE + 1]);
173 r.
confidence = detections[
i * DETECTED_OBJECT_SIZE + 2];
176 r.
location.x =
static_cast<int>(detections[
i * DETECTED_OBJECT_SIZE + 3] *
_width);
177 r.
location.y =
static_cast<int>(detections[
i * DETECTED_OBJECT_SIZE + 4] *
_height);
191 results.push_back( r );
InferenceEngine::InferRequest::Ptr _request
std::string remove_ext(const std::string &filepath)
InferenceEngine::ExecutableNetwork net
std::string _output_layer_name
LOG(INFO)<< "Log message to default logger"
GLsizei const GLchar *const * string
const size_t DETECTED_OBJECT_SIZE
virtual void submit_request()
std::string _input_layer_name
double _detection_threshold
const bool doRawOutputMessages
InferenceEngine::CNNNetwork read_network() override
std::string _im_info_name
void enqueue(const cv::Mat &frame)
void submit_request() override
std::vector< Result > fetch_results()
std::string to_string(T value)