public void VideoWriterTest_CreateTestMP4File_640x480_24fps_15s()
        {

            VideoWriter videoWriter = new VideoWriter();

            string TestOutputDirectoryQualified = Directory.GetCurrentDirectory() + "\\" + TestOutputFilesPath;
            if (!Directory.Exists(TestOutputDirectoryQualified))
            {
                Directory.CreateDirectory(TestOutputDirectoryQualified);
            }

            videoWriter.Init(TestOutputDirectoryQualified + "\\" + MP4_FILENAME, VIDEO_WIDTH, VIDEO_HEIGHT, (int)VIDEO_FPS, 1, (int)VIDEO_ENCODE_BITRATE);

            var filepaths = Directory.EnumerateFiles(Directory.GetCurrentDirectory() + "\\" + DataFilesPath, "*.jpg", SearchOption.TopDirectoryOnly);
            DateTime dateTimeNow = DateTime.Now;
            long startTimeTicks = dateTimeNow.Ticks;

            do
            {
                foreach (string filepath in filepaths)
                {
                    byte[] imagebitArray = GetImageByteArray(filepath);

                    MemoryStream stream = new MemoryStream(imagebitArray);

                    Bitmap image = (Bitmap)Image.FromStream(stream);

                    //lets check if the image is what we expect
                    if (image.PixelFormat != PixelFormat.Format24bppRgb)
                    {
                        string message = String.Format("Image format from is not correct. PixelFormat: {1}", image.PixelFormat);
                        throw new Exception(message);
                    }

                    // Lock the bitmap's bits.  
                    Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
                    BitmapData bmpData = image.LockBits(rect, ImageLockMode.ReadOnly, image.PixelFormat);

                    // Get the address of the first line.
                    IntPtr ptr = bmpData.Scan0;

                    unsafe
                    {
                        videoWriter.AddFrame((byte *)ptr, 3*VIDEO_WIDTH*VIDEO_HEIGHT, VIDEO_WIDTH, VIDEO_HEIGHT, DateTime.Now.Ticks - startTimeTicks);
                    }
                    Thread.Sleep(new TimeSpan(0, 0, 0, 0, (int)(1000.0 / VIDEO_FPS)));

                    image.UnlockBits(bmpData);
                }

            } while (TimeSpan.Compare(new TimeSpan(DateTime.Now.Ticks - startTimeTicks), new TimeSpan(0, 0, (int)((double)VIDEO_DURATION_IN_100_NS/10000000.0))) < 0);
            
            videoWriter.Done();

            ValidateMP4OutputFile(TestOutputDirectoryQualified + "\\" + MP4_FILENAME);

        }
Example #2
0
        public void VideoWriterTest_CreateTestMP4File_640x480_24fps_15s()
        {
            VideoWriter videoWriter = new VideoWriter();

            string TestOutputDirectoryQualified = Directory.GetCurrentDirectory() + "\\" + TestOutputFilesPath;

            if (!Directory.Exists(TestOutputDirectoryQualified))
            {
                Directory.CreateDirectory(TestOutputDirectoryQualified);
            }

            videoWriter.Init(TestOutputDirectoryQualified + "\\" + MP4_FILENAME, VIDEO_WIDTH, VIDEO_HEIGHT, (int)VIDEO_FPS, 1, (int)VIDEO_ENCODE_BITRATE);

            var      filepaths      = Directory.EnumerateFiles(Directory.GetCurrentDirectory() + "\\" + DataFilesPath, "*.jpg", SearchOption.TopDirectoryOnly);
            DateTime dateTimeNow    = DateTime.Now;
            long     startTimeTicks = dateTimeNow.Ticks;

            do
            {
                foreach (string filepath in filepaths)
                {
                    byte[] imagebitArray = GetImageByteArray(filepath);

                    MemoryStream stream = new MemoryStream(imagebitArray);

                    Bitmap image = (Bitmap)Image.FromStream(stream);

                    //lets check if the image is what we expect
                    if (image.PixelFormat != PixelFormat.Format24bppRgb)
                    {
                        string message = String.Format("Image format from is not correct. PixelFormat: {1}", image.PixelFormat);
                        throw new Exception(message);
                    }

                    // Lock the bitmap's bits.
                    Rectangle  rect    = new Rectangle(0, 0, image.Width, image.Height);
                    BitmapData bmpData = image.LockBits(rect, ImageLockMode.ReadOnly, image.PixelFormat);

                    // Get the address of the first line.
                    IntPtr ptr = bmpData.Scan0;

                    unsafe
                    {
                        videoWriter.AddFrame((byte *)ptr, 3 * VIDEO_WIDTH * VIDEO_HEIGHT, VIDEO_WIDTH, VIDEO_HEIGHT, DateTime.Now.Ticks - startTimeTicks);
                    }
                    Thread.Sleep(new TimeSpan(0, 0, 0, 0, (int)(1000.0 / VIDEO_FPS)));

                    image.UnlockBits(bmpData);
                }
            } while (TimeSpan.Compare(new TimeSpan(DateTime.Now.Ticks - startTimeTicks), new TimeSpan(0, 0, (int)((double)VIDEO_DURATION_IN_100_NS / 10000000.0))) < 0);

            videoWriter.Done();

            ValidateMP4OutputFile(TestOutputDirectoryQualified + "\\" + MP4_FILENAME);
        }
Example #3
0
    /// <summary>
    /// Writes a new video with bounding boxes around detections.
    /// </summary>
    /// <param name="vid_path">Path to the original video.</param>
    /// <param name="out_path">Path to the output video.</param>
    /// <param name="roi">Region of interest output from FindVideoRoi.</param>
    /// <param name="endpoints">Transform 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 WriteVideo(
        string vid_path,
        string out_path,
        Rect roi,
        PointPair endpoints,
        VectorVectorDetection detections,
        VectorVectorClassification scores)
    {
        // Initialize the video reader.
        VideoReader reader = new VideoReader();
        ErrorCode   status = reader.Init(vid_path);

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

        // Initialize the video writer.
        VideoWriter writer = new VideoWriter();

        status = writer.Init(
            out_path,
            reader.FrameRate(),
            Codec.kWmv2,
            new PairIntInt(reader.Width(), reader.Height()));
        if (status != ErrorCode.kSuccess)
        {
            throw new Exception("Failed to write video!");
        }

        // Iterate through frames.
        for (int i = 0; i < detections.Count; ++i)
        {
            Image frame = new Image();
            status = reader.GetFrame(frame);
            if (status != ErrorCode.kSuccess)
            {
                throw new Exception("Error retrieving video frame!");
            }
            Color blue  = new Color(); blue[0] = 255; blue[1] = 0; blue[2] = 0;
            Color red   = new Color(); red[0] = 0; red[1] = 0; red[2] = 255;
            Color green = new Color(); green[0] = 0; green[1] = 255; green[2] = 0;
            frame.DrawRect(roi, blue, 1, endpoints);
            for (int j = 0; j < detections[i].Count; ++j)
            {
                Color  det_color = red;
                double clear     = scores[i][j].cover[2];
                double hand      = scores[i][j].cover[1];
                if (j == 0)
                {
                    if (clear > hand)
                    {
                        det_color = green;
                    }
                    else
                    {
                        det_color = red;
                    }
                }
                frame.DrawRect(detections[i][j].location, det_color, 2, endpoints, roi);
            }
            status = writer.AddFrame(frame);
            if (status != ErrorCode.kSuccess)
            {
                throw new Exception("Error adding frame to video!");
            }
        }
    }