Exemple #1
0
        public SingleFrameResultXml(ISingleFrameResult result)
        {
            if (result.HeadPoints == null)
            {
                HeadPoints = null;
                HeadPoint  = null;
            }
            else
            {
                HeadPoints = result.HeadPoints.Select(x => new PointFXml(x.X, x.Y)).ToArray();
                HeadPoint  = HeadPoints[2];
            }

            SmoothedHeadPoint = new PointFXml(result.SmoothedHeadPoint);

            Orientation     = new VectorXml(result.Orientation);
            CentroidSize    = result.CentroidSize;
            PelvicArea      = result.PelvicArea;
            Velocity        = result.Velocity;
            AngularVelocity = result.AngularVelocity;
            Distance        = result.Distance;
            PelvicArea2     = result.PelvicArea2;
            PelvicArea3     = result.PelvicArea3;
            PelvicArea4     = result.PelvicArea4;
            Centroid        = new PointFXml(result.Centroid);
        }
Exemple #2
0
        public ISingleFrameResult GetData()
        {
            ISingleFrameResult result = ModelResolver.Resolve <ISingleFrameResult>();

            if (HeadPoints != null)
            {
                result.HeadPoints = HeadPoints.Select(x => x.GetPoint()).ToArray();
            }
            else
            {
                result.HeadPoints = null;
            }

            if (HeadPoint != null)
            {
                result.HeadPoint = HeadPoint.GetPoint();
            }

            if (SmoothedHeadPoint != null)
            {
                result.SmoothedHeadPoint = SmoothedHeadPoint.GetPoint();
            }

            if (Orientation != null)
            {
                result.Orientation = Orientation.GetVector();
            }

            result.CentroidSize    = CentroidSize;
            result.PelvicArea      = PelvicArea;
            result.Velocity        = Velocity;
            result.AngularVelocity = AngularVelocity;
            result.Distance        = Distance;
            result.PelvicArea2     = PelvicArea2;
            result.PelvicArea3     = PelvicArea3;
            result.PelvicArea4     = PelvicArea4;

            if (Centroid != null)
            {
                result.Centroid = Centroid.GetPoint();
            }

            result.DataLoadComplete();
            return(result);
        }
Exemple #3
0
        private void UpdateDisplayImage()
        {
            using (Image <Bgr, Byte> img = CurrentImage.Clone())
            {
                img.DrawPolyline(MotionTrack.Select(x => x.ToPoint()).ToArray(), false, new Bgr(Color.Blue), 2);
                img.DrawPolyline(CentroidMotionTrack.Select(x => x.ToPoint()).ToArray(), false, new Bgr(Color.Yellow), 2);

                if (SliderValue >= AnalyseStart && SliderValue <= AnalyseEnd)
                {
                    ISingleFrameResult frame = CurrentResult.Results[SliderValue];

                    if (!frame.HeadPoint.IsEmpty)
                    {
                        img.Draw(new CircleF(frame.HeadPoint, 2), new Bgr(Color.Red), 2);
                        img.Draw(new CircleF(frame.Centroid, 2), new Bgr(Color.Red), 2);
                    }
                }

                DisplayImage = ImageService.ToBitmapSource(img);
            }
        }
Exemple #4
0
        public void UpdateTrack()
        {
            if (Results == null || Results.Count == 0)
            {
                return;
            }

            List <PointF> motionTrack      = new List <PointF>();
            List <Vector> orientationTrack = new List <Vector>();
            int           startOffset      = Results.First(x => x.Value != null).Key;

            int frameCount = Results.Count;

            for (int i = 0; i < frameCount; i++)
            {
                ISingleFrameResult currentFrameResult = Results[i];

                if (currentFrameResult == null)
                {
                    continue;
                }

                PointF[] headPoints = currentFrameResult.HeadPoints;
                if (headPoints != null)
                {
                    PointF midPoint = headPoints[1].MidPoint(headPoints[3]);
                    //Vector up = new Vector(0,1);
                    Vector dir = new Vector(headPoints[2].X - midPoint.X, headPoints[2].Y - midPoint.Y);
                    orientationTrack.Add(dir);

                    motionTrack.Add(headPoints[2]);
                }
            }

            MotionTrack = motionTrack.ToArray();
            //OrientationTrack = orientationTrack.ToArray();
            GenerateBehaviourAnalysis(MotionTrack, Boundries, startOffset);
        }
Exemple #5
0
        //private void Closing(object args)
        //{
        //    if (!Running)
        //    {
        //        return;
        //    }

        //    var result = MessageBox.Show("The program is currently running, are you sure you want to cancel it?", "Batch Running", MessageBoxButton.YesNo, MessageBoxImage.Question);

        //    if (result == MessageBoxResult.Yes)
        //    {
        //        IEnumerable<SingleMouseViewModel> allMice = TgItemsSource.Concat(NtgItemsSource);
        //        foreach (SingleMouseViewModel mouse in allMice)
        //        {
        //            mouse.Stop = true;
        //        }
        //        Running = false;
        //        return;
        //    }

        //    CancelEventArgs cancelEventArgs = args as CancelEventArgs;
        //    if (cancelEventArgs != null)
        //    {
        //        cancelEventArgs.Cancel = true;
        //    }
        //}

        private void LoadOutputFolder()
        {
            IRepository repo            = RepositoryResolver.Resolve <IRepository>();
            string      initialLocation = repo.GetValue <string>("OutputFolderLocation");

            string folderLocation = FileBrowser.BrowseForFolder(initialLocation);

            if (string.IsNullOrWhiteSpace(folderLocation))
            {
                return;
            }

            repo.SetValue("OutputFolderLocation", folderLocation);
            repo.Save();

            string[] artFiles = Directory.GetFiles(folderLocation, "*.art");

            foreach (string file in artFiles)
            //Parallel.ForEach(artFiles, file =>
            {
                string videoFileName = Path.GetFileNameWithoutExtension(file);

                //if (videoFileName.Contains("091119-0038"))
                //{
                //    Console.WriteLine("h");
                //}

                if (string.IsNullOrWhiteSpace(videoFileName))
                {
                    continue;
                    //return;
                }

                //if (videoFileName.Contains("091218-0179"))
                //{
                //    Console.WriteLine("test");
                //}

                //SingleMouseViewModel mouse = null;
                ////videoFileName = file.Replace(".art", "");
                //bool testBreak = false;
                //foreach (var listMouse in TgItemsSource)
                //{
                //    foreach (var temp in listMouse.VideoFiles)
                //    {
                //        if (temp.VideoFileName.Contains(videoFileName))
                //        {
                //            mouse = listMouse;
                //            testBreak = true;
                //            break;
                //        }
                //    }

                //    if (testBreak)
                //    {
                //        break;
                //    }
                //}

                SingleMouseViewModel mouse = TgItemsSource.FirstOrDefault(x => x.VideoFiles.FirstOrDefault(y => y.VideoFileName.Contains(videoFileName)) != null);

                if (mouse == null)
                {
                    //Can't find mouse
                    //return;
                    continue;
                }

                ISingleFile singleFile = mouse.VideoFiles.First(x => x.VideoFileName.Contains(videoFileName));

                XmlSerializer   serializer = new XmlSerializer(typeof(TrackedVideoXml));
                TrackedVideoXml trackedVideoXml;
                using (StreamReader reader = new StreamReader(file))
                {
                    trackedVideoXml = (TrackedVideoXml)serializer.Deserialize(reader);
                }

                ITrackedVideo trackedVideo = trackedVideoXml.GetData();

                IMouseDataResult mouseDataResult = ModelResolver.Resolve <IMouseDataResult>();

                if (trackedVideo.Result != SingleFileResult.Ok)
                {
                    mouseDataResult.Message      = trackedVideo.Message;
                    mouseDataResult.VideoOutcome = trackedVideo.Result;

                    if (mouse.Results == null)
                    {
                        mouse.Results = new Dictionary <ISingleFile, IMouseDataResult>();
                    }
                    mouse.Results.Add(singleFile, mouseDataResult);
                    mouse.ReviewCommand.RaiseCanExecuteChangedNotification();
                    mouse.UpdateProgress(singleFile, 1);

                    continue;
                }

                var tempResults = trackedVideo.Results;
                Dictionary <int, ISingleFrameResult> results = new Dictionary <int, ISingleFrameResult>();
                foreach (var kvp in tempResults)
                {
                    ISingleFrameResult singleFrame = ModelResolver.Resolve <ISingleFrameResult>();

                    if (kvp.Value != null)
                    {
                        ISingleFrameResult tempResult = kvp.Value;
                        singleFrame.HeadPoints        = tempResult.HeadPoints;
                        singleFrame.CentroidSize      = tempResult.CentroidSize;
                        singleFrame.PelvicArea        = tempResult.PelvicArea;
                        singleFrame.PelvicArea2       = tempResult.PelvicArea2;
                        singleFrame.PelvicArea3       = tempResult.PelvicArea3;
                        singleFrame.PelvicArea4       = tempResult.PelvicArea4;
                        singleFrame.SmoothedHeadPoint = tempResult.SmoothedHeadPoint;
                        singleFrame.Centroid          = tempResult.Centroid;
                    }

                    results.Add(kvp.Key, singleFrame);
                }


                mouseDataResult.Name              = mouse.Id;
                mouseDataResult.Results           = results;
                mouseDataResult.Age               = mouse.Age;
                mouseDataResult.Boundaries        = trackedVideo.Boundries;
                mouseDataResult.VideoOutcome      = SingleFileResult.Ok;
                mouseDataResult.GapDistance       = trackedVideo.GapDistance;
                mouseDataResult.ThresholdValue    = trackedVideo.ThresholdValue;
                mouseDataResult.ThresholdValue2   = trackedVideo.ThresholdValue2;
                mouseDataResult.StartFrame        = trackedVideo.StartFrame;
                mouseDataResult.EndFrame          = trackedVideo.EndFrame;
                mouseDataResult.SmoothMotion      = trackedVideo.SmoothMotion;
                mouseDataResult.FrameRate         = trackedVideo.FrameRate;
                mouseDataResult.UnitsToMilimeters = trackedVideo.UnitsToMilimeters;
                mouseDataResult.SmoothFactor      = 0.68;
                mouseDataResult.GenerateResults(file);
                mouseDataResult.PelvicArea  = trackedVideo.PelvicArea1;
                mouseDataResult.PelvicArea2 = trackedVideo.PelvicArea2;
                mouseDataResult.PelvicArea3 = trackedVideo.PelvicArea3;
                mouseDataResult.PelvicArea4 = trackedVideo.PelvicArea4;
                mouseDataResult.Type        = mouse.Type;
                mouseDataResult.DataLoadComplete();

                if (mouse.Results == null)
                {
                    mouse.Results = new Dictionary <ISingleFile, IMouseDataResult>();
                }

                mouse.UpdateProgress(singleFile, 1);
                mouse.Results.Add(singleFile, mouseDataResult);
                mouse.ReviewCommand.RaiseCanExecuteChangedNotification();
            }//);

            ExportAllCommand.RaiseCanExecuteChangedNotification();
        }
Exemple #6
0
        private Dictionary <int, ISingleFrameResult> GenerateDictionary()
        {
            //const int SecondBinaryThresold = 10;
            const double movementDelta = 15;

            Services.RBSK.RBSK rbsk = MouseService.GetStandardMouseRules();

            if (GapDistance <= 0)
            {
                GapDistance = GetBestGapDistance(rbsk);
            }

            rbsk.Settings.GapDistance     = GapDistance;
            rbsk.Settings.BinaryThreshold = ThresholdValue;

            Dictionary <int, ISingleFrameResult> results = new Dictionary <int, ISingleFrameResult>();

            Video.Reset();
            int counter = 0;

            int frameCount = Video.FrameCount;

            while (true)
            {
                if (Paused)
                {
                    continue;
                }

                if (Cancelled)
                {
                    break;
                }

                using (Image <Bgr, byte> frame = Video.GetFrameImage())
                {
                    if (frame == null)
                    {
                        break;
                    }

                    if (Roi != Rectangle.Empty)
                    {
                        frame.ROI = Roi;
                    }

                    double waist, waistArea, waistArea2, waistArea3, waistArea4;
                    PointF centroid;

                    //if (counter == 500)
                    //{
                    //    frame.ROI = new Rectangle(0,0,1,1);
                    //}

                    PointF[] headPoints = ProcessFrame(frame, rbsk, out waist, out waistArea, out waistArea2, out waistArea3, out waistArea4, out centroid, true);

                    if (Roi != Rectangle.Empty)
                    {
                        centroid.X += Roi.X;
                        centroid.Y += Roi.Y;

                        if (headPoints != null)
                        {
                            headPoints[0].X += Roi.X;
                            headPoints[0].Y += Roi.Y;
                            headPoints[1].X += Roi.X;
                            headPoints[1].Y += Roi.Y;
                            headPoints[2].X += Roi.X;
                            headPoints[2].Y += Roi.Y;
                            headPoints[3].X += Roi.X;
                            headPoints[3].Y += Roi.Y;
                            headPoints[4].X += Roi.X;
                            headPoints[4].Y += Roi.Y;
                        }
                    }

                    //Console.WriteLine(waist + " " + waistArea);
                    //if (headPoints != null)
                    //{
                    //    foreach (var point in headPoints)
                    //    {
                    //        frame.Draw(new CircleF(point, 2), new Bgr(Color.Yellow), 2);
                    //    }
                    //}

                    //ImageViewer.Show(frame);
                    ISingleFrameResult frameResult = ModelResolver.Resolve <ISingleFrameResult>();
                    frameResult.HeadPoints   = headPoints;
                    frameResult.CentroidSize = waist;
                    frameResult.PelvicArea   = waistArea;
                    frameResult.PelvicArea2  = waistArea2;
                    frameResult.PelvicArea3  = waistArea3;
                    frameResult.PelvicArea4  = waistArea4;
                    frameResult.Centroid     = centroid;
                    results.Add(counter, frameResult);
                    counter++;

                    if (ProgressUpdates != null)
                    {
                        ProgressUpdates(this, new RBSKVideoUpdateEvent((double)counter / frameCount));
                    }
                }
            }

            if (Cancelled)
            {
                return(results);
            }

            //Find the most confident run of head detections
            List <List <HeadPointHolder> > confidentPoints = new List <List <HeadPointHolder> >();
            PointF lastPoint = PointF.Empty;
            List <HeadPointHolder> currentList = new List <HeadPointHolder>();
            double currentTotalMovement        = 0;

            for (int i = 0; i < results.Count; i++)
            {
                PointF[] headPoints = results[i].HeadPoints;

                if (headPoints == null)
                {
                    //No head found, use alternative methods
                    if (currentList.Count > 0)
                    {
                        confidentPoints.Add(currentList);
                    }
                    currentList          = new List <HeadPointHolder>();
                    lastPoint            = PointF.Empty;
                    currentTotalMovement = 0;
                    //Console.WriteLine("Head points are null " + i);
                    continue;
                }

                //Check against expected position
                PointF currentPoint = headPoints[2];

                if (lastPoint.IsEmpty)
                {
                    lastPoint = currentPoint;
                    currentList.Add(new HeadPointHolder(i, currentPoint, currentTotalMovement));
                    continue;
                }

                double distance = lastPoint.Distance(currentPoint);
                if (distance < movementDelta)
                {
                    //Acceptable
                    currentTotalMovement += distance;
                    currentList.Add(new HeadPointHolder(i, currentPoint, currentTotalMovement));
                    lastPoint = currentPoint;
                }
                else
                {
                    if (currentList.Count > 0)
                    {
                        confidentPoints.Add(currentList);
                    }
                    currentList          = new List <HeadPointHolder>();
                    lastPoint            = PointF.Empty;
                    currentTotalMovement = 0;
                    //Console.WriteLine("Outside of range " + i);
                }
            }

            if (currentList.Count > 0)
            {
                confidentPoints.Add(currentList);
            }

            if (confidentPoints.Count == 0)
            {
                return(results);
            }

            //Find longest list with highest total movement
            List <HeadPointHolder> bestList = confidentPoints[0];

            //double currentMaxTraverse = bestList.Last().TotalDelta;
            foreach (List <HeadPointHolder> list in confidentPoints)
            {
                //double currentTotalDelta = list.Last().TotalDelta;
                if (list.Count > bestList.Count)
                {
                    //currentMaxTraverse = currentTotalDelta;
                    bestList = list;
                }
            }

            //We now have a confident set of headpoints
            int minIndex = bestList.Select(x => x.FrameNumber).Min();
            int maxIndex = bestList.Select(x => x.FrameNumber).Max();


            minIndex--;
            while (minIndex >= 0)
            {
                //Traverse backwards
                PointF[] lastPoints = results[minIndex + 1].HeadPoints;
                if (lastPoints != null)
                {
                    lastPoint = lastPoints[2];
                }
                else
                {
                    lastPoint = new PointF(-100, -100);
                }

                PointF[] headPoints = results[minIndex].HeadPoints;

                if (headPoints == null)
                {
                    //No head found, use alternative methods
                    int previousThreshold = rbsk.Settings.BinaryThreshold;
                    rbsk.Settings.BinaryThreshold = ThresholdValue2;
                    Video.SetFrame(minIndex);
                    PointF[] headPoints2 = null;
                    using (Image <Bgr, byte> frame = Video.GetFrameImage())
                    {
                        if (frame == null)
                        {
                            break;
                        }

                        if (Roi != Rectangle.Empty)
                        {
                            frame.ROI = Roi;
                        }

                        headPoints2 = ProcessFrame(frame, rbsk, lastPoint, movementDelta);
                    }

                    rbsk.Settings.BinaryThreshold = previousThreshold;

                    if (headPoints2 != null)
                    {
                        //We've got a good location
                        //double temp = results[minIndex].CentroidSize;
                        results[minIndex].HeadPoints = headPoints2;
                        //results[minIndex].CentroidSize = temp;
                        //results[minIndex] = new Tuple<PointF[], double>(headPoints2, temp);
                    }
                    else
                    {
                        for (int i = 0; i <= minIndex; i++)
                        {
                            if (results.ContainsKey(i))
                            {
                                //double temp = results[i].Item2;
                                //results[i] = new Tuple<PointF[], double>(null, temp);
                                results[i].HeadPoints = null;
                            }
                        }

                        break;
                    }

                    minIndex--;
                    continue;
                }

                //Check against expected position
                PointF currentPoint = headPoints[2];

                if (lastPoint.Distance(currentPoint) < movementDelta)
                {
                    //Good point
                }
                else
                {
                    //Wrong point, search for another rbsk that falls within range
                    //if (minIndex == 17 || minIndex == 16)
                    //{
                    //    Console.WriteLine("");
                    //}

                    Video.SetFrame(minIndex);
                    PointF[] headPoints2 = null;
                    using (Image <Bgr, byte> frame = Video.GetFrameImage())
                    {
                        if (frame == null)
                        {
                            break;
                        }

                        if (Roi != Rectangle.Empty)
                        {
                            frame.ROI = Roi;
                        }

                        headPoints2 = ProcessFrame(frame, rbsk, lastPoint, movementDelta);
                    }

                    if (headPoints2 != null)
                    {
                        //We've got a good location
                        //double temp = results[maxIndex].Item2;
                        //results[minIndex] = new Tuple<PointF[], double>(headPoints2, temp);
                        results[minIndex].HeadPoints = headPoints2;
                    }
                    else
                    {
                        //No other rbsk falls within range, use alternative methods
                        //Console.WriteLine("Need to use alternative methods");
                        for (int i = 0; i <= minIndex; i++)
                        {
                            if (results.ContainsKey(i))
                            {
                                //double temp = results[i].Item2;
                                //results[i] = new Tuple<PointF[], double>(null, temp);
                                results[i].HeadPoints = null;
                            }
                        }

                        break;
                    }
                }

                minIndex--;
            }

            maxIndex++;
            while (maxIndex < results.Count)
            {
                //Traverse backwards
                PointF[] lastPoints = results[maxIndex - 1].HeadPoints;
                if (lastPoints != null)
                {
                    lastPoint = lastPoints[2];
                }
                else
                {
                    lastPoint = new PointF(-100, -100);
                }

                PointF[] headPoints = results[maxIndex].HeadPoints;


                if (headPoints == null)
                {
                    //No head found, use alternative methods
                    int previousThreshold = rbsk.Settings.BinaryThreshold;
                    rbsk.Settings.BinaryThreshold = ThresholdValue2;
                    Video.SetFrame(maxIndex);
                    PointF[] headPoints2 = null;
                    using (Image <Bgr, byte> frame = Video.GetFrameImage())
                    {
                        if (frame == null)
                        {
                            break;
                        }

                        if (Roi != Rectangle.Empty)
                        {
                            frame.ROI = Roi;
                        }

                        headPoints2 = ProcessFrame(frame, rbsk, lastPoint, movementDelta);
                    }

                    rbsk.Settings.BinaryThreshold = previousThreshold;

                    if (headPoints2 != null)
                    {
                        //We've got a good location
                        //double temp = results[maxIndex].Item2;
                        //results[maxIndex] = new Tuple<PointF[], double>(headPoints2, temp);
                        results[maxIndex].HeadPoints = headPoints2;
                    }
                    else
                    {
                        int max = results.Keys.Max();
                        for (int i = maxIndex; i <= max; i++)
                        {
                            if (results.ContainsKey(i))
                            {
                                //double temp = results[i].Item2;
                                //results[i] = new Tuple<PointF[], double>(null, temp);
                                results[i].HeadPoints = null;
                            }
                        }

                        break;
                    }

                    maxIndex++;
                    continue;
                }

                //Check against expected position
                PointF currentPoint = headPoints[2];

                if (lastPoint.Distance(currentPoint) < movementDelta)
                {
                    //Good point
                }
                else
                {
                    //Wrong point, search for another rbsk that falls within range
                    Video.SetFrame(maxIndex);
                    PointF[] headPoints2 = null;
                    using (Image <Bgr, byte> frame = Video.GetFrameImage())
                    {
                        if (frame == null)
                        {
                            break;
                        }

                        if (Roi != Rectangle.Empty)
                        {
                            frame.ROI = Roi;
                        }

                        headPoints2 = ProcessFrame(frame, rbsk, lastPoint, movementDelta);
                    }

                    if (headPoints2 != null)
                    {
                        //We've got a good location
                        //double temp = results[maxIndex].Item2;
                        //results[maxIndex] = new Tuple<PointF[], double>(headPoints2, temp);
                        results[maxIndex].HeadPoints = headPoints2;
                    }
                    else
                    {
                        //No other rbsk falls within range, use alternative methods
                        //Console.WriteLine("Need to use alternative methods");
                        int max = results.Keys.Max();
                        for (int i = maxIndex; i <= max; i++)
                        {
                            if (results.ContainsKey(i))
                            {
                                //double temp = results[i].Item2;
                                //results[i] = new Tuple<PointF[], double>(null, temp);
                                results[i].HeadPoints = null;
                            }
                        }

                        break;
                    }
                }

                maxIndex++;
            }

            return(results);
        }