|
|
|
@ -22,6 +22,7 @@ const char* keys = |
|
|
|
|
"{ height | -1 | Preprocess input image by resizing to a specific height. }" |
|
|
|
|
"{ rgb | | Indicate that model works with RGB input images instead BGR ones. }" |
|
|
|
|
"{ thr | .5 | Confidence threshold. }" |
|
|
|
|
"{ thr | .4 | Non-maximum suppression threshold. }" |
|
|
|
|
"{ backend | 0 | Choose one of computation backends: " |
|
|
|
|
"0: automatically (by default), " |
|
|
|
|
"1: Halide language (http://halide-lang.org/), " |
|
|
|
@ -37,7 +38,7 @@ const char* keys = |
|
|
|
|
using namespace cv; |
|
|
|
|
using namespace dnn; |
|
|
|
|
|
|
|
|
|
float confThreshold; |
|
|
|
|
float confThreshold, nmsThreshold; |
|
|
|
|
std::vector<std::string> classes; |
|
|
|
|
|
|
|
|
|
void postprocess(Mat& frame, const std::vector<Mat>& out, Net& net); |
|
|
|
@ -59,6 +60,7 @@ int main(int argc, char** argv) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
confThreshold = parser.get<float>("thr"); |
|
|
|
|
nmsThreshold = parser.get<float>("nms"); |
|
|
|
|
float scale = parser.get<float>("scale"); |
|
|
|
|
Scalar mean = parser.get<Scalar>("mean"); |
|
|
|
|
bool swapRB = parser.get<bool>("rgb"); |
|
|
|
@ -144,6 +146,9 @@ void postprocess(Mat& frame, const std::vector<Mat>& outs, Net& net) |
|
|
|
|
static std::vector<int> outLayers = net.getUnconnectedOutLayers(); |
|
|
|
|
static std::string outLayerType = net.getLayer(outLayers[0])->type; |
|
|
|
|
|
|
|
|
|
std::vector<int> classIds; |
|
|
|
|
std::vector<float> confidences; |
|
|
|
|
std::vector<Rect> boxes; |
|
|
|
|
if (net.getLayer(0)->outputNameToIndex("im_info") != -1) // Faster-RCNN or R-FCN
|
|
|
|
|
{ |
|
|
|
|
// Network produces output blob with a shape 1x1xNx7 where N is a number of
|
|
|
|
@ -160,8 +165,11 @@ void postprocess(Mat& frame, const std::vector<Mat>& outs, Net& net) |
|
|
|
|
int top = (int)data[i + 4]; |
|
|
|
|
int right = (int)data[i + 5]; |
|
|
|
|
int bottom = (int)data[i + 6]; |
|
|
|
|
int classId = (int)(data[i + 1]) - 1; // Skip 0th background class id.
|
|
|
|
|
drawPred(classId, confidence, left, top, right, bottom, frame); |
|
|
|
|
int width = right - left + 1; |
|
|
|
|
int height = bottom - top + 1; |
|
|
|
|
classIds.push_back((int)(data[i + 1]) - 1); // Skip 0th background class id.
|
|
|
|
|
boxes.push_back(Rect(left, top, width, height)); |
|
|
|
|
confidences.push_back(confidence); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -181,16 +189,16 @@ void postprocess(Mat& frame, const std::vector<Mat>& outs, Net& net) |
|
|
|
|
int top = (int)(data[i + 4] * frame.rows); |
|
|
|
|
int right = (int)(data[i + 5] * frame.cols); |
|
|
|
|
int bottom = (int)(data[i + 6] * frame.rows); |
|
|
|
|
int classId = (int)(data[i + 1]) - 1; // Skip 0th background class id.
|
|
|
|
|
drawPred(classId, confidence, left, top, right, bottom, frame); |
|
|
|
|
int width = right - left + 1; |
|
|
|
|
int height = bottom - top + 1; |
|
|
|
|
classIds.push_back((int)(data[i + 1]) - 1); // Skip 0th background class id.
|
|
|
|
|
boxes.push_back(Rect(left, top, width, height)); |
|
|
|
|
confidences.push_back(confidence); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else if (outLayerType == "Region") |
|
|
|
|
{ |
|
|
|
|
std::vector<int> classIds; |
|
|
|
|
std::vector<float> confidences; |
|
|
|
|
std::vector<Rect> boxes; |
|
|
|
|
for (size_t i = 0; i < outs.size(); ++i) |
|
|
|
|
{ |
|
|
|
|
// Network produces output blob with a shape NxC where N is a number of
|
|
|
|
@ -218,8 +226,12 @@ void postprocess(Mat& frame, const std::vector<Mat>& outs, Net& net) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
CV_Error(Error::StsNotImplemented, "Unknown output layer type: " + outLayerType); |
|
|
|
|
|
|
|
|
|
std::vector<int> indices; |
|
|
|
|
NMSBoxes(boxes, confidences, confThreshold, 0.4f, indices); |
|
|
|
|
NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices); |
|
|
|
|
for (size_t i = 0; i < indices.size(); ++i) |
|
|
|
|
{ |
|
|
|
|
int idx = indices[i]; |
|
|
|
@ -228,9 +240,6 @@ void postprocess(Mat& frame, const std::vector<Mat>& outs, Net& net) |
|
|
|
|
box.x + box.width, box.y + box.height, frame); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
CV_Error(Error::StsNotImplemented, "Unknown output layer type: " + outLayerType); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame) |
|
|
|
|
{ |
|
|
|
|