7 people like it.

Microsoft Kinect Body Basics with Kinect SDK 2.0 and F#

Microsoft Kinect Body Basics with Kinect SDK 2.0 and F-Sharp

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
#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!")            
        )
namespace Microsoft
namespace System
namespace System.Collections
namespace System.Collections.Generic
val bodyFrameReader : obj

Full name: Script.bodyFrameReader
val kinectSensor : obj
Multiple items
module Event

from Microsoft.FSharp.Control

--------------------
type Event<'T> =
  new : unit -> Event<'T>
  member Trigger : arg:'T -> unit
  member Publish : IEvent<'T>

Full name: Microsoft.FSharp.Control.Event<_>

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
  new : unit -> Event<'Delegate,'Args>
  member Trigger : sender:obj * args:'Args -> unit
  member Publish : IEvent<'Delegate,'Args>

Full name: Microsoft.FSharp.Control.Event<_,_>

--------------------
new : unit -> Event<'T>

--------------------
new : unit -> Event<'Delegate,'Args>
val add : callback:('T -> unit) -> sourceEvent:IEvent<'Del,'T> -> unit (requires delegate and 'Del :> System.Delegate)

Full name: Microsoft.FSharp.Control.Event.add
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val stop : unit -> 'a

Full name: Script.stop
val mutable bodyContainer : 'a [] option (requires equality)

Full name: Script.bodyContainer
union case Option.None: Option<'T>
val bodyTracking : unit

Full name: Script.bodyTracking
val bfae : 'a
val bodyFrame : 'a (requires equality and 'a : null and 'a :> System.IDisposable)
union case Option.Some: Value: 'T -> Option<'T>
module Array

from Microsoft.FSharp.Collections
val create : count:int -> value:'T -> 'T []

Full name: Microsoft.FSharp.Collections.Array.create
module Unchecked

from Microsoft.FSharp.Core.Operators
val defaultof<'T> : 'T

Full name: Microsoft.FSharp.Core.Operators.Unchecked.defaultof
val bodies : 'a [] (requires equality)
val parts : seq<'a> (requires equality)
module Seq

from Microsoft.FSharp.Collections
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val b : 'a (requires equality)
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val j : 'a (requires 'a :> seq<'b>)
val p : 'a
val part : string
val pos : 'a
Raw view Test code New version

More information

Link:http://fssnip.net/oU
Posted:9 years ago
Author:Tuomas Hietanen
Tags: kinect