public void SiteMapPath_ChangeProperties () { PokerSiteMapPath p = new PokerSiteMapPath (); p.ShowToolTips = false; Assert.IsFalse (p.ShowToolTips, "ShowToolTips"); Style currentNodeStyle = new Style (); p.CurrentNodeStyle.ForeColor = Color.AliceBlue; Assert.AreEqual (Color.AliceBlue, p.CurrentNodeStyle.ForeColor, "CurrentNodeStyle"); Style NodeStyle = new Style (); p.NodeStyle.BackColor = Color.Aqua; Assert.AreEqual (Color.Aqua, p.NodeStyle.BackColor, "NodeStyle"); p.PathDirection = PathDirection.CurrentToRoot; Assert.AreEqual (PathDirection.CurrentToRoot, p.PathDirection, "PathDirection"); p.PathSeparator = " - "; Assert.AreEqual (" - ", p.PathSeparator, "PathSeparator"); Style RootNodeStyle = new Style (); p.RootNodeStyle.BackColor = Color.Red; Assert.IsFalse (p.RootNodeStyle.IsEmpty, "RootNodeStyle#1"); Assert.AreEqual (Color.Red, p.RootNodeStyle.BackColor, "RootNodeStyle#2"); p.ParentLevelsDisplayed = 2; Assert.AreEqual (2, p.ParentLevelsDisplayed, "ParentLevelsDisplayed"); p.RenderCurrentNodeAsLink = true; Assert.IsTrue (p.RenderCurrentNodeAsLink, "RenderCurrentNodeAsLink"); p.SiteMapProvider = "test"; Assert.AreEqual ("test", p.SiteMapProvider, "SiteMapProvider"); p.SkipLinkText = "test"; Assert.AreEqual ("test", p.SkipLinkText, "Skip Navigation Links"); //programmatically create template MyWebControl.Image myImage = new MyWebControl.Image (); myImage.ImageUrl = "myimage.jpg"; ImageTemplate rootNodeImageTemplate = new ImageTemplate (); rootNodeImageTemplate.MyImage = myImage; // end create template image p.RootNodeTemplate = rootNodeImageTemplate; Assert.IsNotNull (p.RootNodeTemplate, "RootNodeTemplate"); Assert.AreEqual (rootNodeImageTemplate, p.RootNodeTemplate, "RootNodeTemplate"); p.NodeTemplate = rootNodeImageTemplate; Assert.IsNotNull (p.NodeTemplate, "NodeTemplate"); Assert.AreEqual (rootNodeImageTemplate, p.NodeTemplate, "NodeTemplate"); p.CurrentNodeTemplate = rootNodeImageTemplate; Assert.IsNotNull (p.CurrentNodeTemplate, "RootNodeTemplate"); Assert.AreEqual (rootNodeImageTemplate, p.CurrentNodeTemplate, "RootNodeTemplate"); }
public void Menu_ChangeDefaultProperties () { PokerMenu p = new PokerMenu (); Button B = new Button (); p.Controls.Add (B); Assert.AreEqual (1,p.Controls.Count, "ControlsCollection"); MenuItemBinding M = new MenuItemBinding (); M.DataMember = "test"; M.Depth = 0; M.TextField = "title"; M.NavigateUrl="url"; Object C = p.DataBindings; Assert.AreEqual (0, p.DataBindings.Count, "DataBindings#1"); ((MenuItemBindingCollection)C).Add (M); Assert.AreEqual (1,p.DataBindings.Count,"DataBindings#2"); p.DisappearAfter = 100; Assert.AreEqual (100, p.DisappearAfter, "DisappearAfter"); p.DynamicBottomSeparatorImageUrl = "test.aspx"; Assert.AreEqual ("test.aspx", p.DynamicBottomSeparatorImageUrl, "DynamicBottomSeparatorImageUrl"); p.DynamicEnableDefaultPopOutImage = false; Assert.AreEqual (false,p.DynamicEnableDefaultPopOutImage, "DynamicEnableDefaultPopOutImage"); p.DynamicHorizontalOffset = 10; Assert.AreEqual (10, p.DynamicHorizontalOffset, "DynamicHorizontalOffset"); p.DynamicHoverStyle.BackColor = Color.Red; Assert.AreEqual (Color.Red ,p.DynamicHoverStyle.BackColor, "DynamicHoverStyle"); p.DynamicItemFormatString = "Mytest"; Assert.AreEqual ("Mytest", p.DynamicItemFormatString, "DynamicItemFormatString"); MyWebControl.Image myImage = new MyWebControl.Image (); myImage.ImageUrl = "myimage.jpg"; ImageTemplate Template = new ImageTemplate (); Template.MyImage = myImage; // end create template image p.DynamicItemTemplate = Template; Assert.IsNotNull (p.DynamicItemTemplate, "RootNodeTemplate"); Assert.AreEqual (typeof (ImageTemplate), p.DynamicItemTemplate.GetType (), "RootNodeTemplate#1"); p.DynamicMenuItemStyle.BackColor = Color.Red; Assert.AreEqual (Color.Red, p.DynamicMenuItemStyle.BackColor, "DynamicMenuItemStyle"); p.DynamicMenuStyle.BackColor = Color.Red; Assert.AreEqual (Color.Red,p.DynamicMenuStyle.BackColor, "DynamicMenuStyle"); p.DynamicPopOutImageUrl = "test"; Assert.AreEqual ("test", p.DynamicPopOutImageUrl, "DynamicPopOutImageUrl"); p.DynamicSelectedStyle.BackColor = Color.Red; Assert.AreEqual (Color.Red,p.DynamicSelectedStyle.BackColor, "DynamicSelectedStyle"); p.DynamicTopSeparatorImageUrl = "test"; Assert.AreEqual ("test", p.DynamicTopSeparatorImageUrl, "DynamicTopSeparatorImageUrl"); p.DynamicVerticalOffset = 10; Assert.AreEqual (10, p.DynamicVerticalOffset, "DynamicVerticalOffset"); MenuItem I = new MenuItem (); I.NavigateUrl = "default.aspx"; I.Text = "MyText"; I.ToolTip = "Test"; p.Items.Add (I); Assert.AreEqual (1, p.Items.Count, "Items"); p.ItemWrap = true; Assert.AreEqual (true, p.ItemWrap, "ItemWrap"); MenuItemStyle S = new MenuItemStyle (); S.BackColor = Color.Red; p.LevelSelectedStyles.Add (S); Assert.AreEqual (1,p.LevelSelectedStyles.Count , "LevelSelectedStyles#1"); Assert.AreEqual (true, p.LevelSelectedStyles.Contains (S), "LevelSelectedStyles#2"); SubMenuStyle SM = new SubMenuStyle (); SM.BackColor = Color.Red; p.LevelSubMenuStyles.Add (SM); Assert.AreEqual(1, p.LevelSubMenuStyles.Count, "LevelSubMenuStyles#1"); Assert.AreEqual (true, p.LevelSubMenuStyles.Contains (SM), "LevelSubMenuStyles#2"); p.MaximumDynamicDisplayLevels = 5; Assert.AreEqual (5, p.MaximumDynamicDisplayLevels, "MaximumDynamicDisplayLevels"); p.Orientation = Orientation.Horizontal; Assert.AreEqual (Orientation.Horizontal, p.Orientation, "Orientation"); p.PathSeparator = 'A'; Assert.AreEqual ('A', p.PathSeparator, "PathSeparator"); p.ScrollDownImageUrl = "test"; Assert.AreEqual ("test", p.ScrollDownImageUrl, "ScrollDownImageUrl"); p.ScrollDownText = "test"; Assert.AreEqual ("test", p.ScrollDownText, "ScrollDownText"); // This properties will be checked in events part of tests // Assert.AreEqual (0, p.SelectedItem, "p.SelectedItem"); // Assert.AreEqual (string.Empty, p.SelectedValue, "SelectedValue"); p.SkipLinkText = "test"; Assert.AreEqual ("test", p.SkipLinkText, "SkipLinkText"); p.StaticBottomSeparatorImageUrl = "test"; Assert.AreEqual ("test", p.StaticBottomSeparatorImageUrl, "StaticBottomSeparatorImageUrl"); p.StaticDisplayLevels = 2; Assert.AreEqual (2, p.StaticDisplayLevels, "StaticDisplayLevels"); p.StaticEnableDefaultPopOutImage = false; Assert.AreEqual (false, p.StaticEnableDefaultPopOutImage, "StaticEnableDefaultPopOutImage"); p.StaticHoverStyle.BackColor = Color.Red; Assert.AreEqual(Color.Red, p.StaticHoverStyle.BackColor, "StaticHoverStyle"); p.StaticItemFormatString = "test"; Assert.AreEqual ("test", p.StaticItemFormatString, "StaticItemFormatString"); p.StaticItemTemplate = Template; Assert.IsNotNull (p.StaticItemTemplate, "StaticItemTemplate"); p.StaticMenuItemStyle.BackColor = Color.Red; Assert.AreEqual(Color.Red,p.StaticMenuItemStyle.BackColor, "StaticMenuItemStyle"); p.StaticMenuStyle.BackColor = Color.Red; Assert.AreEqual (Color.Red,p.StaticMenuStyle.BackColor, "StaticMenuStyle"); p.StaticPopOutImageUrl = "test"; Assert.AreEqual ("test", p.StaticPopOutImageUrl, "StaticPopOutImageUrl"); p.StaticSelectedStyle.BackColor = Color.Red; Assert.AreEqual(Color.Red,p.StaticSelectedStyle.BackColor, "StaticSelectedStyle"); p.StaticSubMenuIndent = 20; Assert.AreEqual (Unit.Pixel (20), p.StaticSubMenuIndent, "StaticSubMenuIndent"); p.StaticTopSeparatorImageUrl = "test"; Assert.AreEqual ("test", p.StaticTopSeparatorImageUrl, "StaticTopSeparatorImageUrl"); p.Target = "test"; Assert.AreEqual ("test", p.Target, "Target"); }
public LocationAndGestureDataCollectionTool() { InitializeComponent(); Location = Properties.Settings.Default.MainLocation; Size = Properties.Settings.Default.MainSize; captureSound = new SoundPlayer("sounds\\camera_capture.wav"); beepSound = new SoundPlayer("sounds\\camera_beep.wav"); chimeSound = new SoundPlayer("sounds\\endTask.wav"); speech.Rate = 2; captureSound.LoadAsync(); beepSound.LoadAsync(); chimeSound.LoadAsync(); Text = "Connecting to Camera and Sensors..."; trainingForm.RecordLocation += (string coarseLocation, string fineLocation) => { Logging.LogTrainingEvent("prepare_to_capture_location"); coarseLocationToTrain = coarseLocation; fineLocationToTrain = fineLocation; if (Properties.Settings.Default.EnableSoundEffects) { captureSound.Play(); } ImageTemplate newTemplate = AddTemplate(); Logging.LogTrainingEvent("Added template: " + newTemplate["coarse"] + " " + newTemplate["fine"]); }; trainingForm.RecordGesture += (string gesture) => { Logging.LogTrainingEvent("start_recording_gesture"); if (Properties.Settings.Default.EnableSoundEffects) { beepSound.Play(); } gestureToTrain = gesture; recordingGesture = true; }; trainingForm.AutoCaptureGesture += (string gesture) => { Logging.LogTrainingEvent("start_autocapture_gesture"); if (Properties.Settings.Default.EnableSoundEffects) { beepSound.Play(); } gestureToTrain = gesture; numAutoSamplesGathered = 0; autoTrainGesture = true; }; trainingForm.StopAutoCapturingGestures += () => { Logging.LogTrainingEvent("stop_autocapture_gesture"); autoTrainGesture = false; GestureRecognition.Train(); trainingForm.UpdateGestureList(); }; trainingForm.AutoCaptureLocation += (string coarseLocation, string fineLocation) => { coarseLocationToTrain = coarseLocation; fineLocationToTrain = fineLocation; Logging.LogTrainingEvent("start_autocapture_location"); if (Properties.Settings.Default.EnableSoundEffects) { beepSound.Play(); } autoTrainLocation = true; }; trainingForm.StopAutoCapturingLocation += () => { autoTrainLocation = false; Logging.LogTrainingEvent("stop_autocapture_location"); }; TouchSegmentation.TouchDownEvent += () => { touchDown = true; touchStart = DateTime.Now; Sensors.Instance.Brightness = 1; Logging.LogTouchEvent("touch_down"); // reset the location predictions if (!recentTouchUp) { while (gestureSensorReadings.Count > 0) { gestureSensorReadings.Take(); } foreach (Sensors.Reading reading in sensorReadingHistory) { gestureSensorReadings.Add(reading); } Logging.LogOtherEvent("smoothing_reset"); } Invoke(new MethodInvoker(delegate { TouchStatusLabel.Text = "Touch Down"; })); }; TouchSegmentation.TouchUpEvent += () => { touchDown = false; recentTouchUp = true; Invoke(new MethodInvoker(delegate { TouchStatusLabel.Text = "Touch Up"; })); Logging.LogTouchEvent("touch_up"); Task.Factory.StartNew(() => { Thread.Sleep(touchUpDelay); recentTouchUp = false; if (!touchDown) { Logging.LogOtherEvent("touch_up_timeout"); Sensors.Instance.Brightness = 0; if (autoTrainLocation) { autoTrainLocation = false; trainingForm.StopAutoCapture(); } if (trainingForm.Training) { return; } // process the gesture Gesture gesture = new Gesture(gestureSensorReadings.ToArray()); if (gestureSensorReadings.Count > 60) { GestureRecognition.PreprocessGesture(gesture); if (recordingGesture || autoTrainGesture) { //Monitor.Enter(recognitionLock); if (recordingGesture) { Logging.LogTrainingEvent("stop_recording_gesture"); } recordingGesture = false; if (Properties.Settings.Default.EnableSoundEffects) { captureSound.Play(); Logging.LogAudioEvent("capture", false); } DateTime start = DateTime.Now; gesture.ClassName = gestureToTrain; gesture.IsTrainingData = true; GestureRecognition.AddTrainingExample(gesture, gestureToTrain); // autosave the gesture in the background Task.Factory.StartNew(() => { if (!Directory.Exists(Path.Combine("savedProfiles", "autosave"))) { Directory.CreateDirectory(Path.Combine("savedProfiles", "autosave")); } int index = 1; while (File.Exists(Path.Combine("savedProfiles", "autosave", gestureToTrain + "_" + index + ".png"))) { index++; } gesture.Path = Path.Combine("savedProfiles", "autosave", gestureToTrain + "_" + index + ".png"); string json = JsonConvert.SerializeObject(gesture); Invoke(new MethodInvoker(delegate { File.WriteAllText(gesture.Path, json); })); }); Logging.LogTrainingEvent("Add gesture: " + gestureToTrain + ", path = " + gesture.Path); if (autoTrainGesture) { numAutoSamplesGathered++; if (numAutoSamplesGathered >= Properties.Settings.Default.NumAutoGestureSamples) { if (Properties.Settings.Default.EnableSoundEffects) { chimeSound.Play(); Logging.LogAudioEvent("chime", false); } Logging.LogTrainingEvent("stop_autocapture_gesture"); autoTrainGesture = false; } } start = DateTime.Now; if (!autoTrainGesture) { trainingForm.AddGesture(gesture); trainingForm.IsSaved = false; Debug.WriteLine("Updating List: " + (DateTime.Now - start).TotalMilliseconds + " ms"); } } } } }); }; Task.Factory.StartNew(() => { Camera.Instance.FrameAvailable += Camera_FrameAvailable; Camera.Instance.Error += (string error) => { Debug.WriteLine(error); Invoke(new MethodInvoker(delegate { Text = "Error: could not connect to camera"; })); }; Camera.Instance.Brightness = Properties.Settings.Default.CameraBrightness; Sensors.Instance.ReadingAvailable += Sensors_ReadingAvailable; Camera.Instance.Connect(); Logging.LogOtherEvent("camera_connected"); Sensors.Instance.Connect(Properties.Settings.Default.SensorPort); Logging.LogOtherEvent("sensors_connected"); Sensors.Instance.NumSensors = Properties.Settings.Default.SingleIMU ? 1 : 2; Invoke(new MethodInvoker(delegate { if (Properties.Settings.Default.TrainingVisible) { trainingToolStripMenuItem.PerformClick(); } if (Properties.Settings.Default.SettingsVisible) { settingsToolStripMenuItem.PerformClick(); } })); }); }
public void GridView_AssignedProperties () { PokerGridView g = new PokerGridView (); Assert.AreEqual (0, g.StateBag.Count, "ViewState.Count"); g.AllowPaging = true; Assert.AreEqual (true, g.AllowPaging, "AllowPaging"); g.AllowSorting = true; Assert.AreEqual (true, g.AllowSorting, "AllowSorting"); g.AutoGenerateColumns = false; Assert.AreEqual (false, g.AutoGenerateColumns, "AutoGenerateColumns"); g.AutoGenerateDeleteButton = true; Assert.AreEqual (true, g.AutoGenerateDeleteButton, "AutoGenerateDeleteButton"); g.AutoGenerateEditButton = true; Assert.AreEqual (true, g.AutoGenerateEditButton, "AutoGenerateEditButton"); g.AutoGenerateSelectButton = true; Assert.AreEqual (true, g.AutoGenerateSelectButton, "AutoGenerateSelectButton"); g.BackImageUrl = "test"; Assert.AreEqual ("test", g.BackImageUrl, "BackImageUrl"); g.Caption = "test"; Assert.AreEqual ("test", g.Caption, "Caption"); g.CaptionAlign = TableCaptionAlign.Left; Assert.AreEqual (TableCaptionAlign.Left, g.CaptionAlign, "CaptionAlign"); g.CellPadding = 0; Assert.AreEqual (0, g.CellPadding, "CellPadding"); g.CellSpacing = 1; Assert.AreEqual (1, g.CellSpacing, "CellSpacing"); // ToDo: The Columns property default value is tested by the DataControlFieldCollection and DataControlField tests string[] str = new String[1]; str[0] = "test"; g.DataKeyNames = str; Assert.AreEqual ("test", g.DataKeyNames[0], "DataKeyNames"); // ToDo: The DataKeys property default value is tested by the DataKeyArray and DataKey tests g.EditIndex = 0; Assert.AreEqual (0, g.EditIndex, "EditIndex"); // The EditRowStyle property default value is tested by the TableItemStyle test (already exists) // The EmptyDataRowStyle property default value is tested by the TableItemStyle test (already exists) MyWebControl.Image myImage = new MyWebControl.Image (); myImage.ImageUrl = "myimage.jpg"; ImageTemplate template = new ImageTemplate (); template.MyImage = myImage; // end create template image g.EmptyDataTemplate = template; Assert.IsNotNull (g.EmptyDataTemplate, "EmptyDataTemplate"); g.EmptyDataText = "test"; Assert.AreEqual ("test", g.EmptyDataText, "EmptyDataText"); g.EnableSortingAndPagingCallbacks = true; Assert.AreEqual (true, g.EnableSortingAndPagingCallbacks, "EnableSortingAndPagingCallbacks"); // The FooterStyle property default value is tested by the TableItemStyle test (already exists) g.GridLines = GridLines.Horizontal; Assert.AreEqual (GridLines.Horizontal, g.GridLines, "GridLines"); g.HorizontalAlign = HorizontalAlign.Justify; Assert.AreEqual (HorizontalAlign.Justify, g.HorizontalAlign, "HorizontalAlign"); g.PageIndex = 1; Assert.AreEqual (1, g.PageIndex, "PageIndex"); // ToDo: The PagerSettings property default value is tested by the PagerSettings test // The PagerStyle property default value is tested by the TableItemStyle test (already exists) g.PagerTemplate = template; Assert.IsNotNull (g.PagerTemplate, "PagerTemplate"); g.PageSize = 5; Assert.AreEqual (5, g.PageSize, "PageSize"); g.RowHeaderColumn = "test"; Assert.AreEqual ("test", g.RowHeaderColumn, "RowHeaderColumn"); // ToDo: The Rows property default value is tested by the GridViewRowCollection and GridViewRow test // The RowStyle property default value is tested by the TableItemStyle test (already exists) g.ShowFooter = true; Assert.AreEqual (true, g.ShowFooter, "ShowFooter"); g.ShowHeader = false; Assert.AreEqual (false, g.ShowHeader, "ShowHeader"); g.UseAccessibleHeader = false; Assert.AreEqual (false, g.UseAccessibleHeader, "UseAccessibleHeader"); }
void Camera_FrameAvailable(VideoFrame frame) { if (closing) { return; } ImageTemplate template = new ImageTemplate(frame.Clone()); FPS.Camera.Update(); Task.Factory.StartNew(() => { try { Logging.LogVideoFrame(frame.Image.Bitmap); float focus = 0; if (Monitor.TryEnter(preprocessingLock)) { ImageProcessing.ProcessTemplate(template, false); focus = ImageProcessing.ImageFocus(template); Logging.LogFrameProcessed(); currTemplate = template; FPS.Instance("processing").Update(); Monitor.Exit(preprocessingLock); } else { return; } string coarseLocation = "", fineLocation = ""; float coarseProbability = 0, fineProbability = 0; bool hasUpdate = false; string predictedCoarseLocation = "", predictedFineLocation = ""; if (touchDown && !trainingForm.Training && Monitor.TryEnter(recognitionLock)) { try { if (Localization.Instance.GetNumTrainingExamples() > 0) { Dictionary <string, float> coarseProbabilities = new Dictionary <string, float>(); predictedCoarseLocation = Localization.Instance.PredictCoarseLocation(currTemplate, out coarseProbabilities); coarseLocation = predictedCoarseLocation; if (!Properties.Settings.Default.CoarseOnly) { bool foundFeatureMatch = false; Dictionary <string, float> fineProbabilities = new Dictionary <string, float>(); predictedFineLocation = Localization.Instance.PredictFineLocation(currTemplate, out foundFeatureMatch, out fineProbabilities, true, false, false, coarseLocation); fineLocation = predictedFineLocation; } Logging.LogLocationEvent(predictedCoarseLocation + " " + predictedFineLocation); FPS.Instance("matching").Update(); hasUpdate = true; } if (autoTrainLocation && (coarseLocation != coarseLocationToTrain || fineLocation != fineLocationToTrain) && Monitor.TryEnter(trainingLock)) { trainingForm.Training = true; ImageTemplate newTemplate = CopyTemplate(currTemplate); Invoke(new MethodInvoker(delegate { TrainingLabel.Visible = true; })); if (Properties.Settings.Default.EnableSoundEffects) { captureSound.Play(); Logging.LogAudioEvent("capture", false); } AddTemplate(newTemplate); Logging.LogTrainingEvent("Added template: " + newTemplate["coarse"] + " " + newTemplate["fine"]); Thread.Sleep(100); //if (Properties.Settings.Default.EnableSoundEffects) { beepSound.Play(); Logging.LogAudioEvent("beep", false); } trainingForm.IsSaved = false; trainingForm.Training = false; Invoke(new MethodInvoker(delegate { TrainingLabel.Visible = false; })); Monitor.Exit(trainingLock); return; } } finally { Monitor.Exit(recognitionLock); } } //LBP.GetInstance(frame.Image.Size).GetHistogram(frame); Invoke(new MethodInvoker(delegate { Display.Image = frame.Image.Bitmap; if (hasUpdate) { CoarsePredictionLabel.Text = coarseLocation; FinePredictionLabel.Text = fineLocation; } Text = FPS.Camera.Average.ToString("0") + " fps camera / " + (Sensors.Instance.IsConnected ? FPS.Sensors.Average.ToString("0") + " fps sensors / " + FPS.Instance("processing").Average.ToString("0") + "/" + FPS.Instance("matching").Average.ToString("0") + " fps processing" : "Waiting for Sensors") /*+ " / focus = " + focus.ToString("0")*/; })); } catch { } }); }
ImageTemplate CopyTemplate(ImageTemplate template) { ImageTemplate newTemplate = template.Clone(); return(newTemplate); }
public OnBodyInputRealtimeExperiments() { InitializeComponent(); Location = Properties.Settings.Default.MainLocation; Size = Properties.Settings.Default.MainSize; captureSound = new SoundPlayer("sounds\\camera_capture.wav"); tickSound = new SoundPlayer("sounds\\tick.wav"); beepSound = new SoundPlayer("sounds\\camera_beep.wav"); phoneSound = new SoundPlayer("sounds\\phone.wav"); speech.Rate = 2; captureSound.LoadAsync(); tickSound.LoadAsync(); beepSound.LoadAsync(); timer.Elapsed += Timer_Elapsed; CountdownLabel.Parent = Display; numLocationPredictions = Properties.Settings.Default.PredictionSmoothing; Text = "Connecting to Camera and Sensors..."; // set up the gestures and menus GestureActionMap.LoadMacros(File.ReadAllText("defaults/macros.txt")); GestureActionMap.LoadMenus(File.ReadAllText("defaults/menus.txt")); GestureActionMap.LoadActions(File.ReadAllText("defaults/actions.txt")); // needs to be initialized after the GestureActionMap testingForm = new TestingForm(); testingForm.TaskStarted += (string task) => { speech.SpeakAsyncCancelAll(); speech.SelectVoice(TestingVoice); speech.SpeakAsync("Begin"); }; testingForm.TaskFinished += (string task) => { speech.SpeakAsyncCancelAll(); speech.SelectVoice(TestingVoice); speech.SpeakAsync("Task Completed"); }; //trainingForm.TrainingDataUpdated += () => { UpdateTrainingLabel(); }; trainingForm.RecordLocation += (string coarseLocation, string fineLocation) => { Logging.LogTrainingEvent("prepare_to_capture_location"); coarseLocationToTrain = coarseLocation; fineLocationToTrain = fineLocation; // change the countdown timer setting if you don't want to capture the sample immediately (useful if you're training on yourself, for example) countdown = Properties.Settings.Default.CountdownTimer; if (countdown > 0) { tickSound.Play(); CountdownLabel.Text = countdown.ToString(); CountdownLabel.Visible = true; timer.Start(); } else { if (Properties.Settings.Default.EnableSoundEffects) { captureSound.Play(); } ImageTemplate newTemplate = AddTemplate(); Logging.LogTrainingEvent("Added template: " + newTemplate["coarse"] + " " + newTemplate["fine"]); //trainingForm.UpdateLocationList(); } }; trainingForm.RecordGesture += (string gesture) => { Logging.LogTrainingEvent("start_recording_gesture"); if (Properties.Settings.Default.EnableSoundEffects) { beepSound.Play(); } gestureToTrain = gesture; recordingGesture = true; }; trainingForm.AutoCaptureGesture += (string gesture) => { Logging.LogTrainingEvent("start_autocapture_gesture"); if (Properties.Settings.Default.EnableSoundEffects) { beepSound.Play(); } gestureToTrain = gesture; autoTrainGesture = true; }; trainingForm.StopAutoCapturingGestures += () => { Logging.LogTrainingEvent("stop_autocapture_gesture"); autoTrainGesture = false; GestureRecognition.Train(); trainingForm.UpdateGestureList(); }; trainingForm.AutoCaptureLocation += (string coarseLocation, string fineLocation) => { coarseLocationToTrain = coarseLocation; fineLocationToTrain = fineLocation; Logging.LogTrainingEvent("start_autocapture_location"); countdown = Properties.Settings.Default.CountdownTimer; if (countdown > 0) { prepareToAutoTrainLocation = true; tickSound.Play(); CountdownLabel.Text = countdown.ToString(); CountdownLabel.Visible = true; timer.Start(); } else { if (Properties.Settings.Default.EnableSoundEffects) { beepSound.Play(); } autoTrainLocation = true; } }; trainingForm.StopAutoCapturingLocation += () => { autoTrainLocation = false; Logging.LogTrainingEvent("stop_autocapture_location"); }; TouchSegmentation.TouchDownEvent += () => { touchDown = true; hovering = false; hoverCoarseLocation = null; hoverFineLocation = null; touchStart = DateTime.Now; Sensors.Instance.Brightness = ledMaxBrightness; Logging.LogOtherEvent("touch_down"); // reset the location predictions if (!recentTouchUp) { while (coarseLocationProbabilities.Count > 0) { coarseLocationProbabilities.Take(); } while (fineLocationProbabilities.Count > 0) { fineLocationProbabilities.Take(); } while (gestureCoarseLocationProbabilities.Count > 0) { gestureCoarseLocationProbabilities.Take(); } while (gestureFineLocationProbabilities.Count > 0) { gestureFineLocationProbabilities.Take(); } while (coarseLocationPredictions.Count > 0) { coarseLocationPredictions.Take(); } while (gestureCoarseLocationPredictions.Count > 0) { gestureCoarseLocationPredictions.Take(); } while (gestureFocusWeights.Count > 0) { gestureFocusWeights.Take(); } while (gestureSensorReadings.Count > 0) { gestureSensorReadings.Take(); } foreach (Sensors.Reading reading in sensorReadingHistory) { gestureSensorReadings.Add(reading); } Logging.LogOtherEvent("smoothing_reset"); } Invoke(new MethodInvoker(delegate { TouchStatusLabel.Text = "Touch Down"; })); }; TouchSegmentation.TouchUpEvent += () => { touchDown = false; recentTouchUp = true; Invoke(new MethodInvoker(delegate { TouchStatusLabel.Text = "Touch Up"; })); Logging.LogOtherEvent("touch_up"); // trigger an event after a short delay to prevent false positives and allow for the double-tap gesture // it is cancelled if the user touches down again within that time Task.Factory.StartNew(() => { Thread.Sleep(touchUpDelay); recentTouchUp = false; if (!touchDown) { Logging.LogOtherEvent("touch_up_timeout"); Sensors.Instance.Brightness = 0; if (autoTrainLocation) { autoTrainLocation = false; trainingForm.StopAutoCapture(); } if ((hovering && !recordingGesture && !autoTrainGesture) || trainingForm.Training) { return; } // process the gesture Gesture gesture = new Gesture(gestureSensorReadings.ToArray()); if (gestureSensorReadings.Count > 60) { GestureRecognition.PreprocessGesture(gesture); if (recordingGesture || autoTrainGesture) { //Monitor.Enter(recognitionLock); recordingGesture = false; if (Properties.Settings.Default.EnableSoundEffects) { captureSound.Play(); Logging.LogAudioEvent("capture", false); } DateTime start = DateTime.Now; gesture.ClassName = gestureToTrain; GestureRecognition.AddTrainingExample(gesture, gestureToTrain); Logging.LogTrainingEvent("Add gesture: " + gestureToTrain); if (!autoTrainGesture) { GestureRecognition.Train(); Debug.WriteLine("Training: " + (DateTime.Now - start).TotalMilliseconds + " ms"); } start = DateTime.Now; if (!autoTrainGesture) { //trainingForm.UpdateGestureList(); trainingForm.AddGesture(gesture); Debug.WriteLine("Updating List: " + (DateTime.Now - start).TotalMilliseconds + " ms"); } //Monitor.Exit(recognitionLock); } else { gesture.ClassName = GestureRecognition.PredictGesture(gesture); } } Monitor.Enter(recognitionLock); // predict the most likely location over the coarse of the gesture, weighted by voting and image focus Dictionary <string, float>[] tempCoarseProbabilities = gestureCoarseLocationProbabilities.ToArray(); Dictionary <string, float>[] tempFineProbabilities = gestureFineLocationProbabilities.ToArray(); string[] tempCoarsePredictions = gestureCoarseLocationPredictions.ToArray(); float[] tempFocusWeights = gestureFocusWeights.ToArray(); // sum up the probabilities Dictionary <string, float> totalProbabilities = new Dictionary <string, float>(); //foreach (Dictionary<string, float> probabilities in gestureCoarseLocationProbabilities) for (int i = 0; i < tempCoarseProbabilities.Length; i++) { Dictionary <string, float> probabilities = tempCoarseProbabilities[i]; float weight = Math.Max(tempFocusWeights.Length > i ? tempFocusWeights[i] : 0.01f, 0.01f); foreach (string key in probabilities.Keys) { if (!totalProbabilities.ContainsKey(key)) { totalProbabilities[key] = 0; } totalProbabilities[key] += weight * probabilities[key] / numLocationPredictions; } } float maxProb = 0; string coarseLocation = ""; float coarseProbability = 0; foreach (string key in totalProbabilities.Keys) { if (totalProbabilities[key] > maxProb) { maxProb = totalProbabilities[key]; coarseLocation = key; coarseProbability = maxProb; } } // sum up the probabilities totalProbabilities = new Dictionary <string, float>(); //foreach (Dictionary<string, float> probabilities in gestureFineLocationProbabilities) for (int i = 0; i < tempFineProbabilities.Length; i++) { if (i < tempCoarsePredictions.Length && tempCoarsePredictions[i] == coarseLocation) { Dictionary <string, float> probabilities = tempFineProbabilities[i]; float weight = Math.Max(tempFocusWeights.Length > i ? tempFocusWeights[i] : 0.01f, 0.01f); foreach (string key in probabilities.Keys) { if (!totalProbabilities.ContainsKey(key)) { totalProbabilities[key] = 0; } totalProbabilities[key] += weight * probabilities[key] / numLocationPredictions; } } } maxProb = 0; string fineLocation = ""; float fineProbability = 0; foreach (string key in totalProbabilities.Keys) { if (totalProbabilities[key] > maxProb) { maxProb = totalProbabilities[key]; fineLocation = key; fineProbability = maxProb; } } Logging.LogGestureEvent(gesture.ClassName, coarseLocation + " " + fineLocation, gesture); Monitor.Exit(recognitionLock); if (fineLocation == "") { Debug.WriteLine("Error"); } Invoke(new MethodInvoker(delegate { CoarsePredictionLabel.Text = "= " + coarseLocation; CoarseProbabilityLabel.Text = " (response = " + (coarseProbability * 100).ToString("0.0") + ")"; FinePredictionLabel.Text = "= " + fineLocation; FineProbabilityLabel.Text = " (response = " + (fineProbability * 100).ToString("0.0") + ")"; GesturePredictionLabel.Text = gesture.ClassName; if (Properties.Settings.Default.EnableSpeechOutput) { if (Properties.Settings.Default.EnableApplicationDemos) { string actionResult = GestureActionMap.PerformAction(gesture.ClassName, coarseLocation, fineLocation, Properties.Settings.Default.GestureMode, Properties.Settings.Default.FixedApplicationResponses, lockToCurrentTask: Properties.Settings.Default.LockToCurrentTask); if (actionResult != null && actionResult.Length > 0) { speech.SpeakAsyncCancelAll(); speech.SelectVoice(MenuVoice); //speech.SpeakAsync(gesture.ClassName + " " + coarseLocation + " " + fineLocation); speech.SpeakAsync(actionResult); Logging.LogAudioEvent(actionResult); } } else { speech.SpeakAsyncCancelAll(); speech.SelectVoice(MenuVoice); speech.SpeakAsync(gesture.ClassName + " " + coarseLocation + " " + fineLocation); Logging.LogAudioEvent(gesture.ClassName + " " + coarseLocation + " " + fineLocation); } } })); } }); }; // Initialize the camera and other sensors in a background thread to avoid hanging the UI Task.Factory.StartNew(() => { Camera.Instance.FrameAvailable += Camera_FrameAvailable; Camera.Instance.Error += (string error) => { Debug.WriteLine(error); Invoke(new MethodInvoker(delegate { Text = "Error: could not connect to camera"; })); }; Camera.Instance.Brightness = Properties.Settings.Default.CameraBrightness; Sensors.Instance.ReadingAvailable += Sensors_ReadingAvailable; Camera.Instance.Connect(); Logging.LogOtherEvent("camera_connected"); Sensors.Instance.Connect(Properties.Settings.Default.SensorPort); Logging.LogOtherEvent("sensors_connected"); Sensors.Instance.NumSensors = Properties.Settings.Default.SingleIMU ? 1 : 2; Invoke(new MethodInvoker(delegate { if (Properties.Settings.Default.TrainingVisible) { trainingToolStripMenuItem.PerformClick(); } if (Properties.Settings.Default.SettingsVisible) { settingsToolStripMenuItem.PerformClick(); } if (Properties.Settings.Default.TestingVisible) { testingToolStripMenuItem.PerformClick(); } })); }); }
void Camera_FrameAvailable(VideoFrame frame) { if (closing) { return; } numLocationPredictions = Properties.Settings.Default.TestingMode.Contains("Training") ? 1 : Properties.Settings.Default.PredictionSmoothing; ImageTemplate template = new ImageTemplate(frame.Clone()); FPS.Camera.Update(); Task.Factory.StartNew(() => { try { Logging.LogVideoFrame(frame.Image.Bitmap); float focus = 0; //lock (processingLock) if (Monitor.TryEnter(preprocessingLock)) { ImageProcessing.ProcessTemplate(template, false); focus = ImageProcessing.ImageFocus(template); Logging.LogFrameProcessed(); currTemplate = template; FPS.Instance("processing").Update(); Monitor.Exit(preprocessingLock); } else { return; } string coarseLocation = "", fineLocation = ""; float coarseProbability = 0, fineProbability = 0; bool hasUpdate = false; string predictedCoarseLocation = "", predictedFineLocation = ""; if (touchDown && !trainingForm.Training && Monitor.TryEnter(recognitionLock)) { try { if (Localization.Instance.GetNumTrainingExamples() > 0) { Dictionary <string, float> coarseProbabilities = new Dictionary <string, float>(); predictedCoarseLocation = Localization.Instance.PredictCoarseLocation(currTemplate, out coarseProbabilities); //coarseLocationPredictions.Add(predictedCoarseLocation); //while (coarseLocationPredictions.Count > numLocationPredictions) coarseLocationPredictions.Take(); coarseLocationProbabilities.Add(coarseProbabilities); gestureCoarseLocationProbabilities.Add(coarseProbabilities); while (coarseLocationProbabilities.Count > numLocationPredictions) { coarseLocationProbabilities.Take(); } //// compute the mode of the array //var groups = coarseLocationPredictions.GroupBy(v => v); //int maxCount = groups.Max(g => g.Count()); //coarseLocation = groups.First(g => g.Count() == maxCount).Key; // sum up the probabilities Dictionary <string, float> totalProbabilities = new Dictionary <string, float>(); foreach (Dictionary <string, float> probabilities in coarseLocationProbabilities) { foreach (string key in probabilities.Keys) { if (!totalProbabilities.ContainsKey(key)) { totalProbabilities[key] = 0; } totalProbabilities[key] += probabilities[key] / numLocationPredictions; } } float maxProb = 0; foreach (string key in totalProbabilities.Keys) { if (totalProbabilities[key] > maxProb) { maxProb = totalProbabilities[key]; coarseLocation = key; coarseProbability = maxProb; } } coarseLocationPredictions.Add(coarseLocation); gestureCoarseLocationPredictions.Add(coarseLocation); while (coarseLocationPredictions.Count > numLocationPredictions) { coarseLocationPredictions.Take(); } if (!Properties.Settings.Default.CoarseOnly) { bool foundFeatureMatch = false; Dictionary <string, float> fineProbabilities = new Dictionary <string, float>(); predictedFineLocation = Localization.Instance.PredictFineLocation(currTemplate, out foundFeatureMatch, out fineProbabilities, true, false, false, coarseLocation); //fineLocationPredictions.Add(predictedFineLocation); //while (fineLocationPredictions.Count > numLocationPredictions) fineLocationPredictions.Take(); fineLocationProbabilities.Add(fineProbabilities); gestureFineLocationProbabilities.Add(fineProbabilities); while (fineLocationProbabilities.Count > numLocationPredictions) { fineLocationProbabilities.Take(); } // compute the mode of the array //var groups = fineLocationPredictions.GroupBy(v => v); //int maxCount = groups.Max(g => g.Count()); //fineLocation = groups.First(g => g.Count() == maxCount).Key; // sum up the probabilities totalProbabilities = new Dictionary <string, float>(); //foreach (Dictionary<string, float> probabilities in fineLocationProbabilities) for (int fineIndex = 0; fineIndex < fineLocationProbabilities.Count; fineIndex++) { Dictionary <string, float> probabilities = fineLocationProbabilities.ToArray()[fineIndex]; // make sure that the fine prediction matches the classes for the coarse prediction if (coarseLocationPredictions.ToArray()[fineIndex] == coarseLocation) { foreach (string key in probabilities.Keys) { if (!totalProbabilities.ContainsKey(key)) { totalProbabilities[key] = 0; } totalProbabilities[key] += probabilities[key] / numLocationPredictions; } } } maxProb = 0; foreach (string key in totalProbabilities.Keys) { if (totalProbabilities[key] > maxProb) { maxProb = totalProbabilities[key]; fineLocation = key; fineProbability = maxProb; } } } Logging.LogLocationEvent(predictedCoarseLocation + " " + predictedFineLocation, coarseLocation + " " + fineLocation); gestureFocusWeights.Add(focus); FPS.Instance("matching").Update(); hasUpdate = true; } if (autoTrainLocation && (coarseLocation != coarseLocationToTrain || fineLocation != fineLocationToTrain) && Monitor.TryEnter(trainingLock)) { trainingForm.Training = true; ImageTemplate newTemplate = CopyTemplate(currTemplate); Invoke(new MethodInvoker(delegate { TrainingLabel.Visible = true; })); if (Properties.Settings.Default.EnableSoundEffects) { captureSound.Play(); Logging.LogAudioEvent("capture", false); } AddTemplate(newTemplate); Logging.LogTrainingEvent("Added template: " + newTemplate["coarse"] + " " + newTemplate["fine"]); Thread.Sleep(100); //if (Properties.Settings.Default.EnableSoundEffects) { beepSound.Play(); Logging.LogAudioEvent("beep", false); } trainingForm.Training = false; Invoke(new MethodInvoker(delegate { TrainingLabel.Visible = false; })); Monitor.Exit(trainingLock); return; } if ((DateTime.Now - touchStart).TotalMilliseconds > Properties.Settings.Default.HoverTimeThreshold) { hovering = true; //if (hoverCoarseLocation == null || coarseLocation == hoverCoarseLocation) // make sure we have the same coarse location, to help prevent jumping around { hoverCoarseLocation = coarseLocation; if (hoverFineLocation == null || fineLocation != hoverFineLocation) // make sure we haven't reported the fine location already { hoverFineLocation = fineLocation; Debug.WriteLine(hoverCoarseLocation + " " + hoverFineLocation); Invoke(new MethodInvoker(delegate { GesturePredictionLabel.Text = "Hover"; })); if (Properties.Settings.Default.EnableSpeechOutput) { if (Properties.Settings.Default.EnableApplicationDemos) { string actionResult = GestureActionMap.PerformAction("Hover", coarseLocation, fineLocation, Properties.Settings.Default.GestureMode, Properties.Settings.Default.FixedApplicationResponses); if (actionResult != null && actionResult.Length > 0) { speech.SpeakAsyncCancelAll(); speech.SelectVoice(MenuVoice); speech.SpeakAsync(actionResult); Logging.LogAudioEvent(actionResult); } } else { speech.SpeakAsyncCancelAll(); speech.SelectVoice(MenuVoice); speech.SpeakAsync("Hover " + coarseLocation + " " + fineLocation); Logging.LogAudioEvent("Hover " + coarseLocation + " " + fineLocation); } } } } } } finally { Monitor.Exit(recognitionLock); } } //LBP.GetInstance(frame.Image.Size).GetHistogram(frame); Invoke(new MethodInvoker(delegate { Display.Image = frame.Image.Bitmap; if (hasUpdate) { CoarsePredictionLabel.Text = coarseLocation; CoarseProbabilityLabel.Text = " (response = " + (coarseProbability * 100).ToString("0.0") + ")"; //Debug.WriteLine(coarseLocation); FinePredictionLabel.Text = fineLocation; FineProbabilityLabel.Text = " (response = " + (fineProbability * 100).ToString("0.0") + ")"; //Debug.WriteLine(fineLocation); } //else if(!touchDown) //{ // CoarsePredictionLabel.Text = ""; // CoarseProbabilityLabel.Text = ""; // FinePredictionLabel.Text = ""; // FineProbabilityLabel.Text = ""; //} //focus = (int)(focus / 100) * 100; Text = FPS.Camera.Average.ToString("0") + " fps camera / " + (Sensors.Instance.IsConnected ? FPS.Sensors.Average.ToString("0") + " fps sensors / " + FPS.Instance("processing").Average.ToString("0") + " fps processing / " + FPS.Instance("matching").Average.ToString("0") + " fps matching" : "Waiting for Sensors") /*+ " / focus = " + focus.ToString("0")*/; })); } catch { } }); }
private void RemoveButton_Click(object sender, EventArgs e) { if (TrainingDataViewer.SelectedIndex == 0) // location tab is showing { foreach (ListViewItem removeItem in LocationView.SelectedItems) { int imageIndex = removeItem.ImageIndex; ImageTemplate template = (ImageTemplate)removeItem.Tag; Localization.Instance.RemoveTrainingExample(template); Logging.LogOtherEvent("Removed location example: " + (string)template["path"]); LocationView.LargeImageList.Images.RemoveAt(imageIndex); foreach (ListViewItem updateItem in LocationView.Items) { if (updateItem.ImageIndex > imageIndex) { updateItem.ImageIndex--; } } if (removeItem.Group.Items.Count == 0) { LocationView.Groups.Remove(removeItem.Group); } LocationView.Items.Remove(removeItem); } training = true; Localization.Instance.Train(); UpdateLocationCount(); training = false; OnTrainingDataUpdated(); } else // gesture tab is showing { foreach (ListViewItem removeItem in GestureView.SelectedItems) { int imageIndex = removeItem.ImageIndex; Gesture template = (Gesture)removeItem.Tag; Logging.LogOtherEvent("Removed gesture example: " + template.Path); GestureRecognition.RemoveTrainingExample(template); GestureView.LargeImageList.Images.RemoveAt(imageIndex); foreach (ListViewItem updateItem in GestureView.Items) { if (updateItem.ImageIndex > imageIndex) { updateItem.ImageIndex--; } } if (removeItem.Group.Items.Count == 0) { GestureView.Groups.Remove(removeItem.Group); } GestureView.Items.Remove(removeItem); } training = true; GestureRecognition.Train(); UpdateGestureCount(); training = false; OnTrainingDataUpdated(); } }