private Task DoLoadFromFile(object o) { return(Task.Factory.StartNew(() => { Parent.SyncContext.Post(c => { CustomDialog customDialog = new CustomDialog() { Title = "Select calibration file" }; var dataContext = new ReplaySelectDialogModel(obj => { Parent.SyncContext.Post(d => { Task.Factory.StartNew(async() => { ReplaySelectDialogModel replaySelectDialogModel = obj as ReplaySelectDialogModel; if (replaySelectDialogModel.SelectedFile != null) { MetroDialogSettings settings = new MetroDialogSettings() { AnimateShow = false, AnimateHide = false }; var controller = await Parent.DialogCoordinator.ShowProgressAsync(Parent, "Please wait...", "Loading calibration images!", settings: Parent.MetroDialogSettings); controller.SetCancelable(false); controller.SetIndeterminate(); string file = null; string outputPath = null; Parent.SyncContext.Send(async d2 => { file = replaySelectDialogModel.SelectedFile.FullPath; outputPath = Path.Combine(Path.GetTempPath(), "firefly", Guid.NewGuid().ToString()); }, null); RawDataReader reader = new RawDataReader(file, RawReaderMode.Camera0); reader.Open(); MemoryImageExporter exporter = new MemoryImageExporter(); exporter.AddFromReader(reader, delegate(double percent) { double value = percent; value = value > 1 ? 1 : value; controller.SetProgress(value); }); reader.Close(); Parent.SyncContext.Post(d2 => { int i = 0; int count = exporter.Images.Count; foreach (Mat m in exporter.Images) { //double value = 0.5 + i / count * 0.5; //value = value > 1 ? 1 : value; //controller.SetProgress(value); if (i++ % 20 == 0) { int squaresX = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.SquaresX; int squaresY = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.SquaresY; float squareLength = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.SquareLength; float markerLength = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.MarkerLength; PredefinedDictionaryName dictionary = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.Dictionary; ChArUcoImageContainer cauic = new ChArUcoImageContainer(squaresX, squaresY, squareLength, markerLength, dictionary); CvImageContainer container = new CvImageContainer(); container.CvImage = m; cauic.OriginalImage = container; Images.Add(cauic); } } controller.CloseAsync(); }, null); } }, TaskCreationOptions.LongRunning); Parent.DialogCoordinator.HideMetroDialogAsync(Parent, customDialog); }, null); }); Parent.ReplayViewModel.Refresh(); dataContext.FilesForReplay.AddRange(Parent.ReplayViewModel.FilesForReplay.Where(x => !x.Item1.IsRemote)); customDialog.Content = new ReplaySelectDialog { DataContext = dataContext }; Parent.DialogCoordinator.ShowMetroDialogAsync(Parent, customDialog); }, null); })); }
public void ReplayOffline(RawDataReader reader, Action <TimeSpan> updateTime, Action onClose, Func <bool> isPaused, Func <bool> isStopped) { _Tasks.Add(Task.Factory.StartNew(() => { ProxyMode = IOProxyMode.Offline; Stopwatch watch = new Stopwatch(); watch.Start(); int nextTimeUpdate = 1000; long startTime = -1; int currentTime = 0; while (reader.HasNext()) { while (isPaused()) { watch.Stop(); Thread.Sleep(500); watch.Start(); } if (isStopped()) { break; } Tuple <long, List <Tuple <RawReaderMode, object> > > res = reader.Next(); if (startTime == -1) { startTime = res.Item1; } ImuEventData imuEventData = null; CameraEventData cameraEventData = null; int rawSize = 0; byte[] rawImage = null; byte[] rawImu = null; double exposureTime = 0.0; foreach (Tuple <RawReaderMode, object> val in res.Item2) { if (val.Item1 == RawReaderMode.Imu0) { imuEventData = ImuEventData.Parse(res.Item1, (Tuple <double, double, double, double, double, double>)val.Item2, res.Item2.Any(c => c.Item1 == RawReaderMode.Camera0)); rawSize += imuEventData.RawSize; rawImu = imuEventData.GetRaw(_SettingContainer.Settings.ImuSettings.GyroscopeScale, _SettingContainer.Settings.ImuSettings.AccelerometerScale, _SettingContainer.Settings.ImuSettings.TemperatureScale, _SettingContainer.Settings.ImuSettings.TemperatureOffset); } if (val.Item1 == RawReaderMode.Camera0) { cameraEventData = CameraEventData.Parse(((Tuple <double, byte[]>)val.Item2).Item2, 0, false, ((Tuple <double, byte[]>)val.Item2).Item1); rawSize += cameraEventData.RawSize; rawImage = ((Tuple <double, byte[]>)val.Item2).Item2; exposureTime = ((Tuple <double, byte[]>)val.Item2).Item1; } } if (ConnectivityState == LinkUpConnectivityState.Connected) { if (rawSize > 0) { byte[] data = new byte[rawSize]; Array.Copy(rawImu, data, rawImu.Length); if (rawImage != null) { Array.Copy(BitConverter.GetBytes(exposureTime), 0, data, imuEventData.RawSize, sizeof(double)); Array.Copy(rawImage, 0, data, imuEventData.RawSize + sizeof(double), rawImage.Length); } _ReplayDataSend.AsyncCall(data); } } else { _BackgroundQueue.Add(new Tuple <ImuEventData, CameraEventData>(imuEventData, cameraEventData)); } currentTime += reader.DeltaTimeMs; int sleep = (int)(currentTime - watch.ElapsedMilliseconds); if (sleep > reader.DeltaTimeMs) { Thread.Sleep(sleep); } if (res.Item1 / 1000 > nextTimeUpdate) { nextTimeUpdate += 1000; updateTime(reader.Length - TimeSpan.FromMilliseconds((res.Item1 - startTime) / (1000 * 1000))); } } reader.Close(); ProxyMode = IOProxyMode.Live; onClose(); }, TaskCreationOptions.LongRunning)); }
private Action <ReplaySelectDialogModel> OnReplaySelectDialogClose(bool response, 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) { MetroDialogSettings settings = new MetroDialogSettings() { AnimateShow = false, AnimateHide = false }; var controller = await Parent.DialogCoordinator.ShowProgressAsync(Parent, "Please wait...", "Photometric calibration!", settings: Parent.MetroDialogSettings); controller.SetCancelable(false); controller.SetIndeterminate(); string file = null; string outputPath = null; double fxO = 0.0; double fyO = 0.0; double cxO = 0.0; double cyO = 0.0; double fxN = 0.0; double fyN = 0.0; double cxN = 0.0; double cyN = 0.0; double k1 = 0.0; double k2 = 0.0; double k3 = 0.0; double k4 = 0.0; int width = 0; int height = 0; List <double> responseValues = new List <double>(); Parent.SyncContext.Send(d2 => { file = replaySelectDialogModel.SelectedFile.FullPath; outputPath = Path.Combine(Path.GetTempPath(), "firefly", Guid.NewGuid().ToString()); fxO = Parent.CameraViewModel.OrginalCameraMatrix.GetValue(0, 0); fyO = Parent.CameraViewModel.OrginalCameraMatrix.GetValue(1, 1); cxO = Parent.CameraViewModel.OrginalCameraMatrix.GetValue(0, 2); cyO = Parent.CameraViewModel.OrginalCameraMatrix.GetValue(1, 2); fxN = Parent.CameraViewModel.NewCameraMatrix.GetValue(0, 0); fyN = Parent.CameraViewModel.NewCameraMatrix.GetValue(1, 1); cxN = Parent.CameraViewModel.NewCameraMatrix.GetValue(0, 2); cyN = Parent.CameraViewModel.NewCameraMatrix.GetValue(1, 2); k1 = Parent.CameraViewModel.DistortionCoefficients.GetValue(0, 0); k2 = Parent.CameraViewModel.DistortionCoefficients.GetValue(0, 1); k3 = Parent.CameraViewModel.DistortionCoefficients.GetValue(0, 2); k4 = Parent.CameraViewModel.DistortionCoefficients.GetValue(0, 3); if (!response) { responseValues.AddRange(ResponseValues.Select(f => f.Y)); } width = Parent.CameraViewModel.ImageWidth; height = Parent.CameraViewModel.ImageHeight; }, null); RawDataReader reader = new RawDataReader(file, RawReaderMode.Camera0); reader.Open(); PhotometricCalibratrionExporter exporter = new PhotometricCalibratrionExporter(fxO, fyO, cxO, cyO, fxN, fyN, cxN, cyN, width, height, k1, k2, k3, k4, outputPath, response, responseValues); exporter.Open(); exporter.AddFromReader(reader, delegate(double percent) { double value = percent * 0.33; value = value > 1 ? 1 : value; controller.SetProgress(value); }); exporter.Close(); reader.Close(); Process p = new Process(); p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false; p.StartInfo.WorkingDirectory = outputPath; p.StartInfo.CreateNoWindow = true; p.EnableRaisingEvents = true; string options = ""; if (response) { p.StartInfo.FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Tools", "responseCalib.exe"); } else { p.StartInfo.FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Tools", "vignetteCalib.exe"); options = "facW=7 facH=7"; } p.StartInfo.Arguments = string.Format("{0}\\ -noGUI -showPercent {1}", outputPath, options); p.OutputDataReceived += new DataReceivedEventHandler((s, e) => { Debug.WriteLine(e.Data); if (!string.IsNullOrEmpty(e.Data)) { foreach (string line in e.Data.Split('\n')) { if (line.StartsWith("percent: ")) { double percent = double.Parse(line.Replace("percent: ", ""), CultureInfo.InvariantCulture); double value = 0.33 + percent * 0.66; value = value > 1 ? 1 : value; controller.SetProgress(value); } } } } ); p.Exited += new EventHandler((s, e) => { Parent.SyncContext.Post(async x => { if (response) { ParseResponseResult(outputPath); } else { ParseVignetteResult(outputPath); } Directory.Delete(outputPath, true); await controller.CloseAsync(); }, null); }); p.Start(); p.BeginOutputReadLine(); } }, TaskCreationOptions.LongRunning); }, null); }); }
private Task DoExportVideo(object o) { return(Task.Factory.StartNew(async() => { ReplayFile file = o as ReplayFile; RawDataReader reader = null; string fullPath = null; bool isRemote = false; System.Windows.Forms.OpenFileDialog openFileDialog = null; System.Windows.Forms.SaveFileDialog saveFileDialog = null; bool open = false; bool save = false; CameraViewModel cvm = null; Parent.SyncContext.Send(c => { cvm = Parent.CameraViewModel; fullPath = file.FullPath; isRemote = file.IsRemote; openFileDialog = new System.Windows.Forms.OpenFileDialog(); openFileDialog.Filter = "Matlab (*.mat) | *.mat"; open = openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK; saveFileDialog = new System.Windows.Forms.SaveFileDialog(); saveFileDialog.Filter = "Video (*.mp4) | *.mp4"; save = saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK; }, null); if (open && save && !isRemote) { MetroDialogSettings settings = new MetroDialogSettings() { AnimateShow = false, AnimateHide = false }; var controller = await Parent.DialogCoordinator.ShowProgressAsync(Parent, "Please wait...", "Export video file!", settings: Parent.MetroDialogSettings); controller.SetCancelable(false); reader = new RawDataReader(fullPath, RawReaderMode.Imu0 | RawReaderMode.Camera0, null, false); controller.SetIndeterminate(); reader.Open(); VIMatlabImporter matlabImporter = new VIMatlabImporter(openFileDialog.FileName); matlabImporter.Open(); VIVideoRenderer renderer = new VIVideoRenderer(saveFileDialog.FileName, 1920, 1080, Parent.SyncContext, cvm.OrginalCameraMatrix, cvm.CenteredCameraMatrix, cvm.DistortionCoefficients); renderer.Open(); renderer.Render(matlabImporter, reader, delegate(double percent) { controller.SetProgress(percent); }); renderer.Close(); matlabImporter.Close(); reader.Close(); await controller.CloseAsync(); } })); }