Exemplo n.º 1
0
        /// <summary>
        /// Do all the heavy lifting on the background thread.
        /// Request the data, sort it and force LINQ to exectute the query.
        /// Using the parallel libraries, for each returned object perform the following:
        ///     run the validation checks for each object,
        ///     and make each data movie if its already in our database.
        /// </summary>
        void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            //ToList forces the LINQ query to execute here.
            //We need the query to not perform delayed execution so that the paralled methods will run correctly.
            var results = RemoteDataStore.SearchByName(e.Argument.ToString()).OrderBy(r => r.Name).ToList();

            //Read up on PLINQ and Parallel and why to use Parallel here.
            //http://services.social.microsoft.com/feeds/FeedItem?feedId=639a99a9-ff25-4062-b61d-a86ea9d66a06&itemId=a611e616-779e-4bb9-b88f-8ab228165a82&title=When+Should+I+Use+Parallel.ForEach%3f+When+Should+I+Use+PLINQ%3f&uri=http%3a%2f%2fdownload.microsoft.com%2fdownload%2fB%2fC%2fF%2fBCFD4868-1354-45E3-B71B-B851CD78733D%2fWhenToUseParallelForEachOrPLINQ.pdf&k=gAdKnW7xH8RXzlALrQceTdMvpCzxVujB07sMnqbwvgM%3d
            //
            results.AsParallel().ForAll(r => { r.IsValid(); r.InLocalDatabase = MovieDataStoreService.Keys.Contains(r.Id); });
            e.Result = results;
        }
Exemplo n.º 2
0
        private Task DoStop(object o)
        {
            return(Task.Factory.StartNew(() =>
            {
                string expactString = string.Format("{0}@{1}:.{{0,}}[$]", Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Hostname);

                RemoteDataStore remoteDataStore = new RemoteDataStore(Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.IpAddress, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Password);
                remoteDataStore.ExecuteCommands(new List <string>()
                {
                    "killall -9 computer-vision"
                }, expactString);
            }));
        }
        private Action <ReplaySelectDialogModel> OnReplaySelectDialogClose(CustomDialog customDialog)
        {
            return(obj =>
            {
                Parent.SyncContext.Send(d =>
                {
                    Parent.DialogCoordinator.HideMetroDialogAsync(Parent, customDialog);
                }, null);

                Parent.SyncContext.Post(d =>
                {
                    Task.Factory.StartNew(async() =>
                    {
                        ReplaySelectDialogModel replaySelectDialogModel = obj as ReplaySelectDialogModel;
                        if (replaySelectDialogModel.SelectedFile != null)
                        {
                            RemoteDataStore remoteDataStore = new RemoteDataStore(Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.IpAddress, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Password);

                            string guid = Guid.NewGuid().ToString();
                            string localFile = "";
                            string remoteFile = "";
                            string remoteFolder = string.Format(@"/var/tmp/firefly/{0}", guid);
                            string expactString = string.Format("{0}@{1}:.{{0,}}[$]", Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Hostname);

                            Parent.SyncContext.Send(c =>
                            {
                                localFile = replaySelectDialogModel.SelectedFile.FullPath;
                                remoteFile = string.Format(@"/var/tmp/firefly/{0}/{1}", guid, Path.GetFileName(localFile));
                            }, null);

                            var controller = await Parent.DialogCoordinator.ShowProgressAsync(Parent, "Please wait...", "Calculating calibration parameter now!", settings: Parent.MetroDialogSettings);
                            controller.SetIndeterminate();
                            controller.SetCancelable(false);

                            remoteDataStore.ExecuteCommands(new List <string>()
                            {
                                string.Format("mkdir -p {0}", remoteFolder)
                            }, expactString);

                            remoteDataStore.UploadFile(remoteFile, localFile);

                            remoteDataStore.UploadContentToFile(string.Format(@"{0}/target.yaml", remoteFolder), YamlTranslator.ConvertToYaml(new CalibrationTarget()
                            {
                                TargetType = CalibrationTargetType.Aprilgrid,
                                TagSize = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagSize,
                                TagSpacing = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagSpacingFactor,
                                TagCols = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagsX,
                                TagRows = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagsY
                            }));

                            remoteDataStore.ExecuteCommands(new List <string>()
                            {
                                string.Format(@"cd {0}", remoteFolder),
                                string.Format(@"unzip {0} -d {1}", Path.GetFileName(localFile), Path.GetFileNameWithoutExtension(localFile)),
                                @"source ~/kalibr_workspace/devel/setup.bash",
                                string.Format(@"kalibr_bagcreater --folder {0} --output-bag {0}.bag", Path.GetFileNameWithoutExtension(localFile)),
                                string.Format(@"kalibr_calibrate_cameras --bag {0}.bag --topics /cam0/image_raw --models pinhole-equi --target target.yaml --dont-show-report", Path.GetFileNameWithoutExtension(localFile)),
                                string.Format("pdftoppm report-cam-{0}.pdf result -png", Path.GetFileNameWithoutExtension(localFile))
                            }, expactString);

                            CameraChain cameraChain = YamlTranslator.ConvertFromYaml <CameraChain>(remoteDataStore.DownloadFileToMemory(string.Format("{0}/camchain-{1}.yaml", remoteFolder, Path.GetFileNameWithoutExtension(localFile))));

                            string outputPath = Path.Combine(Path.GetTempPath(), "firefly", guid);

                            if (!Directory.Exists(outputPath))
                            {
                                Directory.CreateDirectory(outputPath);
                            }

                            foreach (string file in remoteDataStore.GetAllFileNames(remoteFolder))
                            {
                                if (!file.Contains(".bag") && !file.Contains(".ffc"))
                                {
                                    remoteDataStore.DownloadFile(string.Format("{0}/{1}", remoteFolder, file), Path.Combine(outputPath, file));
                                }
                            }

                            ShowResults(outputPath, cameraChain);

                            remoteDataStore.ExecuteCommands(new List <string>()
                            {
                                string.Format(@"rm -r {0}", remoteFolder)
                            }, expactString);

                            await controller.CloseAsync();
                        }
                    });
                }, null);
            });
        }
        private Action <ReplaySelectDialogModel> OnReplaySelectDialogClose(CustomDialog customDialog)
        {
            return(obj =>
            {
                Parent.SyncContext.Send(d =>
                {
                    Parent.DialogCoordinator.HideMetroDialogAsync(Parent, customDialog);
                }, null);

                Parent.SyncContext.Post(d =>
                {
                    Task.Factory.StartNew(async() =>
                    {
                        ReplaySelectDialogModel replaySelectDialogModel = obj as ReplaySelectDialogModel;
                        if (replaySelectDialogModel.SelectedFile != null)
                        {
                            RemoteDataStore remoteDataStore = new RemoteDataStore(Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.IpAddress, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Password);

                            string guid = Guid.NewGuid().ToString();
                            string localFile = "";
                            string remoteFile = "";
                            string remoteFolder = string.Format(@"/var/tmp/firefly/{0}", guid);
                            string expactString = string.Format("{0}@{1}:.{{0,}}[$]", Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Hostname);

                            string imuModel = Imu.ConvertImuModelToString(Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.ImuModel);

                            Parent.SyncContext.Send(c =>
                            {
                                localFile = replaySelectDialogModel.SelectedFile.FullPath;
                                remoteFile = string.Format(@"/var/tmp/firefly/{0}/{1}", guid, Path.GetFileName(localFile));
                            }, null);

                            var controller = await Parent.DialogCoordinator.ShowProgressAsync(Parent, "Please wait...", "Calculating calibration parameter now!", settings: Parent.MetroDialogSettings);
                            controller.SetIndeterminate();
                            controller.SetCancelable(false);

                            remoteDataStore.ExecuteCommands(new List <string>()
                            {
                                string.Format("mkdir -p {0}", remoteFolder)
                            }, expactString);

                            remoteDataStore.UploadFile(remoteFile, localFile);

                            remoteDataStore.UploadContentToFile(string.Format(@"{0}/target.yaml", remoteFolder), YamlTranslator.ConvertToYaml(new CalibrationTarget()
                            {
                                TargetType = CalibrationTargetType.Aprilgrid,
                                TagSize = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagSize,
                                TagSpacing = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagSpacingFactor,
                                TagCols = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagsX,
                                TagRows = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagsY
                            }));

                            remoteDataStore.UploadContentToFile(string.Format(@"{0}/imu.yaml", remoteFolder), YamlTranslator.ConvertToYaml(new Imu()
                            {
                                RosTopic = "/imu0",
                                UpdateRate = Parent.SettingContainer.Settings.ImuSettings.UpdateRate,
                                AccelerometerNoiseDensity = Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.AccelerometerNoiseDensity * Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.AccelerometerNoiseDensitySafetyScale,
                                AccelerometerRandomWalk = Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.AccelerometerRandomWalk * Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.AccelerometerRandomWalkSafetyScale,
                                GyroscopeNoiseDensity = Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.GyroscopeNoiseDensity * Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.GyroscopeNoiseDensitySafetyScale,
                                GyroscopeRandomWalk = Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.GyroscopeRandomWalk * Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.GyroscopeRandomWalkSafetyScale
                            }));

                            remoteDataStore.UploadContentToFile(string.Format(@"{0}/cam.yaml", remoteFolder), YamlTranslator.ConvertToYaml(new CameraChain()
                            {
                                Cam0 = new Data.Storage.Model.Camera()
                                {
                                    CameraModel = CameraModel.Pinhole,
                                    DistortionModel = DistortionModel.Equidistant,
                                    DistortionCoefficients = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.DistCoeffs.ToArray(),
                                    Fx = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Fx,
                                    Fy = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Fy,
                                    Cx = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Cx,
                                    Cy = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Cy,
                                    RosTopic = "/cam0/image_raw",
                                    Height = Parent.SettingContainer.Settings.CameraSettings.Height,
                                    Width = Parent.SettingContainer.Settings.CameraSettings.Width
                                }
                            }));

                            string options = string.Format(CultureInfo.InvariantCulture, "--dont-show-report --reprojection-sigma {0} {1}", Parent.SettingContainer.Settings.CalibrationSettings.ExtrinsicCalibrationSettings.ReprojectionSigma, Parent.SettingContainer.Settings.CalibrationSettings.ExtrinsicCalibrationSettings.TimeCalibration ? "--time-calibration" : "");

                            remoteDataStore.ExecuteCommands(new List <string>()
                            {
                                string.Format(@"cd {0}", remoteFolder),
                                string.Format(@"unzip {0} -d {1}", Path.GetFileName(localFile), Path.GetFileNameWithoutExtension(localFile)),
                                @"source ~/kalibr_workspace/devel/setup.bash",
                                string.Format(@"kalibr_bagcreater --folder {0} --output-bag {0}.bag", Path.GetFileNameWithoutExtension(localFile)),
                                string.Format(@"kalibr_calibrate_imu_camera --bag {0}.bag --cams cam.yaml --imu imu.yaml --imu-models {1} --target target.yaml {2}", Path.GetFileNameWithoutExtension(localFile), imuModel, options),
                                string.Format("pdftoppm report-imucam-{0}.pdf result -png", Path.GetFileNameWithoutExtension(localFile))
                            }, expactString);

                            CameraChain cameraChain = YamlTranslator.ConvertFromYaml <CameraChain>(remoteDataStore.DownloadFileToMemory(string.Format("{0}/camchain-imucam-{1}.yaml", remoteFolder, Path.GetFileNameWithoutExtension(localFile))));
                            ImuCain imuChain = YamlTranslator.ConvertFromYaml <ImuCain>(remoteDataStore.DownloadFileToMemory(string.Format("{0}/imu-{1}.yaml", remoteFolder, Path.GetFileNameWithoutExtension(localFile))));

                            List <string> availablePlots = new List <string>();

                            foreach (string file in remoteDataStore.GetAllFileNames(remoteFolder))
                            {
                                if (file.Contains(".csv"))
                                {
                                    availablePlots.Add(file.Replace(string.Format("data-imucam-{0}.", Path.GetFileNameWithoutExtension(localFile)), "").Replace(".csv", ""));
                                }
                            }

                            Dictionary <string, string> plotDataImu = new Dictionary <string, string>();

                            foreach (string plot in availablePlots)
                            {
                                plotDataImu.Add(plot, remoteDataStore.DownloadFileToMemory(string.Format("{0}/data-imucam-{1}.{2}.csv", remoteFolder, Path.GetFileNameWithoutExtension(localFile), plot)));
                            }

                            string outputPath = Path.Combine(Path.GetTempPath(), "firefly", guid);

                            if (!Directory.Exists(outputPath))
                            {
                                Directory.CreateDirectory(outputPath);
                            }

                            foreach (string file in remoteDataStore.GetAllFileNames(remoteFolder))
                            {
                                if (!file.Contains(".bag") && !file.Contains(".ffc"))
                                {
                                    remoteDataStore.DownloadFile(string.Format("{0}/{1}", remoteFolder, file), Path.Combine(outputPath, file));
                                }
                            }

                            CsvToMatlabWritter csvToMatlabWritter = new CsvToMatlabWritter(Path.Combine(outputPath, "plot-data.mat"));
                            csvToMatlabWritter.Write(plotDataImu, "ExtrinsicCalibrationData");
                            csvToMatlabWritter.Save();

                            ShowResults(outputPath, cameraChain, imuChain);

                            remoteDataStore.ExecuteCommands(new List <string>()
                            {
                                string.Format(@"rm -r {0}", remoteFolder)
                            }, expactString);

                            await controller.CloseAsync();
                        }
                    });
                }, null);
            });
        }
Exemplo n.º 5
0
        private Task DoPrintBoard(object o)
        {
            return(Task.Factory.StartNew(() =>
            {
                Parent.SyncContext.Post(c =>
                {
                    if (o is string && !string.IsNullOrEmpty(o as string))
                    {
                        switch (o as string)
                        {
                        case "AprilGrid":
                            {
                                CustomDialog customDialog = new CustomDialog()
                                {
                                    Title = "Board Properties"
                                };

                                var dataContext = new PrintAprilGridDialogModel(obj =>
                                {
                                    Parent.SyncContext.Post(d =>
                                    {
                                        Parent.DialogCoordinator.HideMetroDialogAsync(Parent, customDialog);

                                        System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
                                        saveFileDialog.Filter = "Portable Document Format (*.pdf) | *.pdf";

                                        if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                        {
                                            RemoteDataStore remoteDataStore = new RemoteDataStore(Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.IpAddress, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Password);

                                            string expactString = string.Format("{0}@{1}:.{{0,}}[$]", Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Hostname);
                                            string guid = Guid.NewGuid().ToString();
                                            string remoteFolder = string.Format(@"/var/tmp/firefly/{0}", guid);

                                            remoteDataStore.ExecuteCommands(new List <string>()
                                            {
                                                string.Format("mkdir -p {0}", remoteFolder)
                                            }, expactString);

                                            string res = remoteDataStore.ExecuteCommands(new List <string>()
                                            {
                                                string.Format(@"cd {0}", remoteFolder),
                                                @"source ~/kalibr_workspace/devel/setup.bash",
                                                string.Format(CultureInfo.InvariantCulture, @"kalibr_create_target_pdf --type apriltag --nx {2} --ny {3} --tsize {0} --tspace {1} {4}/{5}", obj.TagSize, obj.TagSpacingFactor, obj.TagsX, obj.TagsY, remoteFolder, Path.GetFileName(saveFileDialog.FileName)),
                                            }, expactString);

                                            remoteDataStore.DownloadFile(string.Format("{0}/{1}", remoteFolder, Path.GetFileName(saveFileDialog.FileName)), saveFileDialog.FileName);

                                            remoteDataStore.ExecuteCommands(new List <string>()
                                            {
                                                string.Format(@"rm -r {0}", remoteFolder)
                                            }, expactString);
                                        }
                                    }, null);
                                },
                                                                                obj =>
                                {
                                    Parent.SyncContext.Post(d =>
                                    {
                                        Parent.DialogCoordinator.HideMetroDialogAsync(Parent, customDialog);
                                    }, null);
                                });

                                dataContext.TagSize = TagSize;
                                dataContext.TagSpacingFactor = TagSpacingFactor;
                                dataContext.TagsX = TagsX;
                                dataContext.TagsY = TagsY;

                                customDialog.Content = new PrintAprilGridBoardDialog {
                                    DataContext = dataContext
                                };

                                Parent.DialogCoordinator.ShowMetroDialogAsync(Parent, customDialog);
                            }
                            break;

                        case "ChAruco":
                            {
                                CustomDialog customDialog = new CustomDialog()
                                {
                                    Title = "Board Properties"
                                };

                                var dataContext = new PrintCharucoBoardDialogModel(obj =>
                                {
                                    Parent.SyncContext.Post(d =>
                                    {
                                        Parent.DialogCoordinator.HideMetroDialogAsync(Parent, customDialog);

                                        System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
                                        saveFileDialog.Filter = "Portable Network Graphics (*.png) | *.png";

                                        if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                        {
                                            CvInvoke.Imwrite(saveFileDialog.FileName, obj.Image.CvImage, new KeyValuePair <Emgu.CV.CvEnum.ImwriteFlags, int>()
                                            {
                                            });
                                        }
                                    }, null);
                                },
                                                                                   obj =>
                                {
                                    Parent.SyncContext.Post(d =>
                                    {
                                        Parent.DialogCoordinator.HideMetroDialogAsync(Parent, customDialog);
                                    }, null);
                                });

                                dataContext.SquareLength = (int)Math.Round(SquareLength / 0.0254 * 100);
                                dataContext.MarkerLength = (int)Math.Round(MarkerLength / 0.0254 * 100);
                                dataContext.Dictionary = Dictionary;
                                dataContext.SquaresX = SquaresX;
                                dataContext.SquaresY = SquaresY;

                                customDialog.Content = new PrintCharucoBoardDialog {
                                    DataContext = dataContext
                                };

                                Parent.DialogCoordinator.ShowMetroDialogAsync(Parent, customDialog);
                            }
                            break;

                        default:
                            break;
                        }
                    }
                }, null);
            }));
        }
Exemplo n.º 6
0
        public void Refresh()
        {
            if (!IsReplaying)
            {
                FilesForReplay.Clear();
                if (Parent.SettingContainer.Settings.GeneralSettings.FileLocations != null && Parent.SettingContainer.Settings.GeneralSettings.FileLocations.Count > 0)
                {
                    foreach (FileLocation loc in Parent.SettingContainer.Settings.GeneralSettings.FileLocations)
                    {
                        Tuple <FileLocation, List <ReplayFile> > tuple = new Tuple <FileLocation, List <ReplayFile> >(loc, new List <ReplayFile>());
                        FilesForReplay.Add(tuple);
                        if (!string.IsNullOrEmpty(loc.Path))
                        {
                            try
                            {
                                string fullPath = Path.GetFullPath(Environment.ExpandEnvironmentVariables(loc.Path));
                                if (Directory.Exists(fullPath))
                                {
                                    foreach (string file in Directory.GetFiles(fullPath, "*.ffc"))
                                    {
                                        ReplayFile replayFile = new ReplayFile()
                                        {
                                            Name = Path.GetFileNameWithoutExtension(file), FullPath = file
                                        };
                                        tuple.Item2.Add(replayFile);
                                        Task.Run(() =>
                                        {
                                            string notes = RawDataReader.ReadNotes(file);
                                            Parent.SyncContext.Post(c =>
                                            {
                                                replayFile.Notes = notes;
                                            }, null);
                                        });
                                    }
                                }
                            }
                            catch (Exception) { }
                        }
                    }
                }
                if (Parent.ConnectivityState == LinkUp.Raw.LinkUpConnectivityState.Connected)
                {
                    RemoteDataStore remoteDataStore = new RemoteDataStore(Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.IpAddress, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Password);

                    List <string> files = remoteDataStore.GetAllFileNames("/home/up/data");
                    if (files.Count > 0)
                    {
                        Tuple <FileLocation, List <ReplayFile> > tuple = new Tuple <FileLocation, List <ReplayFile> >(new FileLocation()
                        {
                            Name = "Remote", Path = "/home/up/data", IsRemote = true
                        }, new List <ReplayFile>());
                        FilesForReplay.Add(tuple);

                        foreach (string filename in files)
                        {
                            if (Path.GetExtension(filename) == ".csv")
                            {
                                tuple.Item2.Add(new ReplayFile()
                                {
                                    Name = filename, IsRemote = true, FullPath = string.Format("{0}/{1}", tuple.Item1.Path, filename)
                                });
                            }
                        }
                    }
                }
            }
        }