Esempio n. 1
0
        //private void button1_Click(object sender, EventArgs e) //Frame btn
        //{
        //    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        //    sw.Start();
        //    FFMPEG_TEST();
        //    sw.Stop();
        //    MessageBox.Show(sw.ElapsedMilliseconds.ToString() + "ms");
        //}
        public void FFMPEG_TEST()
        {
            // create instance of video reader
            Accord.Video.FFMPEG.VideoFileReader reader = new Accord.Video.FFMPEG.VideoFileReader();
            // open video file
            //reader.Open(@"C:\Users\trevor\Desktop\Videos\cctv2.wmv"); //local path
            reader.Open(@"C:\videos\tracking.mp4"); //public pc path
            Bitmap[] objBitmap = new Bitmap[nrow];

            // read video frames out of it
            int j = 0;

            for (int i = 0; i < 10000 /*reader.FrameCount*/; i++) //i = frameNum
            {
                //make...folder... 일단 폴더에 저장해보자 //SQL에서 DISTINCT해서 불러와도 무관..속돈 나중에

                Bitmap videoFrame = reader.ReadVideoFrame();
                for (; i == trackingData[j].frameNum; j++) // j == row number
                {
                    makeFolder(@"C:\videos\obj\" + trackingData[j].objId);
                    //corp
                    //objBitmap[j] = cropImage(videoFrame, new Rectangle(trackingData[j].x, trackingData[j].y, trackingData[j].w, trackingData[j].h));
                    //objBitmap[j].Save(@"C:\videos\obj\" + trackingData[j].objId + "\\" + j + ".bmp");

                    cropImage(videoFrame, new Rectangle(trackingData[j].x, trackingData[j].y, trackingData[j].w, trackingData[j].h)).Save(@"C:\videos\obj\" + trackingData[j].objId + "\\" + j + ".bmp");
                }
                // dispose the frame when it is no longer required
                videoFrame.Dispose();
            }
            reader.Close();
        }
Esempio n. 2
0
        private static void reencode(FileInfo fileInput, FileInfo fileOutput, VideoCodec outputCodec,
                                     AVPixelFormat format = AVPixelFormat.FormatYuv420P, double expectedFrameRate = 2997 / 100.0)
        {
            using (var videoFileReader = new Accord.Video.FFMPEG.VideoFileReader())
            {
                videoFileReader.Open(fileInput.FullName);

                using (var videoFileWriter = new Accord.Video.FFMPEG.VideoFileWriter())
                {
                    Assert.AreEqual(2997, videoFileReader.FrameRate.Numerator);
                    Assert.AreEqual(100, videoFileReader.FrameRate.Denominator);

                    videoFileWriter.Width      = videoFileReader.Width;
                    videoFileWriter.Height     = videoFileReader.Height;
                    videoFileWriter.FrameRate  = videoFileReader.FrameRate;
                    videoFileWriter.VideoCodec = outputCodec;
                    Assert.AreEqual(AVPixelFormat.FormatYuv420P, videoFileWriter.PixelFormat);
                    videoFileWriter.PixelFormat = format;

                    videoFileWriter.Open(fileOutput.FullName);

                    do
                    {
                        using (var bitmap = videoFileReader.ReadVideoFrame())
                        {
                            if (bitmap == null)
                            {
                                break;
                            }

                            videoFileWriter.WriteVideoFrame(bitmap);
                        }
                    }while (true);

                    videoFileWriter.Close();
                }

                videoFileReader.Close();
            }

            using (var videoFileReader = new Accord.Video.FFMPEG.VideoFileReader())
            {
                videoFileReader.Open(fileOutput.FullName);

                Assert.AreEqual(expectedFrameRate, videoFileReader.FrameRate.Value, 0.01);
            }
        }
Esempio n. 3
0
        private static void reencode(FileInfo fileInput, FileInfo fileOutput, VideoCodec outputCodec)
        {
            using (var videoFileReader = new Accord.Video.FFMPEG.VideoFileReader())
            {
                videoFileReader.Open(fileInput.FullName);

                using (var videoFileWriter = new Accord.Video.FFMPEG.VideoFileWriter())
                {
                    Assert.AreEqual(2997, videoFileReader.FrameRate.Numerator);
                    Assert.AreEqual(100, videoFileReader.FrameRate.Denominator);

                    videoFileWriter.Open
                    (
                        fileOutput.FullName,
                        videoFileReader.Width,
                        videoFileReader.Height,
                        videoFileReader.FrameRate,
                        outputCodec
                    );

                    do
                    {
                        using (var bitmap = videoFileReader.ReadVideoFrame())
                        {
                            if (bitmap == null)
                            {
                                break;
                            }
                            videoFileWriter.WriteVideoFrame(bitmap);
                        }
                    }while (true);

                    videoFileWriter.Close();
                }

                videoFileReader.Close();
            }

            using (var videoFileReader = new Accord.Video.FFMPEG.VideoFileReader())
            {
                videoFileReader.Open(fileOutput.FullName);

                Assert.AreEqual(2997 / 100.0, videoFileReader.FrameRate.Value, 0.01);
            }
        }
Esempio n. 4
0
        public void imageCrop()
        {
            Accord.Video.FFMPEG.VideoFileReader reader2 = new Accord.Video.FFMPEG.VideoFileReader();
            //reader = new Accord.Video.FFMPEG.VideoFileReader();
            reader2.Open(videoPath);

            int cropMaxFrameNum = 0;

            for (int frameNum = 0; frameNum < maxFrameNum /*reader.FrameCount*/; frameNum += frameStep)
            {
                if (objidByFrame.ContainsKey(frameNum))
                {
                    cropMaxFrameNum = frameNum;
                }
            }
            for (int frameNum = 0; frameNum <= cropMaxFrameNum /*reader.FrameCount*/; frameNum++)
            {
                Bitmap videoFrame = reader2.ReadVideoFrame();
                if (frameNum % frameStep != 0)
                {
                    videoFrame.Dispose();
                    continue;
                }
                dataGridView1.Invoke(new Action(() =>
                {
                    if (objidByFrame.ContainsKey(frameNum))
                    {
                        foreach (int objid in objidByFrame[frameNum])
                        {
                            if (idxbyObjid[objid] == -1)
                            {
                                continue;
                            }
                            Rectangle temp = ObjList[idxbyObjid[objid]].getNextCropArea();
                            Bitmap bit     = videoFrame.Clone(temp, videoFrame.PixelFormat);
                            ObjList[idxbyObjid[objid]].addCropImage(bit);

                            if (ObjList[idxbyObjid[objid]].cropImages.Count == 1)
                            {
                                //int gridHeight = 190;
                                int headlineHeight = 30;
                                Bitmap gridImg     = null;
                                if (trackingTableClassid[objid] == 0)
                                {
                                    gridImg = (Bitmap)backgroundCar.Clone();
                                }
                                else
                                {
                                    gridImg = (Bitmap)backgroundPeople.Clone();
                                }
                                Bitmap headlineBox = new Bitmap(gridImg.Width, headlineHeight); // obj info

                                //SET HEADLINEBOX COLOR WHITE
                                for (int i = 0; i < headlineBox.Height; i++)
                                {
                                    for (int j = 0; j < headlineBox.Width; j++)
                                    {
                                        headlineBox.SetPixel(j, i, Color.White);
                                    }
                                }

                                RectangleF rectf = new RectangleF(35, 0, headlineBox.Width, headlineBox.Height); // 다음 위치에 택스트를 그린다 (시간)

                                //alpha
                                ColorMatrix matrix  = new ColorMatrix();
                                matrix.Matrix33     = 0.7f; //0.7~0.75
                                ImageAttributes att = new ImageAttributes();
                                att.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                                Graphics g = Graphics.FromImage(gridImg);
                                //System.Drawing.SolidBrush bgcolor = new System.Drawing.SolidBrush(System.Drawing.Color.White);
                                //g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;

                                double ratio = 1.0;
                                if (bit.Width * gridImg.Height < gridImg.Width * bit.Height)
                                {
                                    ratio = (double)gridImg.Height / bit.Height;
                                }
                                else
                                {
                                    ratio = (double)gridImg.Width / bit.Width;
                                }

                                int width      = (int)(bit.Width * ratio);
                                int height     = (int)(bit.Height * ratio);
                                Rectangle rect = new Rectangle((gridImg.Width - width) / 2, (gridImg.Height - height) / 2, width, height);
                                g.DrawImage(bit, rect);

                                g.DrawImage(headlineBox, new Rectangle(0, 0, headlineBox.Width, headlineBox.Height), 0, 0, headlineBox.Width, headlineHeight, GraphicsUnit.Pixel, att);

                                switch (trackingTableDirectionid[objid])
                                {
                                case 0:
                                    g.DrawImage(Direction0, new Rectangle(0, 0, 30, 30));
                                    break;

                                case 1:
                                    g.DrawImage(Direction1, new Rectangle(0, 0, 30, 30));
                                    break;

                                case 2:
                                    g.DrawImage(Direction2, new Rectangle(0, 0, 30, 30));
                                    break;

                                case 3:
                                    g.DrawImage(Direction3, new Rectangle(0, 0, 30, 30));
                                    break;

                                case 4:
                                    g.DrawImage(Direction4, new Rectangle(0, 0, 30, 30));
                                    break;

                                case 5:
                                    g.DrawImage(Direction5, new Rectangle(0, 0, 30, 30));
                                    break;

                                case 6:
                                    g.DrawImage(Direction6, new Rectangle(0, 0, 30, 30));
                                    break;

                                case 7:
                                    g.DrawImage(Direction7, new Rectangle(0, 0, 30, 30));
                                    break;
                                }
                                g.DrawString(ObjList[idxbyObjid[objid]].startTime.ToString("HH:mm"), new Font("SpoqaHanSans", 14, FontStyle.Bold), Brushes.Black, rectf);

                                if (trackingTableClassid[objid] == 0) // class id = 0 => people
                                {
                                    dataGridView1.Rows.Add(gridImg);
                                    dataGridView1.Rows[dataGridView1.RowCount - 1].Height = gridImg.Height;
                                    gridViewList1.Add(objid);
                                }
                                else if (trackingTableClassid[objid] == 1)
                                {
                                    dataGridView2.Rows.Add(gridImg);
                                    dataGridView2.Rows[dataGridView2.RowCount - 1].Height = gridImg.Height;
                                    gridViewList2.Add(objid);
                                }
                                else
                                {
                                    MessageBox.Show("DataGridView ERROR");
                                }
                            }
                        }
                    }
                }));
                videoFrame.Dispose();
                int percent = frameNum * 100 / cropMaxFrameNum;
                labelProgress.Text = "Detection 100%\nTracking 100%\nOverlay " + percent + "%";
            }
            reader2.Close();
        }