Beispiel #1
0
        private static void threat_classifier(string datacfg, string cfgfile, string weightfile, int camIndex, string filename)
        {
            float threat = 0;
            float roll   = .2f;

            Console.Write($"Classifier Demo\n");
            Network net = Parser.parse_network_cfg(cfgfile);

            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }
            Network.set_batch_network(net, 1);
            var options = OptionList.read_data_cfg(datacfg);

            Utils.Rand = new Random(2222222);
            using (VideoCapture cap = !string.IsNullOrEmpty(filename)
                ? new VideoCapture(filename)
                : new VideoCapture(camIndex))
            {
                int top = OptionList.option_find_int(options, "top", 1);

                string   nameList = OptionList.option_find_str(options, "names", "");
                string[] names    = Data.Data.get_labels(nameList);

                int[] indexes = new int[top];

                if (!cap.IsOpened)
                {
                    Utils.Error("Couldn't connect to webcam.\n");
                }
                float fps = 0;
                int   i;

                int count = 0;

                while (true)
                {
                    ++count;
                    var sw = new Stopwatch();
                    sw.Start();

                    Image ini = LoadArgs.get_image_from_stream(cap);
                    if (ini.Data.Length == 0)
                    {
                        break;
                    }
                    Image inS = LoadArgs.resize_image(ini, net.W, net.H);

                    Image outo = ini;
                    int   x1   = outo.W / 20;
                    int   y1   = outo.H / 20;
                    int   x2   = 2 * x1;
                    int   y2   = outo.H - outo.H / 20;

                    int border = (int)(.01f * outo.H);
                    int h      = y2 - y1 - 2 * border;
                    int w      = x2 - x1 - 2 * border;

                    float[] predictions = Network.network_predict(net, inS.Data);
                    float   currThreat  = 0;
                    currThreat = predictions[0] * 0f +
                                 predictions[1] * .6f +
                                 predictions[2];
                    threat = roll * currThreat + (1 - roll) * threat;

                    LoadArgs.draw_box_width(outo, x2 + border, (int)(y1 + .02 * h), (int)(x2 + .5 * w), (int)(y1 + .02 * h + border), border, 0, 0,
                                            0);
                    if (threat > .97)
                    {
                        LoadArgs.draw_box_width(outo, (int)(x2 + .5 * w + border),
                                                (int)(y1 + .02 * h - 2 * border),
                                                (int)(x2 + .5 * w + 6 * border),
                                                (int)(y1 + .02 * h + 3 * border), 3 * border, 1, 0, 0);
                    }

                    LoadArgs.draw_box_width(outo, (int)(x2 + .5 * w + border),
                                            (int)(y1 + .02 * h - 2 * border),
                                            (int)(x2 + .5 * w + 6 * border),
                                            (int)(y1 + .02 * h + 3 * border), (int)(.5 * border), 0, 0, 0);
                    LoadArgs.draw_box_width(outo, x2 + border, (int)(y1 + .42 * h), (int)(x2 + .5 * w), (int)(y1 + .42 * h + border), border, 0, 0,
                                            0);
                    if (threat > .57)
                    {
                        LoadArgs.draw_box_width(outo, (int)(x2 + .5 * w + border),
                                                (int)(y1 + .42 * h - 2 * border),
                                                (int)(x2 + .5 * w + 6 * border),
                                                (int)(y1 + .42 * h + 3 * border), 3 * border, 1, 1, 0);
                    }

                    LoadArgs.draw_box_width(outo, (int)(x2 + .5 * w + border),
                                            (int)(y1 + .42 * h - 2 * border),
                                            (int)(x2 + .5 * w + 6 * border),
                                            (int)(y1 + .42 * h + 3 * border), (int)(.5 * border), 0, 0, 0);

                    LoadArgs.draw_box_width(outo, x1, y1, x2, y2, border, 0, 0, 0);
                    for (i = 0; i < threat * h; ++i)
                    {
                        float ratio = (float)i / h;
                        float r     = (ratio < .5f) ? (2 * (ratio)) : 1;
                        float g     = (ratio < .5f) ? 1 : 1 - 2 * (ratio - .5f);
                        LoadArgs.draw_box_width(outo, x1 + border, y2 - border - i, x2 - border, y2 - border - i, 1, r, g, 0);
                    }

                    Network.top_predictions(net, top, indexes);

                    string buff = $"/home/pjreddie/tmp/threat_{count:06}";

                    Console.Write($"\033[2J");
                    Console.Write($"\033[1;1H");
                    Console.Write($"\nFPS:%.0f\n", fps);

                    for (i = 0; i < top; ++i)
                    {
                        int index = indexes[i];
                        Console.Write($"%.1f%%: %s\n", predictions[index] * 100, names[index]);
                    }

                    LoadArgs.show_image(outo, "Threat");
                    CvInvoke.WaitKey(10);

                    sw.Stop();
                    float curr = 1000.0f / sw.ElapsedMilliseconds;
                    fps = .9f * fps + .1f * curr;
                }
            }
        }