Last active
August 29, 2015 14:11
-
-
Save Thorium/b1dff8526d407ff43597 to your computer and use it in GitHub Desktop.
Microsoft Kinect Body Basics with Kinect SDK 2.0 and F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Microsoft Kinect Body Basics with Kinect SDK 2.0 and F# | |
#if INTERACTIVE | |
#r @"..\packages\Microsoft.Kinect.2.0.1410.19000\lib\net45\Microsoft.Kinect.dll" | |
#else | |
module Kinect20 | |
#endif | |
open Microsoft.Kinect | |
open System.Collections.Generic | |
let bodyFrameReader = | |
let kinectSensor = KinectSensor.GetDefault() | |
kinectSensor.IsAvailableChanged |> Event.add(fun _ -> | |
match kinectSensor.IsAvailable with | |
| true -> printfn "sensor available" | |
| false -> printfn "sensor disconnected") | |
kinectSensor.Open() | |
kinectSensor.BodyFrameSource.OpenReader() | |
let stop() = bodyFrameReader.Dispose() | |
let mutable bodyContainer = None // mutable API :-( | |
let bodyTracking = | |
bodyFrameReader.FrameArrived | |
|> Event.add( | |
fun bfae -> | |
use bodyFrame = bfae.FrameReference.AcquireFrame() | |
match bodyFrame<>null && bodyFrame.BodyCount > 0 with | |
| false -> () | |
| true -> | |
if bodyContainer = None then | |
bodyContainer <- Some(Array.create bodyFrame.BodyCount (Unchecked.defaultof<Body>)) | |
match bodyContainer with | |
| None -> () | |
| Some bodies -> //printfn "bc: %i bf: %i" bodies.Length bodyFrame.BodyCount | |
bodyFrame.GetAndRefreshBodyData(bodies) | |
let parts = bodies |> Seq.filter (fun b -> b.IsTracked) | |
parts | |
|> Seq.map (fun b -> b.Joints) | |
|> Seq.iter(fun j -> j |> Seq.iter(fun p -> | |
let part = p.Key.ToString("F") | |
let pos = p.Value.Position | |
printfn "Bodypart %s at %f %f %f" part pos.X pos.Y pos.Z)) | |
// parts | |
// |> Seq.filter(fun b -> b.HandLeftState = HandState.Lasso) | |
// |> Seq.iter(fun i -> printfn "Lasso!") | |
) | |
//item SpineShoulder at -0.474309 -0.082094 1.528226 | |
//item HandTipLeft at -0.355548 0.007269 1.494294 | |
//item ThumbLeft at -0.425318 -0.049197 1.453333 | |
//item HandTipRight at -0.291631 0.188657 1.813159 | |
//item ThumbRight at -0.320072 0.151973 1.688667 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment