├── Screenshot.png ├── Kinect.Server ├── packages.config ├── Mode.cs ├── Constants.cs ├── Properties │ └── AssemblyInfo.cs ├── FrameSerializer.cs ├── ColorSerializer.cs ├── DepthSerializer.cs ├── Kinect.Server.csproj ├── Program.cs └── SkeletonSerializer.cs ├── Kinect.Client ├── Web.config ├── style.css ├── index.html ├── Web.Debug.config ├── Web.Release.config ├── Properties │ └── AssemblyInfo.cs ├── web-sockets.js └── Kinect.Client.csproj ├── Packages.dgml ├── README.md ├── .gitignore ├── KinectHtml5.sln ├── .gitattributes └── LICENSE /Screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LightBuzz/Kinect-HTML5/HEAD/Screenshot.png -------------------------------------------------------------------------------- /Kinect.Server/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Kinect.Server/Mode.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Kinect.Server 7 | { 8 | public enum Mode 9 | { 10 | Color, 11 | Depth 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Kinect.Client/Web.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Kinect.Client/style.css: -------------------------------------------------------------------------------- 1 | html, body 2 | { 3 | background: #eee; 4 | font-family: Segoe UI, Helvetica Neue, Arial, Sans-Serif; 5 | font-size: 1em; 6 | margin: 0; 7 | padding: 0; 8 | } 9 | 10 | body 11 | { 12 | margin: 20px auto; 13 | width: 640px; 14 | } 15 | 16 | h1 17 | { 18 | font-family: Segoe UI Light, Segoe UI, Helvetica Neue, Arial, Sans-Serif; 19 | font-weight: normal; 20 | } 21 | 22 | #canvas 23 | { 24 | box-shadow: 0px 0px 10px #ccc; 25 | margin: 20px 0px; 26 | } -------------------------------------------------------------------------------- /Kinect.Client/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Kinect & HTML5 5 | 6 | 7 | 8 | 9 |

Kinect & HTML5 WebSockets

10 | Status: 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /Packages.dgml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 14 | 15 | -------------------------------------------------------------------------------- /Kinect.Client/Web.Debug.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 17 | 18 | 29 | 30 | -------------------------------------------------------------------------------- /Kinect.Client/Web.Release.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 17 | 18 | 19 | 30 | 31 | -------------------------------------------------------------------------------- /Kinect.Server/Constants.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Windows.Media; 6 | 7 | namespace Kinect.Server 8 | { 9 | class Constants 10 | { 11 | /// 12 | /// Maximmum depth distance. 13 | /// 14 | public static readonly float MAX_DEPTH_DISTANCE = 4095; 15 | 16 | /// 17 | /// Minimum depth distance. 18 | /// 19 | public static readonly float MIN_DEPTH_DISTANCE = 850; 20 | 21 | /// 22 | /// DPI. 23 | /// 24 | public static readonly double DPI = 96.0; 25 | 26 | /// 27 | /// Maximum depth distance offset. 28 | /// 29 | public static readonly float MAX_DEPTH_DISTANCE_OFFSET = MAX_DEPTH_DISTANCE - MIN_DEPTH_DISTANCE; 30 | 31 | /// 32 | /// Default name for temporary color files. 33 | /// 34 | public static readonly string CAPTURE_FILE_COLOR = "Capture_Color.jpg"; 35 | 36 | /// 37 | /// Default name for temporary depth files. 38 | /// 39 | public static readonly string CAPTURE_FILE_DEPTH = "Capture_Depth.jpg"; 40 | 41 | /// 42 | /// The pixel format. 43 | /// 44 | public static readonly PixelFormat PIXEL_FORMAT = PixelFormats.Bgra32; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Kinect-HTML5 2 | ============ 3 | 4 | Display Kinect data on an HTML5 canvas using WebSockets. 5 | 6 | * Color frames 7 | * Depth frames 8 | * Body skeletons 9 | 10 | ![Displaying Kinect skeleton data on an HTML5 canvas](https://raw.github.com/LightBuzz/Kinect-HTML5/master/Screenshot.png) 11 | 12 | Description 13 | --- 14 | This project connects a Kinect-enabled application to an HTML5 web page and displays the users' skeletons. 15 | 16 | The application acts as a WebSocket server, transmitting new skeleton data whenever Kinect frames are available. The web page uses WebSockets to get the Kinect data and display them on a canvas. 17 | 18 | Prerequisites 19 | --- 20 | * [Kinect for Windows](http://amzn.to/1k7rquZ) or [Kinect for XBOX](http://amzn.to/1dO0R0s) sensor 21 | * [Kinect for Windows SDK v1.8](http://go.microsoft.com/fwlink/?LinkID=323588) 22 | 23 | WebSockets 24 | --- 25 | Read more about WebSockets in the book [Getting Started with HTML5 WebSocket Programming, by Vangos Pterneas](http://amzn.to/19cvMj9). 26 | 27 | Credits 28 | --- 29 | * Developed by [Vangos Pterneas](http://pterneas.com) for [LightBuzz](http://lightbuzz.com) 30 | * The WebSocket server application uses [Fleck, by Jason Staten](https://github.com/statianzo/Fleck) 31 | 32 | License 33 | --- 34 | You are free to use these libraries in personal and commercial projects by attributing the original creator of Vitruvius. Licensed under [Apache v2 License](https://github.com/LightBuzz/Kinect-HTML5/blob/master/LICENSE). 35 | -------------------------------------------------------------------------------- /Kinect.Client/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Kinect.Client")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Microsoft")] 12 | [assembly: AssemblyProduct("Kinect.Client")] 13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2011")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("2c57bf22-4e9b-4ad3-8862-5a1a0df640df")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Revision and Build Numbers 33 | // by using the '*' as shown below: 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /Kinect.Server/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Kinect.Server")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Microsoft")] 12 | [assembly: AssemblyProduct("Kinect.Server")] 13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2011")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("3b1e6c3f-3f78-43b8-a251-1aa34cbd50bf")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Kinect.Server/FrameSerializer.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Kinect; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Windows; 8 | using System.Windows.Media; 9 | using System.Windows.Media.Imaging; 10 | 11 | namespace Kinect.Server 12 | { 13 | /// 14 | /// Converts a Kinect frame into an HTML5 blob. 15 | /// 16 | public static class FrameSerializer 17 | { 18 | /// 19 | /// Converts a WriteableBitmap into a byte array. 20 | /// 21 | /// The specified bitmap. 22 | /// The specified temporary file. 23 | /// A binary representation of the bitmap. 24 | public static byte[] CreateBlob(WriteableBitmap bitmap, string file) 25 | { 26 | // Save bitmap. 27 | BitmapEncoder encoder = new JpegBitmapEncoder(); 28 | 29 | encoder.Frames.Add(BitmapFrame.Create(bitmap as BitmapSource)); 30 | 31 | using (var stream = new FileStream(file, FileMode.Create)) 32 | { 33 | encoder.Save(stream); 34 | } 35 | 36 | // Convert saved bitmap to blob. 37 | using (FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) 38 | { 39 | using (BinaryReader reader = new BinaryReader(stream)) 40 | { 41 | return reader.ReadBytes((int)stream.Length); 42 | } 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Build Folders (you can keep bin if you'd like, to store dlls and pdbs) 2 | [Bb]in/ 3 | [Oo]bj/ 4 | 5 | # mstest test results 6 | TestResults 7 | 8 | ## Ignore Visual Studio temporary files, build results, and 9 | ## files generated by popular Visual Studio add-ons. 10 | 11 | # User-specific files 12 | *.suo 13 | *.user 14 | *.sln.docstates 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Rr]elease/ 19 | x64/ 20 | *_i.c 21 | *_p.c 22 | *.ilk 23 | *.meta 24 | *.obj 25 | *.pch 26 | *.pdb 27 | *.pgc 28 | *.pgd 29 | *.rsp 30 | *.sbr 31 | *.tlb 32 | *.tli 33 | *.tlh 34 | *.tmp 35 | *.log 36 | *.vspscc 37 | *.vssscc 38 | .builds 39 | 40 | # Visual C++ cache files 41 | ipch/ 42 | *.aps 43 | *.ncb 44 | *.opensdf 45 | *.sdf 46 | 47 | # Visual Studio profiler 48 | *.psess 49 | *.vsp 50 | *.vspx 51 | 52 | # Guidance Automation Toolkit 53 | *.gpState 54 | 55 | # ReSharper is a .NET coding add-in 56 | _ReSharper* 57 | 58 | # NCrunch 59 | *.ncrunch* 60 | .*crunch*.local.xml 61 | 62 | # Installshield output folder 63 | [Ee]xpress 64 | 65 | # DocProject is a documentation generator add-in 66 | DocProject/buildhelp/ 67 | DocProject/Help/*.HxT 68 | DocProject/Help/*.HxC 69 | DocProject/Help/*.hhc 70 | DocProject/Help/*.hhk 71 | DocProject/Help/*.hhp 72 | DocProject/Help/Html2 73 | DocProject/Help/html 74 | 75 | # Click-Once directory 76 | publish 77 | 78 | # Publish Web Output 79 | *.Publish.xml 80 | 81 | # NuGet Packages Directory 82 | packages 83 | 84 | # Windows Azure Build Output 85 | csx 86 | *.build.csdef 87 | 88 | # Windows Store app package directory 89 | AppPackages/ 90 | 91 | # Others 92 | [Bb]in 93 | [Oo]bj 94 | sql 95 | TestResults 96 | [Tt]est[Rr]esult* 97 | *.Cache 98 | ClientBin 99 | [Ss]tyle[Cc]op.* 100 | ~$* 101 | *.dbmdl 102 | Generated_Code #added for RIA/Silverlight projects 103 | 104 | # Backup & report files from converting an old project file to a newer 105 | # Visual Studio version. Backup files are not needed, because we have git ;-) 106 | _UpgradeReport_Files/ 107 | Backup*/ 108 | UpgradeLog*.XML 109 | -------------------------------------------------------------------------------- /Kinect.Server/ColorSerializer.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Kinect; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Windows; 7 | using System.Windows.Media; 8 | using System.Windows.Media.Imaging; 9 | 10 | namespace Kinect.Server 11 | { 12 | /// 13 | /// Handles color frame serialization. 14 | /// 15 | public static class ColorSerializer 16 | { 17 | /// 18 | /// The color bitmap source. 19 | /// 20 | static WriteableBitmap _colorBitmap = null; 21 | 22 | /// 23 | /// The RGB pixel values. 24 | /// 25 | static byte[] _colorPixels = null; 26 | 27 | /// 28 | /// Color frame width. 29 | /// 30 | static int _colorWidth; 31 | 32 | /// 33 | /// Color frame height. 34 | /// 35 | static int _colorHeight; 36 | 37 | /// 38 | /// Color frame stride. 39 | /// 40 | static int _colorStride; 41 | 42 | /// 43 | /// Serializes a color frame. 44 | /// 45 | /// The specified color frame. 46 | /// A binary representation of the frame. 47 | public static byte[] Serialize(this ColorImageFrame frame) 48 | { 49 | if (_colorBitmap == null) 50 | { 51 | _colorWidth = frame.Width; 52 | _colorHeight = frame.Height; 53 | _colorStride = _colorWidth * Constants.PIXEL_FORMAT.BitsPerPixel / 8; 54 | _colorPixels = new byte[frame.PixelDataLength]; 55 | _colorBitmap = new WriteableBitmap(_colorWidth, _colorHeight, Constants.DPI, Constants.DPI, Constants.PIXEL_FORMAT, null); 56 | } 57 | 58 | frame.CopyPixelDataTo(_colorPixels); 59 | 60 | _colorBitmap.WritePixels(new Int32Rect(0, 0, _colorWidth, _colorHeight), _colorPixels, _colorStride, 0); 61 | 62 | return FrameSerializer.CreateBlob(_colorBitmap, Constants.CAPTURE_FILE_COLOR); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /KinectHtml5.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kinect.Server", "Kinect.Server\Kinect.Server.csproj", "{635FB54B-3DD0-453F-BC0B-67A754A2E5DE}" 5 | EndProject 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kinect.Client", "Kinect.Client\Kinect.Client.csproj", "{0F825F8E-0D13-453E-B97B-E91D4EED5AB3}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Debug|Mixed Platforms = Debug|Mixed Platforms 12 | Debug|x86 = Debug|x86 13 | Release|Any CPU = Release|Any CPU 14 | Release|Mixed Platforms = Release|Mixed Platforms 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {635FB54B-3DD0-453F-BC0B-67A754A2E5DE}.Debug|Any CPU.ActiveCfg = Debug|x86 19 | {635FB54B-3DD0-453F-BC0B-67A754A2E5DE}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 20 | {635FB54B-3DD0-453F-BC0B-67A754A2E5DE}.Debug|Mixed Platforms.Build.0 = Debug|x86 21 | {635FB54B-3DD0-453F-BC0B-67A754A2E5DE}.Debug|x86.ActiveCfg = Debug|x86 22 | {635FB54B-3DD0-453F-BC0B-67A754A2E5DE}.Debug|x86.Build.0 = Debug|x86 23 | {635FB54B-3DD0-453F-BC0B-67A754A2E5DE}.Release|Any CPU.ActiveCfg = Release|x86 24 | {635FB54B-3DD0-453F-BC0B-67A754A2E5DE}.Release|Mixed Platforms.ActiveCfg = Release|x86 25 | {635FB54B-3DD0-453F-BC0B-67A754A2E5DE}.Release|Mixed Platforms.Build.0 = Release|x86 26 | {635FB54B-3DD0-453F-BC0B-67A754A2E5DE}.Release|x86.ActiveCfg = Release|x86 27 | {635FB54B-3DD0-453F-BC0B-67A754A2E5DE}.Release|x86.Build.0 = Release|x86 28 | {0F825F8E-0D13-453E-B97B-E91D4EED5AB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {0F825F8E-0D13-453E-B97B-E91D4EED5AB3}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {0F825F8E-0D13-453E-B97B-E91D4EED5AB3}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU 31 | {0F825F8E-0D13-453E-B97B-E91D4EED5AB3}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU 32 | {0F825F8E-0D13-453E-B97B-E91D4EED5AB3}.Debug|x86.ActiveCfg = Debug|Any CPU 33 | {0F825F8E-0D13-453E-B97B-E91D4EED5AB3}.Release|Any CPU.ActiveCfg = Release|Any CPU 34 | {0F825F8E-0D13-453E-B97B-E91D4EED5AB3}.Release|Any CPU.Build.0 = Release|Any CPU 35 | {0F825F8E-0D13-453E-B97B-E91D4EED5AB3}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU 36 | {0F825F8E-0D13-453E-B97B-E91D4EED5AB3}.Release|Mixed Platforms.Build.0 = Release|Any CPU 37 | {0F825F8E-0D13-453E-B97B-E91D4EED5AB3}.Release|x86.ActiveCfg = Release|Any CPU 38 | EndGlobalSection 39 | GlobalSection(SolutionProperties) = preSolution 40 | HideSolutionNode = FALSE 41 | EndGlobalSection 42 | EndGlobal 43 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /Kinect.Client/web-sockets.js: -------------------------------------------------------------------------------- 1 | window.onload = function () { 2 | var status = document.getElementById("status"); 3 | var canvas = document.getElementById("canvas"); 4 | var buttonColor = document.getElementById("color"); 5 | var buttonDepth = document.getElementById("depth"); 6 | var context = canvas.getContext("2d"); 7 | 8 | var camera = new Image(); 9 | 10 | camera.onload = function () { 11 | context.drawImage(camera, 0, 0); 12 | } 13 | 14 | if (!window.WebSocket) { 15 | status.innerHTML = "Your browser does not support web sockets!"; 16 | return; 17 | } 18 | 19 | status.innerHTML = "Connecting to server..."; 20 | 21 | // Initialize a new web socket. 22 | var socket = new WebSocket("ws://localhost:8181"); 23 | 24 | // Connection established. 25 | socket.onopen = function () { 26 | status.innerHTML = "Connection successful."; 27 | }; 28 | 29 | // Connection closed. 30 | socket.onclose = function () { 31 | status.innerHTML = "Connection closed."; 32 | } 33 | 34 | // Receive data FROM the server! 35 | socket.onmessage = function (event) { 36 | if (typeof event.data === "string") { 37 | // SKELETON DATA 38 | 39 | // Get the data in JSON format. 40 | var jsonObject = JSON.parse(event.data); 41 | 42 | // Display the skeleton joints. 43 | for (var i = 0; i < jsonObject.skeletons.length; i++) { 44 | for (var j = 0; j < jsonObject.skeletons[i].joints.length; j++) { 45 | var joint = jsonObject.skeletons[i].joints[j]; 46 | 47 | // Draw!!! 48 | context.fillStyle = "#FF0000"; 49 | context.beginPath(); 50 | context.arc(joint.x, joint.y, 10, 0, Math.PI * 2, true); 51 | context.closePath(); 52 | context.fill(); 53 | } 54 | } 55 | } 56 | else if (event.data instanceof Blob) { 57 | // RGB FRAME DATA 58 | // 1. Get the raw data. 59 | var blob = event.data; 60 | 61 | // 2. Create a new URL for the blob object. 62 | window.URL = window.URL || window.webkitURL; 63 | 64 | var source = window.URL.createObjectURL(blob); 65 | 66 | // 3. Update the image source. 67 | camera.src = source; 68 | 69 | // 4. Release the allocated memory. 70 | window.URL.revokeObjectURL(source); 71 | } 72 | }; 73 | 74 | buttonColor.onclick = function () { 75 | socket.send("Color"); 76 | } 77 | 78 | buttonDepth.onclick = function () { 79 | socket.send("Depth"); 80 | } 81 | }; -------------------------------------------------------------------------------- /Kinect.Server/DepthSerializer.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Kinect; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Windows; 7 | using System.Windows.Media.Imaging; 8 | 9 | namespace Kinect.Server 10 | { 11 | /// 12 | /// Handles depth frame serialization. 13 | /// 14 | public static class DepthSerializer 15 | { 16 | /// 17 | /// The depth bitmap source. 18 | /// 19 | static WriteableBitmap _depthBitmap = null; 20 | 21 | /// 22 | /// The RGB depth values. 23 | /// 24 | static byte[] _depthPixels = null; 25 | 26 | /// 27 | /// Depth frame width. 28 | /// 29 | static int _depthWidth; 30 | 31 | /// 32 | /// Depth frame height. 33 | /// 34 | static int _depthHeight; 35 | 36 | /// 37 | /// Depth frame stride. 38 | /// 39 | static int _depthStride; 40 | 41 | /// 42 | /// The actual depth values. 43 | /// 44 | static short[] _depthData = null; 45 | 46 | /// 47 | /// Serializes a depth frame. 48 | /// 49 | /// The specified depth frame. 50 | /// A binary representation of the frame. 51 | public static byte[] Serialize(this DepthImageFrame frame) 52 | { 53 | if (_depthBitmap == null) 54 | { 55 | _depthWidth = frame.Width; 56 | _depthHeight = frame.Height; 57 | _depthStride = _depthWidth * Constants.PIXEL_FORMAT.BitsPerPixel / 8; 58 | _depthData = new short[frame.PixelDataLength]; 59 | _depthPixels = new byte[_depthHeight * _depthWidth * 4]; 60 | _depthBitmap = new WriteableBitmap(_depthWidth, _depthHeight, Constants.DPI, Constants.DPI, Constants.PIXEL_FORMAT, null); 61 | } 62 | 63 | frame.CopyPixelDataTo(_depthData); 64 | 65 | for (int depthIndex = 0, colorIndex = 0; depthIndex < _depthData.Length && colorIndex < _depthPixels.Length; depthIndex++, colorIndex += 4) 66 | { 67 | // Get the depth value. 68 | int depth = _depthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; 69 | 70 | // Equal coloring for monochromatic histogram. 71 | byte intensity = (byte)(255 - (255 * Math.Max(depth - Constants.MIN_DEPTH_DISTANCE, 0) / (Constants.MAX_DEPTH_DISTANCE_OFFSET))); 72 | 73 | _depthPixels[colorIndex + 0] = intensity; 74 | _depthPixels[colorIndex + 1] = intensity; 75 | _depthPixels[colorIndex + 2] = intensity; 76 | } 77 | 78 | _depthBitmap.WritePixels(new Int32Rect(0, 0, _depthWidth, _depthHeight), _depthPixels, _depthStride, 0); 79 | 80 | return FrameSerializer.CreateBlob(_depthBitmap, Constants.CAPTURE_FILE_DEPTH); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Kinect.Server/Kinect.Server.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {635FB54B-3DD0-453F-BC0B-67A754A2E5DE} 9 | Exe 10 | Properties 11 | Kinect.Server 12 | Kinect.Server 13 | v4.0 14 | Client 15 | 512 16 | 17 | 18 | x86 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | x86 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | False 39 | ..\packages\Fleck.0.9.8.25\lib\net40\Fleck.dll 40 | 41 | 42 | False 43 | Libs\Microsoft.Kinect.dll 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 78 | -------------------------------------------------------------------------------- /Kinect.Server/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Fleck; 6 | using Microsoft.Kinect; 7 | 8 | namespace Kinect.Server 9 | { 10 | class Program 11 | { 12 | static List _clients = new List(); 13 | 14 | static Skeleton[] _skeletons = new Skeleton[6]; 15 | 16 | static Mode _mode = Mode.Color; 17 | 18 | static CoordinateMapper _coordinateMapper; 19 | 20 | static void Main(string[] args) 21 | { 22 | InitializeConnection(); 23 | InitilizeKinect(); 24 | 25 | Console.ReadLine(); 26 | } 27 | 28 | private static void InitializeConnection() 29 | { 30 | var server = new WebSocketServer("ws://localhost:8181"); 31 | 32 | server.Start(socket => 33 | { 34 | socket.OnOpen = () => 35 | { 36 | _clients.Add(socket); 37 | }; 38 | 39 | socket.OnClose = () => 40 | { 41 | _clients.Remove(socket); 42 | }; 43 | 44 | socket.OnMessage = message => 45 | { 46 | switch (message) 47 | { 48 | case "Color": 49 | _mode = Mode.Color; 50 | break; 51 | case "Depth": 52 | _mode = Mode.Depth; 53 | break; 54 | default: 55 | break; 56 | } 57 | 58 | Console.WriteLine("Switched to " + message); 59 | }; 60 | }); 61 | } 62 | 63 | private static void InitilizeKinect() 64 | { 65 | var sensor = KinectSensor.KinectSensors.SingleOrDefault(); 66 | 67 | if (sensor != null) 68 | { 69 | sensor.ColorStream.Enable(); 70 | sensor.DepthStream.Enable(); 71 | sensor.SkeletonStream.Enable(); 72 | 73 | sensor.AllFramesReady += Sensor_AllFramesReady; 74 | 75 | _coordinateMapper = sensor.CoordinateMapper; 76 | 77 | sensor.Start(); 78 | } 79 | } 80 | 81 | static void Sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e) 82 | { 83 | using (var frame = e.OpenColorImageFrame()) 84 | { 85 | if (frame != null) 86 | { 87 | if (_mode == Mode.Color) 88 | { 89 | var blob = frame.Serialize(); 90 | 91 | foreach (var socket in _clients) 92 | { 93 | socket.Send(blob); 94 | } 95 | } 96 | } 97 | } 98 | 99 | using (var frame = e.OpenDepthImageFrame()) 100 | { 101 | if (frame != null) 102 | { 103 | if (_mode == Mode.Depth) 104 | { 105 | var blob = frame.Serialize(); 106 | 107 | foreach (var socket in _clients) 108 | { 109 | socket.Send(blob); 110 | } 111 | } 112 | } 113 | } 114 | 115 | using (var frame = e.OpenSkeletonFrame()) 116 | { 117 | if (frame != null) 118 | { 119 | frame.CopySkeletonDataTo(_skeletons); 120 | 121 | var users = _skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).ToList(); 122 | 123 | if (users.Count > 0) 124 | { 125 | string json = users.Serialize(_coordinateMapper, _mode); 126 | 127 | foreach (var socket in _clients) 128 | { 129 | socket.Send(json); 130 | } 131 | } 132 | } 133 | } 134 | } 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /Kinect.Server/SkeletonSerializer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.IO; 5 | using System.Text; 6 | using System.Runtime.Serialization; 7 | using System.Runtime.Serialization.Json; 8 | using Microsoft.Kinect; 9 | using System.Windows; 10 | 11 | namespace Kinect.Server 12 | { 13 | /// 14 | /// Serializes a Kinect skeleton to JSON fromat. 15 | /// 16 | public static class SkeletonSerializer 17 | { 18 | [DataContract] 19 | class JSONSkeletonCollection 20 | { 21 | [DataMember(Name = "skeletons")] 22 | public List Skeletons { get; set; } 23 | } 24 | 25 | [DataContract] 26 | class JSONSkeleton 27 | { 28 | [DataMember(Name = "id")] 29 | public string ID { get; set; } 30 | 31 | [DataMember(Name = "joints")] 32 | public List Joints { get; set; } 33 | } 34 | 35 | [DataContract] 36 | class JSONJoint 37 | { 38 | [DataMember(Name = "name")] 39 | public string Name { get; set; } 40 | 41 | [DataMember(Name = "x")] 42 | public double X { get; set; } 43 | 44 | [DataMember(Name = "y")] 45 | public double Y { get; set; } 46 | 47 | [DataMember(Name = "z")] 48 | public double Z { get; set; } 49 | } 50 | 51 | /// 52 | /// Serializes an array of Kinect skeletons into an array of JSON skeletons. 53 | /// 54 | /// The Kinect skeletons. 55 | /// The coordinate mapper. 56 | /// Mode (color or depth). 57 | /// A JSON representation of the skeletons. 58 | public static string Serialize(this List skeletons, CoordinateMapper mapper, Mode mode) 59 | { 60 | JSONSkeletonCollection jsonSkeletons = new JSONSkeletonCollection { Skeletons = new List() }; 61 | 62 | foreach (var skeleton in skeletons) 63 | { 64 | JSONSkeleton jsonSkeleton = new JSONSkeleton 65 | { 66 | ID = skeleton.TrackingId.ToString(), 67 | Joints = new List() 68 | }; 69 | 70 | foreach (Joint joint in skeleton.Joints) 71 | { 72 | Point point = new Point(); 73 | 74 | switch (mode) 75 | { 76 | case Mode.Color: 77 | ColorImagePoint colorPoint = mapper.MapSkeletonPointToColorPoint(joint.Position, ColorImageFormat.RgbResolution640x480Fps30); 78 | point.X = colorPoint.X; 79 | point.Y = colorPoint.Y; 80 | break; 81 | case Mode.Depth: 82 | DepthImagePoint depthPoint = mapper.MapSkeletonPointToDepthPoint(joint.Position, DepthImageFormat.Resolution640x480Fps30); 83 | point.X = depthPoint.X; 84 | point.Y = depthPoint.Y; 85 | break; 86 | default: 87 | break; 88 | } 89 | 90 | jsonSkeleton.Joints.Add(new JSONJoint 91 | { 92 | Name = joint.JointType.ToString().ToLower(), 93 | X = point.X, 94 | Y = point.Y, 95 | Z = joint.Position.Z 96 | }); 97 | } 98 | 99 | jsonSkeletons.Skeletons.Add(jsonSkeleton); 100 | } 101 | 102 | return Serialize(jsonSkeletons); 103 | } 104 | 105 | /// 106 | /// Serializes an object to JSON. 107 | /// 108 | /// The specified object. 109 | /// A JSON representation of the object. 110 | private static string Serialize(object obj) 111 | { 112 | DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); 113 | 114 | using (MemoryStream ms = new MemoryStream()) 115 | { 116 | serializer.WriteObject(ms, obj); 117 | 118 | return Encoding.Default.GetString(ms.ToArray()); 119 | } 120 | } 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /Kinect.Client/Kinect.Client.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 8 | 9 | 2.0 10 | {0F825F8E-0D13-453E-B97B-E91D4EED5AB3} 11 | {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} 12 | Library 13 | Properties 14 | Kinect.Client 15 | Kinect.Client 16 | v4.0 17 | false 18 | 19 | 20 | 21 | 22 | 4.0 23 | 24 | 25 | true 26 | full 27 | false 28 | bin\ 29 | DEBUG;TRACE 30 | prompt 31 | 4 32 | 33 | 34 | pdbonly 35 | true 36 | bin\ 37 | TRACE 38 | prompt 39 | 4 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | Web.config 66 | 67 | 68 | Web.config 69 | 70 | 71 | 72 | 73 | 74 | 75 | 10.0 76 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | False 86 | True 87 | 11636 88 | / 89 | 90 | 91 | False 92 | False 93 | 94 | 95 | False 96 | 97 | 98 | 99 | 100 | 107 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------