1 #include <visp3/core/vpConfig.h> 3 #include <visp3/detection/vpDetectorDNN.h> 4 #include <visp3/gui/vpDisplayGDI.h> 5 #include <visp3/gui/vpDisplayOpenCV.h> 6 #include <visp3/gui/vpDisplayX.h> 8 int main(
int argc,
const char *argv[])
10 #if (VISP_HAVE_OPENCV_VERSION >= 0x030403) 13 std::string input =
"";
15 std::string model =
"opencv_face_detector_uint8.pb";
16 std::string config =
"opencv_face_detector.pbtxt";
18 int inputWidth = 300, inputHeight = 300;
19 double meanR = 104.0, meanG = 177.0, meanB = 123.0;
20 double scaleFactor = 1.0;
22 float confThresh = 0.5f;
23 float nmsThresh = 0.4f;
24 std::string labelFile =
"";
25 for (
int i = 1; i < argc; i++) {
26 if (std::string(argv[i]) ==
"--device" && i+1 < argc) {
27 opt_device = atoi(argv[i+1]);
28 }
else if (std::string(argv[i]) ==
"--input" && i+1 < argc) {
29 input = std::string(argv[i+1]);
30 }
else if (std::string(argv[i]) ==
"--model" && i+1 < argc) {
31 model = std::string(argv[i+1]);
32 }
else if (std::string(argv[i]) ==
"--config" && i+1 < argc) {
33 config = std::string(argv[i+1]);
34 }
else if (std::string(argv[i]) ==
"--width" && i+1 < argc) {
35 inputWidth = atoi(argv[i+1]);
36 }
else if (std::string(argv[i]) ==
"--height" && i+1 < argc) {
37 inputHeight = atoi(argv[i+1]);
38 }
else if (std::string(argv[i]) ==
"--mean" && i+3 < argc) {
39 meanR = atof(argv[i+1]);
40 meanG = atof(argv[i+2]);
41 meanB = atof(argv[i+3]);
42 }
else if (std::string(argv[i]) ==
"--scale" && i+1 < argc) {
43 scaleFactor = atof(argv[i+1]);
44 }
else if (std::string(argv[i]) ==
"--swapRB") {
46 }
else if (std::string(argv[i]) ==
"--confThresh" && i+1 < argc) {
47 confThresh = (float)atof(argv[i+1]);
48 }
else if (std::string(argv[i]) ==
"--nmsThresh" && i+1 < argc) {
49 nmsThresh = (float)atof(argv[i+1]);
50 }
else if (std::string(argv[i]) ==
"--labels" && i+1 < argc) {
51 labelFile = std::string(argv[i+1]);
52 }
else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"-h") {
53 std::cout << argv[0] <<
" --device <camera device number> --input <path to image or video>" 54 " (camera is used if input is empty) --model <path to net trained weights>" 55 " --config <path to net config file>" 56 " --width <blob width> --height <blob height>" 57 " -- mean <meanR meanG meanB> --scale <scale factor>" 58 " --swapRB --confThresh <confidence threshold>" 59 " --nmsThresh <NMS threshold> --labels <path to label file>" << std::endl;
64 std::cout <<
"Model: " << model << std::endl;
65 std::cout <<
"Config: " << config << std::endl;
66 std::cout <<
"Width: " << inputWidth << std::endl;
67 std::cout <<
"Height: " << inputHeight << std::endl;
68 std::cout <<
"Mean: " << meanR <<
", " << meanG <<
", " << meanB << std::endl;
69 std::cout <<
"Scale: " << scaleFactor << std::endl;
70 std::cout <<
"Swap RB? " << swapRB << std::endl;
71 std::cout <<
"Confidence threshold: " << confThresh << std::endl;
72 std::cout <<
"NMS threshold: " << nmsThresh << std::endl;
74 cv::VideoCapture capture;
76 capture.open(opt_device);
82 #if defined(VISP_HAVE_X11) 84 #elif defined(VISP_HAVE_GDI) 86 #elif defined(VISP_HAVE_OPENCV) 94 dnn.
setMean(meanR, meanG, meanB);
101 std::vector<std::string> labels;
102 if (!labelFile.empty()) {
103 std::ifstream f_label(labelFile);
105 while (std::getline(f_label, line)) {
106 labels.push_back(line);
126 std::vector<vpRect> boundingBoxes;
127 dnn.
detect(I, boundingBoxes);
137 for (
size_t i = 0; i < boundingBoxes.size(); i++) {
140 std::ostringstream oss;
142 oss <<
"class: " << classIds[i];
144 oss << labels[classIds[i]];
145 oss <<
" - conf: " << confidences[i];
150 std::ostringstream oss;
151 oss <<
"Detection time: " << t <<
" ms";
160 std::cout << e.
what() << std::endl;
void setMean(double meanR, double meanG, double meanB)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
std::vector< int > getDetectionClassIds(bool afterNMS=true) const
Display for windows using GDI (available on any windows 32 platform).
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
error that can be emited by ViSP classes.
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
void readNet(const std::string &model, const std::string &config="", const std::string &framework="")
const char * what() const
void setSwapRB(bool swapRB)
unsigned int getSize() const
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
void setScaleFactor(double scaleFactor)
void setNMSThreshold(float nmsThreshold)
void setInputSize(int width, int height)
void setConfidenceThreshold(float confThreshold)
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)
virtual bool detect(const vpImage< unsigned char > &I)
std::vector< float > getDetectionConfidence(bool afterNMS=true) const