顯示具有 KINECT 標籤的文章。 顯示所有文章
顯示具有 KINECT 標籤的文章。 顯示所有文章

2014/01/03

XNA 使用Kinect並顯示影像

寫過Kinect會知道顯示影像是透過byte的array去取得,在將值讓UI去呈現
XNA則是透過座標以及一些方框去接收,透過Draw方法將影像描繪出來


程式碼:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Kinect;

namespace WindowsGame3
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {

        /// <summary>
        /// 圖形管理
        /// </summary>
        GraphicsDeviceManager graphics;


        /// <summary>
        /// 繪圖
        /// </summary>
        SpriteBatch spriteBatch;


        /// <summary>
        /// Kinect Sensor
        /// </summary>
        KinectSensor kinect;


        /// <summary>
        /// Kinect框架
        /// </summary>
        Texture2D texture2D;

        /// <summary>
        /// 矩形
        /// </summary>
        readonly Rectangle rect = new Rectangle(0, 0, 640, 480);

        /// <summary>
        /// 影像資料
        /// </summary>
        byte[] dataPixels;

        /// <summary>
        /// Kinect初始化
        /// </summary>
        bool isKinectInit = false;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }


        /// <summary>
        /// Kinect初始化
        /// </summary>
        private void Init()
        {
            var sensor = ((from s in KinectSensor.KinectSensors where (s.IsRunning != true && s.Status == KinectStatus.Connected) select s)).FirstOrDefault();

            if (sensor == null)
                isKinectInit = false;
            else
            {
                this.kinect = sensor;
                this.kinect.Start();
                this.kinect.ColorStream.Enable();
                this.kinect.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(kinect_ColorFrameReady);
            }
        }

        void kinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame frame = e.OpenColorImageFrame())
            {
                if (frame == null)
                    return;

                dataPixels = new byte[frame.PixelDataLength];
                frame.CopyPixelDataTo(dataPixels);

                texture2D = new Texture2D(GraphicsDevice, 640, 480);
                texture2D.SetData(dataPixels);

            }
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Init();
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            if (texture2D != null)
            {

                spriteBatch.Begin();
                spriteBatch.Draw(texture2D, rect, Color.White);
                spriteBatch.End();
            }
            base.Draw(gameTime);
        }
    }
}

2013/12/07

C#.Net Kinect 取得玩家ID

取得玩家ID當然是有點用處,這篇僅把程式碼貼出來而已,詳細的作法可以看MSDN!



XAML:
<Window x:Class="WpfApplicationKinectTest2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Unloaded="Window_Unloaded" Loaded="Window_Loaded">
    <Grid>
        <Image Name="Image"></Image>
    </Grid>
</Window>



C#.Net lock in a single skeleton in Microsoft Kinect SDK v 1.8

我們以這篇『C#.Net KINECT 結合影像與骨架』程式碼作為基底,以該基底延伸出新功能。
KinectSensor內有個SkeletonStreamSkeletonStream是用於接收骨架追蹤的串流,SkeletonStream類別又包含了AppChoosesSkeletons屬性,該屬性為布林值

AppChoosesSkeletons的說明為『Gets or sets a value indicating whether the application or the runtime chooses which skeletons to track.』
聰明的你一定看得出來用於鎖定骨架的第一步就設定為true

在透過ChooseSkeletons來鎖定指定的骨架,每個骨架都有它的編號,要注意ChooseSkeletons是多載的方法



  1. SkeletonStream.ChooseSkeletons ()
  2. SkeletonStream.ChooseSkeletons (Int32)
  3. SkeletonStream.ChooseSkeletons (Int32, Int32)

第一種方法僅偵測當前的骨架
第二種方法僅偵測指定的骨架ID,填入0也等同於第一種方法
第三種方法僅偵測兩個指定的骨架ID,這意謂著Kinect頂多鎖定兩個人的骨架而已

鎖定之前,要先確認AppChoosesSkeletons值要設定為True,否則會出錯

2013/09/17

C#.Net 透過Kinect辨識聲音

Kinect本身內建了麥克風陣列,麥克風陣列有一組是作為辨識聲音用的
在辨識聲音上有一個重點是,體感裝置一定要離人很近以及室內較不易吵雜,否則會讓Kinect辨識不清楚

我這次的示範直接將感測器放置在電腦旁而已,如果想知道能測多遠的朋友可以自行量測

開始開發前,你必須先安裝『Microsoft Speech Platform SDK』,目前的版本是出到第11版
如果你的電腦跟我一樣是安裝64位元的朋友,建議你32和64的版本都要安裝,以免到時候出錯

如果想瞭解『Microsoft Speech Platform』,就直接點擊進去看吧!

安裝完『Microsoft Speech Platform SDK』後,初始語言包僅只有英文,如果想要新增的朋友可以點擊『Microsoft Speech Platform - Runtime Languages』下載你需要的語言包

在這邊提醒一下,W8是不支援語言包的,不過仍可以使用『Microsoft Speech Platform SDK』,目前打文章的電腦就是W8 XDD

程式載入時會觸發Window_Loaded事件,該事件在之前的幾篇文章也講解過了。差異只是呼叫的方法不一樣,如果不懂的可以回去看我之前寫得『透過Kinect取得聲音來源方位』及『透過Kinect取聲音並繪製成音波圖

C#.Net 透過Kinect取得聲音來源方位

Kinect麥克風以感應器為中心,辨識±50度聲音來源


透過聲音來源,我們可以再去限制其範圍,最後將其範圍資料導入,去得知聲音內容


2013/09/16

C#.Net 透過Kinect取聲音並繪製成音波圖

Kinect是有內建麥克風陣列在裡頭,所謂的陣列麥克風就是兩組以上,並以麥克風進行偵測,所得聲音用DSP做比對,來還原原始音效。

Kinect的麥克風陣列內含了四組麥克風,分為兩組,一組來取得聲音並進行消除雜音等演算法處理之後,可提供另一組聲音辨識的功能。


最近因為上課需要,所以要在寫Kinect的相關程式,當然這篇就是拿來練功的,不過這次是透過『Kinect體感程式探索-使用C#』去學習關於Kinect的聲音處理部份。而不再透過官方的API

這樣學起來也比較快,省時間XDDD

照慣例,還是要分析一下程式碼,這樣才能有所學習到





2013/07/09

C#.Net Kinect 改變上下傾角

Kinect有內建一個馬達可以用來改變上下的傾角,Kinect可以轉動的傾角大概±27°
可以透過KinectSensor的ElevationAngle去取得到傾角角度,也可以透過該屬性去改變傾角角度
但官方有建議不要太頻繁的去轉動,有可能會造成馬達損毀

如果有想要左右移動的開發者可以自行安裝馬達,利用arduino或8051等晶片去控制


XAML:
<Window x:Class="KinectAdjustTiltAngle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="700" Width="646" Loaded="Window_Loaded" Unloaded="Window_Unloaded">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="530*" />
            <ColumnDefinition Width="94*" />
        </Grid.ColumnDefinitions>
        <Image Name="image"  Height="640" Width="480" Margin="12,20,38,0"></Image>
        <Slider Height="629" Margin="513,20,81,0" Name="sliderKinectAngle" VerticalAlignment="Top" Minimum="-27" Maximum="27" Orientation="Vertical" Grid.ColumnSpan="2" PreviewMouseUp="sliderKinectAngle_PreviewMouseUp" />
        <Label Name="labelCurrentAngle"  Content="目前角度" Height="28" HorizontalAlignment="Left" Margin="19,323,0,0"  VerticalAlignment="Top" Grid.Column="1" />
    </Grid>
</Window>


2013/07/08

C#.Net 擷取Kinect影像


這個專案用於儲存Kinect影像,每秒都儲存一張照片在使用者的圖片資料夾裏頭
如需要儲存更多張影像的使用者可以自行修正執行續休眠時間
或用其他方式去控制,為了避免硬碟故障,所以我就一秒只儲存一張XD

<Window x:Class="Kinect_Storage_Images.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Kinect Storage Images" Height="50" Width="150" Loaded="Window_Loaded" Unloaded="Window_Unloaded">
</Window>


2013/05/12

C#.Net KINECT 結合影像與骨架

我把『C#.Net 使用WPF開發KINECT』與『C#.Net 分析KINECT追蹤骨架』所學到的東西做一個整合。
也就是將骨架結合影像,順便來衝篇數XDDD

結合後可以幹嗎?就可以用一把光劍啊,或者讓使用者測試衣服是否符合自己的需求等等的功能

C#.Net 分析KINECT追蹤骨架

目前可以追蹤的骨架如下圖,約20個

KINECT的骨架偵測點主要是以SPINE這個點為主,偵測到這個點才會開始判斷其他點的位置


距離太近就有可能會出現判斷方向相反,在測試時就有可能發生左腳被偵測到右腳去了,右腳跑來左腳的位置…


2013/05/11

C#.Net 使用WPF開發KINECT

決定幫學長寫這個程式了,也是因為國科會案子的展示大致上今年應該OK了,剩下明年展完也差不多要畢業了。

2009年6月,我知道這個東西後其實還蠻感興趣的。約2010就有推出Develop的網站,有分32bit和64bit的版本,親戚有購買XBOX360用的KINECT,不過不好意思跟親戚借來開發程式,後來就做罷了。其實我有偷偷拿來玩過XDDD


不過當初還有研究相關KINECT架構以及人體關節,也就是因為這樣對於影像辨識這塊非常的感興趣,不過技術技術說破就不值錢了。

當初最夯的Souce code應該是光劍吧?


微軟也推出切水果的一個遊戲,當初還蠻轟動的。