예제 #1
0
        public void Go()
        {
            using (IVideo video = ModelResolver.Resolve <IVideo>())
            {
                video.SetVideo(FileName);
                IMouseDataExtendedResult result = ModelResolver.Resolve <IMouseDataExtendedResult>();

                if (!PreviewGenerated)
                {
                    GeneratePreview(video);
                }

                result.FrameRate = video.FrameRate;

                IRBSKVideo2 rbskVideo = ModelResolver.Resolve <IRBSKVideo2>();
                rbskVideo.Video           = video;
                rbskVideo.BackgroundImage = BinaryBackground;
                rbskVideo.ThresholdValue  = VideoSettings.ThresholdValue;
            }
        }
예제 #2
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, "*.arwt");

            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));
                if (mouse.Results == null)
                {
                    mouse.Results = new Dictionary <ISingleFile, IMouseDataExtendedResult>();
                }

                if (mouse.Results.ContainsKey(singleFile))
                {
                    //Already been added

                    continue;
                }

                XmlSerializer serializer = new XmlSerializer(typeof(TrackedVideoWithSettingsXml));
                TrackedVideoWithSettingsXml trackedVideoXml;

                try
                {
                    using (StreamReader reader = new StreamReader(file))
                    {
                        trackedVideoXml = (TrackedVideoWithSettingsXml)serializer.Deserialize(reader);
                    }
                }
                catch (Exception)
                {
                    using (StreamReader reader = new StreamReader(file))
                    {
                        XmlSerializer serializer2 = new XmlSerializer(typeof(TrackedVideoXml));
                        trackedVideoXml = new TrackedVideoWithSettingsXml((TrackedVideoXml)serializer2.Deserialize(reader));
                    }
                }

                ITrackedVideo trackedVideo = trackedVideoXml.GetData();

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

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

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

                    continue;
                }

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

                //    if (kvp.Value != null)
                //    {
                //        ISingleFrameExtendedResults 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           = trackedVideo.Results;
                mouseDataResult.Age               = mouse.Age;
                mouseDataResult.Boundaries        = trackedVideo.Boundries;
                mouseDataResult.VideoOutcome      = SingleFileResult.Ok;
                mouseDataResult.GapDistance       = trackedVideo.GapDistance;
                mouseDataResult.WhiskerSettings   = trackedVideo.WhiskerSettings;
                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, IMouseDataExtendedResult>();
                }

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

            ExportAllCommand.RaiseCanExecuteChangedNotification();
        }
예제 #3
0
        public void RunFiles(string outputLocation)
        {
            if (!string.IsNullOrWhiteSpace(outputLocation))
            {
                if (!outputLocation.EndsWith("\\"))
                {
                    outputLocation += "\\";
                }
            }

            Rbsk = new ConcurrentBag <IRBSKVideo2>();
            ConcurrentDictionary <ISingleFile, IMouseDataExtendedResult> allResults = new ConcurrentDictionary <ISingleFile, IMouseDataExtendedResult>();
            List <ISingleFile> files = VideoFiles;

            foreach (var file in files)
            {
                IMouseDataExtendedResult result = ModelResolver.Resolve <IMouseDataExtendedResult>();
                result.Name = file.VideoFileName;
                result.Type = Type;

                ISaveArtFile save = ModelResolver.Resolve <ISaveArtFile>();
                string       artFile;
                string       videoFile = file.VideoFileName;
                if (string.IsNullOrWhiteSpace(outputLocation))
                {
                    string extension = Path.GetExtension(videoFile);
                    artFile = videoFile.Replace(extension, ".arwt");
                }
                else
                {
                    string fileName = Path.GetFileNameWithoutExtension(videoFile);
                    fileName += ".arwt";
                    artFile   = outputLocation + fileName;
                }

                ArtFile = artFile;

                if (File.Exists(artFile))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(TrackedVideoWithSettingsXml));
                    TrackedVideoWithSettingsXml trackedVideoXml;

                    try
                    {
                        using (StreamReader reader = new StreamReader(artFile))
                        {
                            trackedVideoXml = (TrackedVideoWithSettingsXml)serializer.Deserialize(reader);
                        }
                    }
                    catch (Exception)
                    {
                        using (StreamReader reader = new StreamReader(artFile))
                        {
                            XmlSerializer serializer2 = new XmlSerializer(typeof(TrackedVideoXml));
                            trackedVideoXml = new TrackedVideoWithSettingsXml((TrackedVideoXml)serializer2.Deserialize(reader));
                        }
                    }

                    ITrackedVideo trackedVideo = trackedVideoXml.GetData();

                    result.Boundaries        = trackedVideo.Boundries;
                    result.VideoOutcome      = trackedVideo.Result;
                    result.GapDistance       = trackedVideo.GapDistance;
                    result.WhiskerSettings   = trackedVideo.WhiskerSettings;
                    result.ThresholdValue    = trackedVideo.ThresholdValue;
                    result.ThresholdValue2   = trackedVideo.ThresholdValue2;
                    result.StartFrame        = trackedVideo.StartFrame;
                    result.EndFrame          = trackedVideo.EndFrame;
                    result.SmoothMotion      = trackedVideo.SmoothMotion;
                    result.FrameRate         = trackedVideo.FrameRate;
                    result.UnitsToMilimeters = trackedVideo.UnitsToMilimeters;
                    result.SmoothFactor      = 0.68;
                    result.GenerateResults(artFile);
                    result.PelvicArea  = trackedVideo.PelvicArea1;
                    result.PelvicArea2 = trackedVideo.PelvicArea2;
                    result.PelvicArea3 = trackedVideo.PelvicArea3;
                    result.PelvicArea4 = trackedVideo.PelvicArea4;
                    result.Results     = trackedVideo.Results;
                    result.GenerateResults();
                    result.DataLoadComplete();

                    allResults.TryAdd(file, result);

                    UpdateProgress(file, 1);
                    continue;
                }

                try
                {
                    IVideoSettings videoSettings = ModelResolver.Resolve <IVideoSettings>();
                    using (IRBSKVideo2 rbskVideo = ModelResolver.Resolve <IRBSKVideo2>())
                        using (IVideo video = ModelResolver.Resolve <IVideo>())
                        {
                            Rbsk.Add(rbskVideo);
                            video.SetVideo(file.VideoFileName);
                            if (video.FrameCount <= 100)
                            {
                                result.VideoOutcome = SingleFileResult.FrameCountTooLow;
                                result.Message      = "Exception: " + file.VideoFileName + " - Frame count too low";
                                allResults.TryAdd(file, result);
                                UpdateProgress(file, 1);
                                save.SaveFile(artFile, videoFile, result);
                                continue;
                            }

                            result.FrameRate = video.FrameRate;
                            video.SetFrame(0);

                            videoSettings.FileName       = file.VideoFileName;
                            videoSettings.ThresholdValue = ThresholdValue;
                            videoSettings.Roi            = ROI;
                            Image <Gray, Byte>          binaryBackground;
                            IEnumerable <IBoundaryBase> boundaries;
                            videoSettings.GeneratePreview(video, out binaryBackground, out boundaries);
                            result.Boundaries = boundaries.ToArray();

                            rbskVideo.Video            = video;
                            rbskVideo.GapDistance      = GapDistance;
                            rbskVideo.BackgroundImage  = binaryBackground;
                            rbskVideo.ThresholdValue   = ThresholdValue;
                            rbskVideo.ThresholdValue2  = ThresholdValue2;
                            rbskVideo.Roi              = ROI;
                            rbskVideo.WhiskerSettings  = WhiskerSettings;
                            rbskVideo.FindWhiskers     = true;
                            rbskVideo.ProgressUpdates += (s, e) => UpdateProgress(file, e.Progress);
                            rbskVideo.Process();

                            if (Stop)
                            {
                                return;
                            }

                            result.GapDistance            = rbskVideo.GapDistance;
                            result.WhiskerSettings        = rbskVideo.WhiskerSettings;
                            result.MinInteractionDistance = 15;
                            result.ThresholdValue         = rbskVideo.ThresholdValue;
                            result.ThresholdValue2        = rbskVideo.ThresholdValue2;
                            result.WhiskerSettings        = WhiskerSettings;
                            result.Results = rbskVideo.HeadPoints;
                            result.ResetFrames();
                            //result.FrameRate = FrameRate;
                            result.SmoothMotion = SmoothMotion;
                            result.GenerateResults();
                            result.VideoOutcome = SingleFileResult.Ok;
                            result.DataLoadComplete();
                            allResults.TryAdd(file, result);

                            save.SaveFile(artFile, videoFile, result);
                        }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                    Console.WriteLine(e.Message);
                    result.VideoOutcome = SingleFileResult.Error;
                    result.Message      = "Exception: " + file.VideoFileName + " - " + e.Message + " - " + e.StackTrace;
                    result.DataLoadComplete();
                    allResults.TryAdd(file, result);
                    UpdateProgress(file, 1);
                    save.SaveFile(artFile, videoFile, result);
                }

                if (Cancel)
                {
                    break;
                }

                if (Stop)
                {
                    return;
                }
            }

            if (Stop)
            {
                return;
            }

            Application.Current.Dispatcher.Invoke(() =>
            {
                try
                {
                    Results = allResults.ToDictionary(x => x.Key, x => x.Value);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error in single mouse viewmodel!!! " + e.InnerException);
                }
            });
        }
예제 #4
0
        public void LoadData(IMouseDataExtendedResult data)
        {
            StartFrame = 0;
            KeyValuePair <int, ISingleFrameExtendedResults> kvp = data.Results.FirstOrDefault(x => x.Value.HeadPoints == null);

            if (kvp.Equals(default(KeyValuePair <int, ISingleFrameExtendedResults>)))
            {
                EndFrame = data.Results.Count;
            }
            else
            {
                EndFrame = kvp.Key;
            }

            RepeatSmooths             = 1;
            Data                      = data;
            MaxVelocity               = data.MaxSpeed;
            MaxCentroidVelocity       = data.MaxCentroidSpeed;
            MaxAngularVelocity        = data.MaxAngularVelocty;
            AverageVelocity           = data.AverageVelocity;
            AverageCentroidVelocity   = data.AverageCentroidVelocity;
            AverageAngularVelocity    = data.AverageAngularVelocity;
            DistanceTravelled         = data.DistanceTravelled;
            CentroidDistanceTravelled = data.CentroidDistanceTravelled;
            OriginalFrameRate         = data.FrameRate;
            UnitsToMm                 = data.UnitsToMilimeters;

            StatGenerator stat = new StatGenerator();

            stat.StartFrame        = StartFrame;
            stat.EndFrame          = EndFrame;
            stat.SmoothingFunction = SelectedSmoothingFunction;
            stat.UseDft            = UseDft;
            stat.SmoothRepeats     = RepeatSmooths;

            stat.Generate(data.Results, OriginalFrameRate);

            IWhiskerAverageAngles angles = new WhiskerAverageAngles();

            angles.StartFrame = 0;
            angles.EndFrame   = data.Results.Count - 1;
            var allAngles = angles.GetWhiskerAngles(data.Results);

            LoadData(allAngles[0], allAngles[1]);


            LeftWhiskerAmplitude               = stat.LeftWhiskerAmplitude;
            RightWhiskerAmplitude              = stat.RightWhiskerAmplitude;
            LeftWhiskerAvgAngularVelocity      = stat.LeftAverageAngularVelocity;
            RightWhiskerAvgAngularVelocity     = stat.RightAverageAngularVelocity;
            LeftWhiskerAvgProtractionVelocity  = stat.LeftAverageProtractionVelocity;
            RightWhiskerAvgProtractionVelocity = stat.RightAverageProtractionVelocity;
            LeftWhiskerAvgRetractionVelocity   = stat.LeftAverageRetractionVelocity;
            RightWhiskerAvgRetractionVelocity  = stat.RightAverageRetractionVelocity;
            LeftWhiskerFrequency               = stat.LeftWhiskerFrequency;
            RightWhiskerFrequency              = stat.RightWhiskerFrequnecy;
            LeftWhiskerMeanOffset              = stat.LeftMeanOffset;
            RightWhiskerMeanOffset             = stat.RightMeanOffset;

            UpdateSmoothing();
        }
예제 #5
0
 public void SaveFile(string fileLocation, string videoFileName, IMouseDataExtendedResult data)
 {
     SaveFile(fileLocation, videoFileName, data.VideoOutcome, data.Results, data.MotionTrack, data.SmoothedMotionTrack, data.OrientationTrack, data.Events, data.Boundaries, data.InteractingBoundries, data.MinInteractionDistance, data.GapDistance, data.ThresholdValue, data.ThresholdValue2, data.StartFrame, data.EndFrame, data.FrameRate, data.SmoothMotion, data.SmoothFactor, data.CentroidSize, data.PelvicArea, data.PelvicArea2, data.PelvicArea3, data.PelvicArea4, data.UnitsToMilimeters, data.ROI, data.WhiskerSettings, data.Message);
 }