#include <fstream>
#include <sstream>
#include "common.hpp"
std::string keys =
"{ help h | | Print help message. }"
"{ @alias | | An alias name of model to extract preprocessing parameters from models.yml file. }"
"{ zoo | models.yml | An optional path to file with preprocessing parameters }"
"{ device | 0 | camera device number. }"
"{ input i | | Path to input image or video file. Skip this argument to capture frames from a camera. }"
"{ framework f | | Optional name of an origin framework of the model. Detect it automatically if it does not set. }"
"{ classes | | Optional path to a text file with names of classes. }"
"{ colors | | Optional path to a text file with colors for an every class. "
"An every color is represented with three values from 0 to 255 in BGR channels order. }"
"{ backend | 0 | Choose one of computation backends: "
"0: automatically (by default), "
"1: Halide language (http://halide-lang.org/), "
"2: Intel's Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), "
"3: OpenCV implementation }"
"{ target | 0 | Choose one of target computation devices: "
"0: CPU target (by default), "
"1: OpenCL, "
"2: OpenCL fp16 (half-float precision), "
"3: VPU }";
using namespace dnn;
std::vector<std::string> classes;
std::vector<Vec3b> colors;
void showLegend();
void colorizeSegmentation(
const Mat &score,
Mat &segm);
int main(int argc, char** argv)
{
const std::string modelName = parser.get<
String>(
"@alias");
const std::string zooFile = parser.get<
String>(
"zoo");
keys += genPreprocArguments(modelName, zooFile);
parser.about("Use this script to run semantic segmentation deep learning networks using OpenCV.");
if (argc == 1 || parser.has("help"))
{
parser.printMessage();
return 0;
}
float scale = parser.get<float>("scale");
bool swapRB = parser.get<bool>("rgb");
int inpWidth = parser.get<int>("width");
int inpHeight = parser.get<int>("height");
int backendId = parser.get<int>("backend");
int targetId = parser.get<int>("target");
if (parser.has("classes"))
{
std::string file = parser.get<
String>(
"classes");
std::ifstream ifs(file.c_str());
if (!ifs.is_open())
while (std::getline(ifs, line))
{
classes.push_back(line);
}
}
if (parser.has("colors"))
{
std::string file = parser.get<
String>(
"colors");
std::ifstream ifs(file.c_str());
if (!ifs.is_open())
while (std::getline(ifs, line))
{
std::istringstream colorStr(line.c_str());
for (int i = 0; i < 3 && !colorStr.eof(); ++i)
colorStr >> color[i];
colors.push_back(color);
}
}
if (!parser.check())
{
parser.printErrors();
return 1;
}
Net net =
readNet(model, config, framework);
net.setPreferableBackend(backendId);
net.setPreferableTarget(targetId);
static const std::string kWinName = "Deep learning semantic segmentation in OpenCV";
if (parser.has("input"))
else
cap.
open(parser.get<
int>(
"device"));
{
cap >> frame;
if (frame.empty())
{
break;
}
net.setInput(blob);
Mat score = net.forward();
colorizeSegmentation(score, segm);
std::vector<double> layersTimes;
double t = net.getPerfProfile(layersTimes) / freq;
std::string label = format("Inference time: %.2f ms", t);
if (!classes.empty())
showLegend();
}
return 0;
}
void colorizeSegmentation(
const Mat &score,
Mat &segm)
{
const int rows = score.
size[2];
const int cols = score.
size[3];
const int chns = score.
size[1];
if (colors.empty())
{
colors.push_back(
Vec3b());
for (int i = 1; i < chns; ++i)
{
for (int j = 0; j < 3; ++j)
color[j] = (colors[i - 1][j] + rand() % 256) / 2;
colors.push_back(color);
}
}
else if (chns != (int)colors.size())
{
"number of colors (%d != %d)", chns, colors.size()));
}
for (int ch = 1; ch < chns; ch++)
{
for (int row = 0; row < rows; row++)
{
const float *ptrScore = score.
ptr<
float>(0, ch, row);
float *ptrMaxVal = maxVal.ptr<float>(row);
for (int col = 0; col < cols; col++)
{
if (ptrScore[col] > ptrMaxVal[col])
{
ptrMaxVal[col] = ptrScore[col];
ptrMaxCl[col] = (
uchar)ch;
}
}
}
}
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
ptrSegm[col] = colors[ptrMaxCl[col]];
}
}
}
void showLegend()
{
static const int kBlockHeight = 30;
{
const int numClasses = (int)classes.size();
if ((int)colors.size() != numClasses)
{
"number of labels (%d != %d)", colors.size(), classes.size()));
}
for (int i = 0; i < numClasses; i++)
{
Mat block = legend.
rowRange(i * kBlockHeight, (i + 1) * kBlockHeight);
}
}
}