Ejemplo n.º 1
0
        public void GetPosition()
        {
            const string testName = "GetPosition";
            var          path     = this.GetDataFile($"{LoadTarget}.bmp");

            var tests = new[]
            {
                new { Type = ImageTypes.RgbPixel, ExpectResult = true },
                new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false },
                new { Type = ImageTypes.UInt8, ExpectResult = true },
                new { Type = ImageTypes.UInt16, ExpectResult = true },
                new { Type = ImageTypes.HsiPixel, ExpectResult = true },
                new { Type = ImageTypes.Float, ExpectResult = true },
                new { Type = ImageTypes.Double, ExpectResult = true }
            };

            foreach (var input in tests)
            {
                var expectResult = input.ExpectResult;
                var inputType    = input.Type;
                var imageObj     = DlibTest.LoadImage(input.Type, path);
                var tracker      = new CorrelationTracker();
                var rect         = new DRectangle(46, 15, 216, 205);

                var outputImageAction = new Func <bool, Array2DBase>(expect =>
                {
                    tracker.StartTrack(imageObj, rect);
                    return(imageObj);
                });

                var successAction = new Action <Array2DBase>(image =>
                {
                    tracker.Update(image);
                    using (var pos = tracker.GetPosition())
                        Assert.IsFalse(pos.IsEmpty);
                });

                var failAction = new Action(() =>
                {
                    Assert.Fail($"{testName} should throw excption for InputType: {inputType}.");
                });

                var finallyAction = new Action(() =>
                {
                    this.DisposeAndCheckDisposedState(rect);
                    this.DisposeAndCheckDisposedState(tracker);
                    if (imageObj != null)
                    {
                        this.DisposeAndCheckDisposedState(imageObj);
                    }
                });

                var exceptionAction = new Action(() =>
                {
                    Console.WriteLine($"Failed to execute {testName} to InputType: {inputType}.");
                });

                DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction);
            }
        }
Ejemplo n.º 2
0
        private static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Call this program like this: ");
                Console.WriteLine("VideoTracking.exe <path of video_frames directory>");
                return;
            }

            var path  = args[0];
            var files = new DirectoryInfo(path).GetFiles("*.jpg").Select(info => info.FullName).ToList();

            files.Sort();

            if (files.Count == 0)
            {
                Console.WriteLine($"No images found in {path}");
                return;
            }

            using (var win = new ImageWindow())
                using (var tracker = new CorrelationTracker())
                {
                    var firstFile = files.First();
                    using (var img = Dlib.LoadImage <byte>(firstFile))
                        using (var rect = DRectangle.CenteredRect(93, 110, 38, 86))
                            tracker.StartTrack(img, rect);

                    foreach (var file in files.GetRange(1, files.Count - 1))
                    {
                        using (var img = Dlib.LoadImage <byte>(file))
                        {
                            tracker.Update(img);

                            win.SetImage(img);
                            win.ClearOverlay();

                            using (var pos = tracker.GetPosition())
                                win.AddOverlay(pos);

                            Console.WriteLine("hit enter to process next frame");
                            Console.ReadKey();
                        }
                    }
                }
        }
Ejemplo n.º 3
0
        private static void PropagateBoxes(Dataset data, uint prev, uint next)
        {
            if (prev == next || next >= data.Images.Count)
            {
                return;
            }

            using (var img1 = Dlib.LoadImage <RgbPixel>(data.Images[(int)prev].FileName))
                using (var img2 = Dlib.LoadImage <RgbPixel>(data.Images[(int)next].FileName))
                    for (int i = 0, count = data.Images[(int)prev].Boxes.Count; i < count; ++i)
                    {
                        using (var tracker = new CorrelationTracker())
                        {
                            tracker.StartTrack(img1, data.Images[(int)prev].Boxes[i].Rect);
                            tracker.Update(img2);
                            var box = data.Images[(int)prev].Boxes[i];
                            box.Rect = (Rectangle)tracker.GetPosition();
                            data.Images[(int)next].Boxes.Add(box);
                        }
                    }
        }
Ejemplo n.º 4
0
        public void TryTrack()
        {
            const string testName = "TryTrack";

            var tests = new[]
            {
                new { Type = ImageTypes.Int32, ExpectResult = true }
            };

            var type = this.GetType().Name;

            foreach (var input in tests)
            {
                var tracker = new CorrelationTracker();
                try
                {
                    var index = 0;
                    foreach (var file in this.GetDataFiles("video_frames").Where(info => info.Name.EndsWith("jpg")))
                    {
                        var expectResult = input.ExpectResult;
                        var inputType    = input.Type;
                        var imageObj     = DlibTest.LoadImage(input.Type, file);

                        if (index == 0)
                        {
                            using (var rect = DRectangle.CenteredRect(93, 110, 38, 86))
                                tracker.StartTrack(imageObj, rect);
                        }

                        var outputImageAction = new Func <bool, Array2DBase>(expect =>
                        {
                            if (index != 0)
                            {
                                tracker.Update(imageObj);
                            }

                            return(imageObj);
                        });

                        var successAction = new Action <Array2DBase>(image =>
                        {
                            if (index != 0)
                            {
                                tracker.Update(image);
                            }
                            using (var r = tracker.GetPosition())
                            {
                                using (var img = Dlib.LoadImage <RgbPixel>(file.FullName))
                                {
                                    Dlib.DrawRectangle(img, (Rectangle)r, new RgbPixel {
                                        Red = 255
                                    }, 3);
                                    Dlib.SaveJpeg(img, Path.Combine(this.GetOutDir(type), $"{Path.GetFileNameWithoutExtension(file.FullName)}.jpg"));
                                }
                            }
                        });

                        var failAction = new Action(() =>
                        {
                            Assert.Fail($"{testName} should throw excption for InputType: {inputType}.");
                        });

                        var finallyAction = new Action(() =>
                        {
                            index++;

                            if (imageObj != null)
                            {
                                this.DisposeAndCheckDisposedState(imageObj);
                            }
                        });

                        var exceptionAction = new Action(() =>
                        {
                            Console.WriteLine($"Failed to execute {testName} to InputType: {inputType}.");
                        });

                        DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction);
                    }
                }
                finally
                {
                    this.DisposeAndCheckDisposedState(tracker);
                }
            }
        }
Ejemplo n.º 5
0
        public void Create()
        {
            var tracker = new CorrelationTracker();

            this.DisposeAndCheckDisposedState(tracker);
        }
Ejemplo n.º 6
0
        private static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Call this program like this: ");
                Console.WriteLine("VideoTracking.exe <path of video_frames directory>");
                return;
            }

            var path  = args[0];
            var files = new DirectoryInfo(path).GetFiles("*.jpg").Select(info => info.FullName).ToList();



            files.Sort();

            if (files.Count == 0)
            {
                Console.WriteLine($"No images found in {path}");
                return;
            }


            // 定义图像捕捉方式 从摄像头 , 注意 Windows下需要选择 VideoCaptureAPIs.DSHOW
            var cap = new VideoCapture(0, VideoCaptureAPIs.DSHOW);

            // 定义图像捕捉方式 从摄像头 视频文件
            //var cap = new VideoCapture("video.webm");

            //判断捕捉设备是否打开
            if (!cap.IsOpened())
            {
                Console.WriteLine("Unable to connect to camera");
                return;
            }

            Mat temp    = null;
            var tracker = new CorrelationTracker();

            int init = 0;

            //定义显示窗口
            using (var win = new ImageWindow())
            {
                Console.WriteLine("对象追踪程序启动");
                Console.WriteLine("选择命令行为当前窗口,通过按键选择需要追踪的区域Width: [A,Z] Height:[S,X] X:[right,left] Y:[up,down] ,点击Enter开始追踪");
                Console.WriteLine("注意:切换命令行窗口输入法为英文输入状态");
                //选择追踪对象
                while (!win.IsClosed())
                {
                    //获得1帧图片
                    temp = cap.RetrieveMat();// new Mat();


                    if (temp == null)
                    {
                        Console.WriteLine("图像获取错误!");
                        return;
                    }

                    var array = new byte[temp.Width * temp.Height * temp.ElemSize()];
                    Marshal.Copy(temp.Data, array, 0, array.Length);
                    using (var cimg = Dlib.LoadImageData <BgrPixel>(array, (uint)temp.Height, (uint)temp.Width, (uint)(temp.Width * temp.ElemSize())))
                    {
                        init++;
                        if (init > 1)
                        {
                            var KK = Console.ReadKey();
                            if (KK.Key == ConsoleKey.Enter)
                            {
                                Console.WriteLine("开始追踪目标!");

                                //确定 追踪 位置
                                var rect2 = DRectangle.CenteredRect(a_X, a_Y, a_W, a_H);
                                //开始追踪
                                tracker.StartTrack(cimg, rect2);
                                win.SetImage(cimg);
                                win.ClearOverlay();
                                win.AddOverlay(rect2);
                                break;
                            }

                            //选择 追踪区域
                            if (KK.Key == ConsoleKey.RightArrow || KK.Key == ConsoleKey.LeftArrow || KK.Key == ConsoleKey.UpArrow || KK.Key == ConsoleKey.DownArrow || KK.Key == ConsoleKey.A || KK.Key == ConsoleKey.Z || KK.Key == ConsoleKey.S || KK.Key == ConsoleKey.X)
                            {
                                if (KK.Key == ConsoleKey.RightArrow)
                                {
                                    a_X++;
                                    if (a_X > cimg.Rect.Width - a_W)
                                    {
                                        a_X = cimg.Rect.Width - a_W;
                                    }
                                }
                                if (KK.Key == ConsoleKey.LeftArrow)
                                {
                                    a_X--;
                                    if (a_X < 0)
                                    {
                                        a_X = 0;
                                    }
                                }

                                if (KK.Key == ConsoleKey.UpArrow)
                                {
                                    a_Y--;
                                    if (a_Y < 0)
                                    {
                                        a_Y = 0;
                                    }
                                }
                                if (KK.Key == ConsoleKey.DownArrow)
                                {
                                    a_Y++;
                                    if (a_Y > cimg.Rect.Height - a_H)
                                    {
                                        a_Y = cimg.Rect.Height - a_H;
                                    }
                                }

                                if (KK.Key == ConsoleKey.A)
                                {
                                    a_W++;
                                    if (a_W >= cimg.Rect.Width - a_X)
                                    {
                                        a_W = cimg.Rect.Width - a_X;
                                    }
                                }
                                if (KK.Key == ConsoleKey.Z)
                                {
                                    a_W--;
                                    if (a_W < 10)
                                    {
                                        a_W = 10;
                                    }
                                }
                                if (KK.Key == ConsoleKey.S)
                                {
                                    a_H++;
                                    if (a_H > cimg.Rect.Height - a_Y)
                                    {
                                        a_H = cimg.Rect.Height - a_Y;
                                    }
                                }
                                if (KK.Key == ConsoleKey.X)
                                {
                                    a_H--;
                                    if (a_H < 10)
                                    {
                                        a_H = 10;
                                    }
                                }
                            }
                        }

                        var rect = DRectangle.CenteredRect(a_X, a_Y, a_W, a_H);

                        Console.WriteLine("Set RECT:" + a_X + " " + a_Y + " " + a_W + " " + a_H);

                        //显示图片
                        win.SetImage(cimg);
                        win.ClearOverlay();
                        //显示框
                        win.AddOverlay(rect);
                    }
                }

                //选择追踪对象
                while (!win.IsClosed())
                {
                    //获得1帧图片
                    temp = cap.RetrieveMat();// new Mat();


                    if (temp == null)
                    {
                        Console.WriteLine("图像获取错误!");
                        return;
                    }

                    var array = new byte[temp.Width * temp.Height * temp.ElemSize()];
                    Marshal.Copy(temp.Data, array, 0, array.Length);
                    using (var cimg = Dlib.LoadImageData <BgrPixel>(array, (uint)temp.Height, (uint)temp.Width, (uint)(temp.Width * temp.ElemSize())))
                    {
                        //更新追踪图像
                        tracker.Update(cimg);

                        win.SetImage(cimg);
                        win.ClearOverlay();

                        //获得追踪到的目标位置
                        DRectangle rect = tracker.GetPosition();
                        win.AddOverlay(rect);


                        Console.WriteLine("OBJ RECT:" + (int)rect.Left + " " + (int)rect.Top + " " + (int)rect.Width + " " + (int)rect.Height);

                        System.Threading.Thread.Sleep(100);
                    }
                }
            }



            Console.WriteLine("任意键退出");
            Console.ReadKey();
        }
Ejemplo n.º 7
0
        public void TryTrack()
        {
            const string testName = "TryTrack";

            var tests = new[]
            {
                new { Type = ImageTypes.UInt8, ExpectResult = true }
            };

            var type = this.GetType().Name;

            foreach (var input in tests)
            {
                var tracker = new CorrelationTracker();
                try
                {
                    var index = 0;
                    foreach (var file in this.GetDataFiles("video_frames").Where(info => info.Name.EndsWith("jpg")))
                    {
                        var expectResult = input.ExpectResult;
                        var inputType    = input.Type;
                        var imageObj     = DlibTest.LoadImage(input.Type, file);

                        if (index == 0)
                        {
                            using (var rect = DRectangle.CenteredRect(93, 110, 38, 86))
                                tracker.StartTrack(imageObj, rect);
                        }

                        var outputImageAction = new Func <bool, Array2DBase>(expect =>
                        {
                            if (index != 0)
                            {
                                tracker.Update(imageObj);
                            }

                            return(imageObj);
                        });

                        var successAction = new Action <Array2DBase>(image =>
                        {
                            if (index != 0)
                            {
                                tracker.Update(image);
                            }
                            using (var r = tracker.GetPosition())
                            {
#if DEBUG
                                using (var tmp = Image.FromFile(file.FullName))
                                    using (var bmp = new Bitmap(tmp))
                                        using (var g = Graphics.FromImage(bmp))
                                            using (var p = new Pen(Color.Red, 3f))
                                            {
                                                g.DrawRectangle(p, (float)r.Left, (float)r.Top, (float)r.Width, (float)r.Height);
                                                bmp.Save(Path.Combine(this.GetOutDir(type), $"{Path.GetFileNameWithoutExtension(file.FullName)}.jpg"));
                                            }
#endif
                            }
                        });

                        var failAction = new Action(() =>
                        {
                            Assert.Fail($"{testName} should throw excption for InputType: {inputType}.");
                        });

                        var finallyAction = new Action(() =>
                        {
                            index++;

                            if (imageObj != null)
                            {
                                this.DisposeAndCheckDisposedState(imageObj);
                            }
                        });

                        var exceptionAction = new Action(() =>
                        {
                            Console.WriteLine($"Failed to execute {testName} to InputType: {inputType}.");
                        });

                        DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction);
                    }
                }
                finally
                {
                    this.DisposeAndCheckDisposedState(tracker);
                }
            }
        }