Example #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            // instance initialization requires UI thread, wait until load
            this.model = GestureModel.Instance;

            SynchronizationContext ctx = SynchronizationContext.Current;

            this.model.FrameReady += (s, args) =>
                {
                    if (disabled)
                    {
                        return;
                    }

                    Bitmap fullFrame = this.model.RawDepthFrame.ToBitmap();
                    Bitmap croppedFrame = this.model.CroppedFrame.ToBitmap();
                    using (Graphics g = Graphics.FromImage(fullFrame))
                    {
                        int startX = this.model.CropStartX;
                        int startY = this.model.CropStartY;
                        int croppedWidth = this.model.CroppedFrame.Width;
                        int croppedHeight = this.model.CroppedFrame.Height;

                        Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
                        g.DrawRectangle(pen, startX + croppedWidth / 2, startY + croppedHeight / 2, 1, 1);
                        g.DrawRectangle(pen, startX, startY, croppedWidth, croppedHeight);
                    }
                    
                    ctx.Post((o) => {
                        Bitmap fitFull = new Bitmap(fullFrame, this.fullFrameStream.Width, this.fullFrameStream.Height);
                        Bitmap fitCropped;

                        // make sure the cropped image has area
                        if (croppedFrame.Height > 0 && croppedFrame.Width > 0)
                        {
                            // resize images in order to fit into picture box in the home tab
                            double croppedRatio_w_h = (double)croppedFrame.Width / croppedFrame.Height;
                            if (croppedRatio_w_h > Width_To_Height_Ratio)  // cropped image is long in horizontal
                            {
                                fitCropped = new Bitmap(croppedFrame, this.croppedFrameStream.Width, (int)(this.croppedFrameStream.Width / croppedRatio_w_h));
                            }
                            else  // cropped image is long in vertical
                            {
                                fitCropped = new Bitmap(croppedFrame, (int)(this.croppedFrameStream.Height * croppedRatio_w_h), this.croppedFrameStream.Height);
                            }
                        }
                        else
                            fitCropped = null;

                        this.croppedFrameStream.Image = fitCropped;
                        this.fullFrameStream.Image = fitFull;

                        framesCount++;
                    }, null);
                };

            this.model.ImageCollectionFinished += (s, args) =>
                {
                    this.model.Stop();
                    ctx.Post((o) =>
                    {
                        this.message.Text = "Image collection finished. Building new prediction model now...";
                    }, null);
                };

            this.model.NewModelReady += (s, args) =>
                {
                    ctx.Post((o) =>
                    {
                        this.message.Text = "New prediction model ready.";
                    }, null);
                };

            this.model.StatusChanged += (s, args) =>
                {
                    ctx.Post((o) =>
                        {
                            this.modelStatusDisplay.Text = args.Status;
                            this.message.Text = args.Status;
                        }, null);
                };

            System.Timers.Timer fpsCounter = new System.Timers.Timer(1000);
            fpsCounter.AutoReset = true;
            fpsCounter.Elapsed += (src, args) =>
            {
                if (disabled)
                {
                    return;
                }
                ctx.Post((o) =>
                {
                    this.framesPerSecond.Text = "FPS = " + framesCount;
                    framesCount = 0;
                }, null);
            };

            fpsCounter.Start();
        }
        private void CollectImageForm_Load(object sender, EventArgs e)
        {
            // instance initialization requires UI thread, wait until load
            this.model = GestureModel.Instance;
            this.timer = new Stopwatch();

            SynchronizationContext ctx = SynchronizationContext.Current;

            this.model.FrameReady += (s, args) =>
            {
                Bitmap fullFrame = this.model.RawDepthFrame.ToBitmap();
                originalFullFrame = (Bitmap)fullFrame.Clone();
                croppedFrame      = this.model.CroppedFrame.ToBitmap();
                using (Graphics g = Graphics.FromImage(fullFrame))
                {
                    int startX        = this.model.CropStartX;
                    int startY        = this.model.CropStartY;
                    int croppedWidth  = this.model.CroppedFrame.Width;
                    int croppedHeight = this.model.CroppedFrame.Height;

                    System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
                    g.DrawRectangle(pen, startX + croppedWidth / 2, startY + croppedHeight / 2, 1, 1);
                    g.DrawRectangle(pen, startX, startY, croppedWidth, croppedHeight);
                }

                ctx.Post((o) =>
                {
                    this.fullBox.Image    = fullFrame;
                    this.croppedBox.Image = croppedFrame;
                    this.timer.Stop();

                    if (collecting)
                    {
                        if (this.timer.ElapsedMilliseconds < COUNT_TIMER * MILISEC)
                        {
                            this.imageCollectionStatus.Text = "Count: " + (COUNT_TIMER - this.timer.ElapsedMilliseconds / MILISEC);
                            this.timer.Start();
                            return;
                        }
                        this.imageCollectionStatus.Text = "Collecting Images (" + (snapshotCount / CAPTURE_FREQ) + "/" + MAX_CAPTURES + ")";
                        saveImage();

                        // once it gets to max captures, stop capture images.
                        if (snapshotCount / CAPTURE_FREQ > MAX_CAPTURES)
                        {
                            this.collecting = false;
                            this.startCollectButton.Text    = "Start";
                            this.imageCollectionStatus.Text = "Finished image Collection";
                            this.timer.Reset();
                            this.snapshotCount = 0;
                        }
                    }
                    else
                    {
                        this.imageCollectionStatus.Text = "Ready to Collect Images";
                    }
                    this.timer.Start();
                    framesCount++;
                }, null);
            };
        }
        private void MainWindow_Load(object sender, EventArgs e)
        {
            // instance initialization requires UI thread, wait until load
            this.model = GestureModel.Instance;
            this.gestures = Gestures.Instance;
            this.keyControls = KeyControls.Instance;
            this.controller = new Control();
            this.LoadTable();
            this.LoadTutorial();
            this.volumeTimer = new Stopwatch();
            this.volumeTimer.Start();
            this.bWork = new BackgroundWorker();
            bWork.DoWork += new DoWorkEventHandler(bw_DoWork);

            // direct to tutorial page if necessary
            string[] lines = File.ReadAllLines(GestureStudio.SettingFile);
            string[] directTutorial = lines[0].Split(':');
            if (directTutorial[0] == "directTutorial" && directTutorial[1] == "yes")
            {
                using (DirectToTutorialForm directForm = new DirectToTutorialForm())
                {
                    DialogResult result = directForm.ShowDialog();
                    if (DialogResult.Yes == result)
                    {
                        this.mainWindowTabs.SelectedTab = this.tutorialTab;
                    }
                    else if (DialogResult.No == result)
                    {
                        // don't show this dialog next time
                        if (directForm.isIgnoreChecked())
                        {
                            using (StreamWriter file = new StreamWriter(GestureStudio.SettingFile))
                            {
                                StringBuilder sb = new StringBuilder();
                                sb.Append(directTutorial[0] + ":no");
                                file.WriteLine(sb.ToString());
                            }
                        }
                    }
                    else
                    {
                    }

                }
            }

            SynchronizationContext ctx = SynchronizationContext.Current;


            this.model.FrameReady += (s, args) =>
            {
                if (disabled)
                {
                    return;
                }
                
                Bitmap fullFrame = this.model.RawDepthFrame.ToBitmap();
                Bitmap croppedFrame = this.model.CroppedFrame.ToBitmap();

                using (Graphics g = Graphics.FromImage(fullFrame))
                {
                    int startX = this.model.CropStartX;
                    int startY = this.model.CropStartY;
                    int croppedWidth = this.model.CroppedFrame.Width;
                    int croppedHeight = this.model.CroppedFrame.Height;
                    Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
                    if (croppedWidth > 10 && croppedHeight > 10 && croppedHeight < 200 && croppedHeight < 200) {
                        g.DrawRectangle(pen, startX + croppedWidth / 2, startY + croppedHeight / 2, 1, 1);
                        g.DrawRectangle(pen, startX, startY, croppedWidth, croppedHeight);
                    }
                }
                ctx.Post((o) =>
                {
                    Bitmap fitFull = new Bitmap(fullFrame, this.mainWindow_full.Width, this.mainWindow_full.Height);
                    Bitmap fitCropped;
                    if (GestureStudio.DISPLAY_DETECTED_GESTURE_IMG)
                    {


                    }
                    else
                    {
                        // make sure the cropped image has area
                        if (croppedFrame.Height > 0 && croppedFrame.Width > 0)
                        {
                            // resize images in order to fit into picture box in the home tab
                            double croppedRatio_w_h = (double)croppedFrame.Width / croppedFrame.Height;
                            if (croppedRatio_w_h > Width_To_Height_Ratio)  // cropped image is long in horizontal
                            {
                                fitCropped = new Bitmap(croppedFrame, this.mainWindow_cropped.Width, (int)(this.mainWindow_cropped.Width / croppedRatio_w_h));
                            }
                            else  // cropped image is long in vertical
                            {
                                fitCropped = new Bitmap(croppedFrame, (int)(this.mainWindow_cropped.Height * croppedRatio_w_h), this.mainWindow_cropped.Height);
                            }
                        }
                        else
                            fitCropped = null;

                        this.mainWindow_cropped.Image = fitCropped;
                    }




                    this.mainWindow_full.Image = fitFull;

                    framesCount++;
                }, null);

            };

            this.model.CategoryDetected += (s, args) =>
            {
                if (disabled)
                {
                    return;
                }

                ctx.Post((o) =>
                {
                    int label = (int)o;
                    if (GestureStudio.GENERIC_GESTURES)
                    {


                        if (this.volumeTimer.ElapsedMilliseconds > 2500)
                        {
                            
                            
                            if (GestureStudio.DISPLAY_DETECTED_GESTURE_IMG) {
                                string img_path = GestureStudio.GestureImagePath + "/" + Gestures.getGestureName(label) + ".png";

                                Bitmap resized_img = null;
                                if (File.Exists(img_path))
                                {
                                    Bitmap img = new Bitmap(img_path);
                                    // resize

                                    resized_img = new Bitmap(img, this.mainWindow_cropped.Width, this.mainWindow_cropped.Height);
                                }

                                this.mainWindow_cropped.Image = resized_img;
                            }
                            // lookup which window is focused and find if it is in the gestures list
                            if (Gestures.getGestureName(label) != null && Gestures.getGestureName(label).ToLower() != "noise")
                            {

                                this.mainWindow_status.Text = "Your Gesture: [" + Gestures.getGestureName(label) + "]";
                                if (Gestures.getGestures()[label].getAllCommands().Count != 0)
                                    this.commandLabel.Text = "[" + Gestures.getGestures()[label].getAllCommands()[0].getCommand() + "]";
                                else
                                    this.commandLabel.Text = "[]";
                            }
                            else
                            {
                                this.mainWindow_status.Text = "Your Gesture: []";
                                this.commandLabel.Text = "[]";
                            }
                            // string focusedApp = ...
                            // int appId = Gestures.getAppId(focusedApp);
                            AppKeyInfo appInfo = Gestures.getAppKeyForGesture(label, 0 /*appId*/);
                            if (appInfo == null || KeyControls.getKeyMatches()[0/*appId*/] == null)
                                return;

                            string detectedCommand = KeyControls.getKeyMatches()[0][appInfo.getCommand()];
                            if (detectedCommand != null && !detectedCommand.Equals("f8") && !detectedCommand.Equals("f9"))
                            {
                                this.volumeTimer.Reset();
                                this.volumeTimer.Start();
                            }

                            if (!this.bWork.IsBusy) {
                                this.bWork.RunWorkerAsync(detectedCommand); //() => { this.controller.parseThenExecute(detectedCommand);}); 
                            }
                            
                        }                    
                        
                    } 
                    else
                        this.mainWindow_status.Text = "Your Gesture: [" + LabelToString(label) + "]";
                }, args.CategoryLabel);
            };


        }
Example #4
0
        private void Form1_Load(object sender, EventArgs e)
        {
            // instance initialization requires UI thread, wait until load
            this.model = GestureModel.Instance;

            SynchronizationContext ctx = SynchronizationContext.Current;

            this.model.FrameReady += (s, args) =>
            {
                if (disabled)
                {
                    return;
                }

                Bitmap fullFrame    = this.model.RawDepthFrame.ToBitmap();
                Bitmap croppedFrame = this.model.CroppedFrame.ToBitmap();
                using (Graphics g = Graphics.FromImage(fullFrame))
                {
                    int startX        = this.model.CropStartX;
                    int startY        = this.model.CropStartY;
                    int croppedWidth  = this.model.CroppedFrame.Width;
                    int croppedHeight = this.model.CroppedFrame.Height;

                    Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
                    g.DrawRectangle(pen, startX + croppedWidth / 2, startY + croppedHeight / 2, 1, 1);
                    g.DrawRectangle(pen, startX, startY, croppedWidth, croppedHeight);
                }

                ctx.Post((o) => {
                    Bitmap fitFull = new Bitmap(fullFrame, this.fullFrameStream.Width, this.fullFrameStream.Height);
                    Bitmap fitCropped;

                    // make sure the cropped image has area
                    if (croppedFrame.Height > 0 && croppedFrame.Width > 0)
                    {
                        // resize images in order to fit into picture box in the home tab
                        double croppedRatio_w_h = (double)croppedFrame.Width / croppedFrame.Height;
                        if (croppedRatio_w_h > Width_To_Height_Ratio)      // cropped image is long in horizontal
                        {
                            fitCropped = new Bitmap(croppedFrame, this.croppedFrameStream.Width, (int)(this.croppedFrameStream.Width / croppedRatio_w_h));
                        }
                        else      // cropped image is long in vertical
                        {
                            fitCropped = new Bitmap(croppedFrame, (int)(this.croppedFrameStream.Height * croppedRatio_w_h), this.croppedFrameStream.Height);
                        }
                    }
                    else
                    {
                        fitCropped = null;
                    }

                    this.croppedFrameStream.Image = fitCropped;
                    this.fullFrameStream.Image    = fitFull;

                    framesCount++;
                }, null);
            };

            this.model.ImageCollectionFinished += (s, args) =>
            {
                this.model.Stop();
                ctx.Post((o) =>
                {
                    this.message.Text = "Image collection finished. Building new prediction model now...";
                }, null);
            };

            this.model.NewModelReady += (s, args) =>
            {
                ctx.Post((o) =>
                {
                    this.message.Text = "New prediction model ready.";
                }, null);
            };

            this.model.StatusChanged += (s, args) =>
            {
                ctx.Post((o) =>
                {
                    this.modelStatusDisplay.Text = args.Status;
                    this.message.Text            = args.Status;
                }, null);
            };

            System.Timers.Timer fpsCounter = new System.Timers.Timer(1000);
            fpsCounter.AutoReset = true;
            fpsCounter.Elapsed  += (src, args) =>
            {
                if (disabled)
                {
                    return;
                }
                ctx.Post((o) =>
                {
                    this.framesPerSecond.Text = "FPS = " + framesCount;
                    framesCount = 0;
                }, null);
            };

            fpsCounter.Start();
        }
        private void MainWindow_Load(object sender, EventArgs e)
        {
            // instance initialization requires UI thread, wait until load
            this.model       = GestureModel.Instance;
            this.gestures    = Gestures.Instance;
            this.keyControls = KeyControls.Instance;
            this.controller  = new Control();
            this.LoadTable();
            this.LoadTutorial();
            this.volumeTimer = new Stopwatch();
            this.volumeTimer.Start();
            this.bWork    = new BackgroundWorker();
            bWork.DoWork += new DoWorkEventHandler(bw_DoWork);

            // direct to tutorial page if necessary
            string[] lines          = File.ReadAllLines(GestureStudio.SettingFile);
            string[] directTutorial = lines[0].Split(':');
            if (directTutorial[0] == "directTutorial" && directTutorial[1] == "yes")
            {
                using (DirectToTutorialForm directForm = new DirectToTutorialForm())
                {
                    DialogResult result = directForm.ShowDialog();
                    if (DialogResult.Yes == result)
                    {
                        this.mainWindowTabs.SelectedTab = this.tutorialTab;
                    }
                    else if (DialogResult.No == result)
                    {
                        // don't show this dialog next time
                        if (directForm.isIgnoreChecked())
                        {
                            using (StreamWriter file = new StreamWriter(GestureStudio.SettingFile))
                            {
                                StringBuilder sb = new StringBuilder();
                                sb.Append(directTutorial[0] + ":no");
                                file.WriteLine(sb.ToString());
                            }
                        }
                    }
                    else
                    {
                    }
                }
            }

            SynchronizationContext ctx = SynchronizationContext.Current;


            this.model.FrameReady += (s, args) =>
            {
                if (disabled)
                {
                    return;
                }

                Bitmap fullFrame    = this.model.RawDepthFrame.ToBitmap();
                Bitmap croppedFrame = this.model.CroppedFrame.ToBitmap();

                using (Graphics g = Graphics.FromImage(fullFrame))
                {
                    int startX        = this.model.CropStartX;
                    int startY        = this.model.CropStartY;
                    int croppedWidth  = this.model.CroppedFrame.Width;
                    int croppedHeight = this.model.CroppedFrame.Height;
                    Pen pen           = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
                    if (croppedWidth > 10 && croppedHeight > 10 && croppedHeight < 200 && croppedHeight < 200)
                    {
                        g.DrawRectangle(pen, startX + croppedWidth / 2, startY + croppedHeight / 2, 1, 1);
                        g.DrawRectangle(pen, startX, startY, croppedWidth, croppedHeight);
                    }
                }
                ctx.Post((o) =>
                {
                    Bitmap fitFull = new Bitmap(fullFrame, this.mainWindow_full.Width, this.mainWindow_full.Height);
                    Bitmap fitCropped;
                    if (GestureStudio.DISPLAY_DETECTED_GESTURE_IMG)
                    {
                    }
                    else
                    {
                        // make sure the cropped image has area
                        if (croppedFrame.Height > 0 && croppedFrame.Width > 0)
                        {
                            // resize images in order to fit into picture box in the home tab
                            double croppedRatio_w_h = (double)croppedFrame.Width / croppedFrame.Height;
                            if (croppedRatio_w_h > Width_To_Height_Ratio)  // cropped image is long in horizontal
                            {
                                fitCropped = new Bitmap(croppedFrame, this.mainWindow_cropped.Width, (int)(this.mainWindow_cropped.Width / croppedRatio_w_h));
                            }
                            else  // cropped image is long in vertical
                            {
                                fitCropped = new Bitmap(croppedFrame, (int)(this.mainWindow_cropped.Height * croppedRatio_w_h), this.mainWindow_cropped.Height);
                            }
                        }
                        else
                        {
                            fitCropped = null;
                        }

                        this.mainWindow_cropped.Image = fitCropped;
                    }



                    this.mainWindow_full.Image = fitFull;

                    framesCount++;
                }, null);
            };

            this.model.CategoryDetected += (s, args) =>
            {
                if (disabled)
                {
                    return;
                }

                ctx.Post((o) =>
                {
                    int label = (int)o;
                    if (GestureStudio.GENERIC_GESTURES)
                    {
                        if (this.volumeTimer.ElapsedMilliseconds > 2500)
                        {
                            if (GestureStudio.DISPLAY_DETECTED_GESTURE_IMG)
                            {
                                string img_path = GestureStudio.GestureImagePath + "/" + Gestures.getGestureName(label) + ".png";

                                Bitmap resized_img = null;
                                if (File.Exists(img_path))
                                {
                                    Bitmap img = new Bitmap(img_path);
                                    // resize

                                    resized_img = new Bitmap(img, this.mainWindow_cropped.Width, this.mainWindow_cropped.Height);
                                }

                                this.mainWindow_cropped.Image = resized_img;
                            }
                            // lookup which window is focused and find if it is in the gestures list
                            if (Gestures.getGestureName(label) != null && Gestures.getGestureName(label).ToLower() != "noise")
                            {
                                this.mainWindow_status.Text = "Your Gesture: [" + Gestures.getGestureName(label) + "]";
                                if (Gestures.getGestures()[label].getAllCommands().Count != 0)
                                {
                                    this.commandLabel.Text = "[" + Gestures.getGestures()[label].getAllCommands()[0].getCommand() + "]";
                                }
                                else
                                {
                                    this.commandLabel.Text = "[]";
                                }
                            }
                            else
                            {
                                this.mainWindow_status.Text = "Your Gesture: []";
                                this.commandLabel.Text      = "[]";
                            }
                            // string focusedApp = ...
                            // int appId = Gestures.getAppId(focusedApp);
                            AppKeyInfo appInfo = Gestures.getAppKeyForGesture(label, 0 /*appId*/);
                            if (appInfo == null || KeyControls.getKeyMatches()[0 /*appId*/] == null)
                            {
                                return;
                            }

                            string detectedCommand = KeyControls.getKeyMatches()[0][appInfo.getCommand()];
                            if (detectedCommand != null && !detectedCommand.Equals("f8") && !detectedCommand.Equals("f9"))
                            {
                                this.volumeTimer.Reset();
                                this.volumeTimer.Start();
                            }

                            if (!this.bWork.IsBusy)
                            {
                                this.bWork.RunWorkerAsync(detectedCommand); //() => { this.controller.parseThenExecute(detectedCommand);});
                            }
                        }
                    }
                    else
                    {
                        this.mainWindow_status.Text = "Your Gesture: [" + LabelToString(label) + "]";
                    }
                }, args.CategoryLabel);
            };
        }
        private void CollectImageForm_Load(object sender, EventArgs e)
        {
            // instance initialization requires UI thread, wait until load
            this.model = GestureModel.Instance;
            this.timer = new Stopwatch();

            SynchronizationContext ctx = SynchronizationContext.Current;

            this.model.FrameReady += (s, args) =>
            {

                Bitmap fullFrame = this.model.RawDepthFrame.ToBitmap();
                originalFullFrame = (Bitmap)fullFrame.Clone();
                croppedFrame = this.model.CroppedFrame.ToBitmap();
                using (Graphics g = Graphics.FromImage(fullFrame))
                {
                    int startX = this.model.CropStartX;
                    int startY = this.model.CropStartY;
                    int croppedWidth = this.model.CroppedFrame.Width;
                    int croppedHeight = this.model.CroppedFrame.Height;

                    System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
                    g.DrawRectangle(pen, startX + croppedWidth / 2, startY + croppedHeight / 2, 1, 1);
                    g.DrawRectangle(pen, startX, startY, croppedWidth, croppedHeight);

                }

                ctx.Post((o) =>
                {
                    this.fullBox.Image = fullFrame;
                    this.croppedBox.Image = croppedFrame;
                    this.timer.Stop();

                    if (collecting)
                    {
                        if (this.timer.ElapsedMilliseconds < COUNT_TIMER * MILISEC)
                        {
                            this.imageCollectionStatus.Text = "Count: " + (COUNT_TIMER - this.timer.ElapsedMilliseconds / MILISEC);
                            this.timer.Start();
                            return;
                        }
                        this.imageCollectionStatus.Text = "Collecting Images (" + (snapshotCount / CAPTURE_FREQ) + "/" + MAX_CAPTURES + ")";
                        saveImage();

                        // once it gets to max captures, stop capture images.
                        if (snapshotCount / CAPTURE_FREQ > MAX_CAPTURES)
                        {
                            this.collecting = false;
                            this.startCollectButton.Text = "Start";
                            this.imageCollectionStatus.Text = "Finished image Collection";
                            this.timer.Reset();
                            this.snapshotCount = 0;
                        }
                    }
                    else
                    {
                        this.imageCollectionStatus.Text = "Ready to Collect Images";
                    }
                    this.timer.Start();
                    framesCount++;
                }, null);
            };
        }