//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();
        }
Exemple #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);
            }
        }
        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);
            }
        }
        public OverlayVideo(Label labelEndTime, Label labelVideoInfo1, Label labelVideoInfo2, Label labelVideoInfo3, Label labelProgress, DataGridView dataGridView1, DataGridView dataGridView2, Button startBtn, TrackBar TrackingBar, PictureBox pictureBoxVideo, string path, string createTime, int maxFrameNum, int analysisFPS = 5, int minTrackingLength = 29, int clusterNum = 20, int outputFrameNum = 1000)
        {
            //drawing style
            drawFont         = new System.Drawing.Font("Arial", 14);
            drawBrush        = new System.Drawing.SolidBrush(System.Drawing.Color.White);
            backgroundPeople = new Bitmap(@"C:\Users\SoMa\Desktop\RapidCheck\main\RapidCheck\asset\back\[email protected]");
            backgroundCar    = new Bitmap(@"C:\Users\SoMa\Desktop\RapidCheck\main\RapidCheck\asset\back\[email protected]");
            Direction0       = new Bitmap(@"C:\Users\SoMa\Desktop\RapidCheck\main\RapidCheck\asset\dir0.png");
            Direction1       = new Bitmap(@"C:\Users\SoMa\Desktop\RapidCheck\main\RapidCheck\asset\dir1.png");
            Direction2       = new Bitmap(@"C:\Users\SoMa\Desktop\RapidCheck\main\RapidCheck\asset\dir2.png");
            Direction3       = new Bitmap(@"C:\Users\SoMa\Desktop\RapidCheck\main\RapidCheck\asset\dir3.png");
            Direction4       = new Bitmap(@"C:\Users\SoMa\Desktop\RapidCheck\main\RapidCheck\asset\dir4.png");
            Direction5       = new Bitmap(@"C:\Users\SoMa\Desktop\RapidCheck\main\RapidCheck\asset\dir5.png");
            Direction6       = new Bitmap(@"C:\Users\SoMa\Desktop\RapidCheck\main\RapidCheck\asset\dir6.png");
            Direction7       = new Bitmap(@"C:\Users\SoMa\Desktop\RapidCheck\main\RapidCheck\asset\dir7.png");
            //UI
            this.labelVideoInfo1    = labelVideoInfo1;
            this.labelVideoInfo2    = labelVideoInfo2;
            this.labelVideoInfo3    = labelVideoInfo3;
            this.labelEndTime       = labelEndTime;
            this.labelProgress      = labelProgress;
            this.dataGridView1      = dataGridView1;
            this.dataGridView2      = dataGridView2;
            this.trackingBar        = TrackingBar;
            this.pictureBoxVideo    = pictureBoxVideo;
            this.startBtn           = startBtn;
            this.resFrame           = 0;
            this.overlayObjIdx      = 0;
            this.clickFramePosition = 0;
            //------------------------------변수 초기화-----------------------------
            ObjList            = new List <Obj>(); //DB Table
            objectidList       = new List <int>();
            originObjectidList = new List <int>();
            objidByFrame       = new Dictionary <int, List <int> >();
            overlayFrames      = new List <Bitmap>();
            overlayOrders      = new List <List <objIdAndOrderingCnt> >();

            trackingTableObjid       = new List <int>();
            trackingTableClassid     = new Dictionary <int, int>();
            trackingTableDirectionid = new Dictionary <int, int>();
            trackingTableFrameNum    = new List <int>();
            trackingTableRectangle   = new List <Rectangle>();
            gridViewList1            = new List <int>();
            gridViewList2            = new List <int>();
            videoPath               = path;
            videoPath               = videoPath.Replace(@"\", @"\\");
            this.conditionTarget    = "";
            this.conditionDensity   = "";
            this.conditionDirection = "";
            this.conditionColor     = "";
            this.outputFrameNum     = outputFrameNum;
            this.maxFrameNum        = maxFrameNum;
            this.minTrackingLength  = minTrackingLength;
            this.clusterNum         = clusterNum;
            this.speed              = 1;
            this.drawTime           = true;
            this.createTime         = new System.DateTime(2017, 1, 1, Int32.Parse(createTime.Split(':')[0]), Int32.Parse(createTime.Split(':')[1]), 0);
            this.objType            = 1; //default는 둘 다 검색
            this.maxObjectid        = 0;
            startingGroup           = new List <StartingGroup>();
            for (int i = 0; i < clusterNum; i++)
            {
                startingGroup.Add(new StartingGroup());
            }

            //read video info
            //Accord.Video.FFMPEG.VideoFileReader reader = new Accord.Video.FFMPEG.VideoFileReader();
            reader = new Accord.Video.FFMPEG.VideoFileReader();
            reader.Open(videoPath);
            frameRate   = reader.FrameRate;
            videoWidth  = reader.Width;
            videoHeight = reader.Height;
            background  = reader.ReadVideoFrame(); // 첫번째 프레임을 백그라운드로
            fps         = reader.FrameRate;
            //reader.FrameCount
            this.analysisFPS = analysisFPS;
            this.frameStep   = fps / analysisFPS;
            if (maxFrameNum == 0 || maxFrameNum > (int)reader.FrameCount)
            {
                this.maxFrameNum = (int)reader.FrameCount;
            }
            strVideoInfo1        = "path: " + path + "\nSize: " + reader.Width + " x " + reader.Height + "\ncodec: " + reader.CodecName;
            labelVideoInfo1.Text = strVideoInfo1;
            strVideoInfo2        = "FPS: " + fps + "\nCluster value: " + clusterNum + "\nFrame step: " + frameStep;
            labelVideoInfo2.Text = strVideoInfo2;
            strVideoInfo3        = "create Time: " + createTime + "\nVideo Frame: " + reader.FrameCount;
            videoInfo3Flag       = true;
            //reader.Close();

            //tabpage3
            directionCntPeople = new List <int>();
            directionCntCar    = new List <int>();
            colorRatioPeople   = new List <double>();
            colorRatioCar      = new List <double>();
            peopleTotal        = 0;
            carTotal           = 0;
            //------------------------------/변수 초기화-----------------------------
        }
Exemple #5
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();
        }