Example #1
0
    /// <summary>
    /// Finds and classifies detections for all frames in a video.
    /// </summary>
    /// <param name="detect_path">Path to detect model file.</param>
    /// <param name="classify_path">Path to classify model file.</param>
    /// <param name="roi">Region of interest output from FindVideoRoi.</param>
    /// <param name="endpoints">Ruler endpoints output from FindVideoRoi.</param>
    /// <param name="detections">Detections for each frame.</param>
    /// <param name="scores">Cover and species scores for each detection.</param>
    static void DetectAndClassify(
        string detect_path,
        string classify_path,
        string vid_path,
        Rect roi,
        PointPair endpoints,
        out VectorVectorDetection detections,
        out VectorVectorClassification scores)
    {
        // Determined by experimentation with GPU having 8GB memory.
        const int kMaxImg = 32;

        // Initialize the outputs.
        detections = new VectorVectorDetection();
        scores     = new VectorVectorClassification();

        // Create and initialize the detector.
        Detector  detector = new Detector();
        ErrorCode status   = detector.Init(detect_path, 0.5);

        if (status != ErrorCode.kSuccess)
        {
            throw new Exception("Failed to initialize detector!");
        }

        // Create and initialize the classifier.
        Classifier classifier = new Classifier();

        status = classifier.Init(classify_path, 0.5);
        if (status != ErrorCode.kSuccess)
        {
            throw new Exception("Failed to initialize classifier!");
        }

        // Initialize the video reader.
        VideoReader reader = new VideoReader();

        status = reader.Init(vid_path);
        if (status != ErrorCode.kSuccess)
        {
            throw new Exception("Failed to open video!");
        }

        // Iterate through frames.
        bool vid_end = false;

        while (true)
        {
            // Find detections.
            VectorVectorDetection dets = new VectorVectorDetection();
            VectorImage           imgs = new VectorImage();
            for (int i = 0; i < kMaxImg; ++i)
            {
                Image img = new Image();
                status = reader.GetFrame(img);
                if (status != ErrorCode.kSuccess)
                {
                    vid_end = true;
                    break;
                }
                img = openem.Rectify(img, endpoints);
                img = openem.Crop(img, roi);
                imgs.Add(img);
                status = detector.AddImage(img);
                if (status != ErrorCode.kSuccess)
                {
                    throw new Exception("Failed to add frame to detector!");
                }
            }
            status = detector.Process(dets);
            if (status != ErrorCode.kSuccess)
            {
                throw new Exception("Failed to process detector!");
            }
            for (int i = 0; i < dets.Count; ++i)
            {
            }
            detections.AddRange(dets);
            for (int i = 0; i < detections.Count; ++i)
            {
            }

            // Classify detections.
            for (int i = 0; i < dets.Count; ++i)
            {
                VectorClassification score_batch = new VectorClassification();
                for (int j = 0; j < dets[i].Count; ++j)
                {
                    Image det_img = openem.GetDetImage(imgs[i], dets[i][j].location);
                    status = classifier.AddImage(det_img);
                    if (status != ErrorCode.kSuccess)
                    {
                        throw new Exception("Failed to add frame to classifier!");
                    }
                }
                status = classifier.Process(score_batch);
                if (status != ErrorCode.kSuccess)
                {
                    throw new Exception("Failed to process classifier!");
                }
                scores.Add(score_batch);
            }
            if (vid_end)
            {
                break;
            }
        }
    }
Example #2
0
    /// <summary>
    /// Main program.
    /// </summary>
    static int Main(string[] args)
    {
        // Check input arguments.
        if (args.Length < 2)
        {
            Console.WriteLine("Expected at least two arguments:");
            Console.WriteLine("  Path to protobuf file containing model");
            Console.WriteLine("  Paths to one or more image files");
            return(-1);
        }

        // Create and initialize detector.
        Detector  detector = new Detector();
        ErrorCode status   = detector.Init(args[0]);

        if (status != ErrorCode.kSuccess)
        {
            Console.WriteLine("Failed to initialize detector!");
            return(-1);
        }

        // Load in images.
        VectorImage imgs = new VectorImage();

        for (int i = 1; i < args.Length; i++)
        {
            Image img = new Image();
            status = img.FromFile(args[i]);
            if (status != ErrorCode.kSuccess)
            {
                Console.WriteLine("Failed to load image {0}!", args[i]);
                return(-1);
            }
            imgs.Add(img);
        }

        // Add images to processing queue.
        foreach (var img in imgs)
        {
            status = detector.AddImage(img);
            if (status != ErrorCode.kSuccess)
            {
                Console.WriteLine("Failed to add image for processing!");
                return(-1);
            }
        }

        // Process the loaded images.
        VectorVectorDetection detections = new VectorVectorDetection();

        status = detector.Process(detections);
        if (status != ErrorCode.kSuccess)
        {
            Console.WriteLine("Failed to process images!");
            return(-1);
        }

        // Display the detections on the image.
        for (int i = 0; i < detections.Count; ++i)
        {
            foreach (var det in detections[i])
            {
                imgs[i].DrawRect(det.location);
            }
            imgs[i].Show();
        }

        return(0);
    }