{"id":1085,"date":"2012-08-29T00:03:55","date_gmt":"2012-08-28T16:03:55","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1085"},"modified":"2012-08-29T00:10:40","modified_gmt":"2012-08-28T16:10:40","slug":"kinect-for-windows-in-processing-4","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2012\/08\/29\/kinect-for-windows-in-processing-4\/","title":{"rendered":"Kinect for Windows in Processing 4"},"content":{"rendered":"<p>This is the new version of the Kinect for Processing library using the <a href=\"http:\/\/www.microsoft.com\/en-us\/kinectforwindows\/develop\/developer-downloads.aspx\">Kinect for Windows 1.5 SDK<\/a>. In this version, I only implement the skeleton tracking, without the previous RGB image and depth image yet. For all the field, method and constant names, I try to use the same ones as in the SDK. It can have multiple skeletons and comes with three events:<\/p>\n<ul>\n<li>appear (new skeleton enters the screen)<\/li>\n<li>disappear (existing skeleton leaves the screen)<\/li>\n<li>move (existing skeleton stay in the screen)<\/li>\n<\/ul>\n<p>&nbsp;<br \/>\n<iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"http:\/\/www.youtube.com\/embed\/l058fnGf-u0?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><br \/>\nHere is the example Processing code.<\/p>\n<pre lang=\"java\">\r\nimport pKinect.PKinect;\r\n\r\nPKinect kinect;\r\nPFont font;\r\nArrayList<SkeletonData> bodies;\r\n\r\nvoid setup()\r\n{\r\n  size(640, 480);\r\n  background(0);\r\n  kinect = new PKinect(this);\r\n  bodies = new ArrayList<SkeletonData>();\r\n  smooth();\r\n  font = loadFont(\"LucidaSans-18.vlw\");\r\n  textFont(font, 18);\r\n  textAlign(CENTER);\r\n}\r\n\r\nvoid draw()\r\n{\r\n  fill(0, 0, 0, 16);\r\n  rect(0, 0, width, height);\r\n  for (int i=0; i<bodies.size(); i++) \r\n  {\r\n    drawSkeleton(bodies.get(i));\r\n    drawPosition(bodies.get(i));\r\n  }\r\n}\r\n\r\nvoid drawPosition(SkeletonData _s) \r\n{\r\n  noStroke();\r\n  fill(0, 255, 255);\r\n  String s1 = str(_s.dwTrackingID);\r\n  text(s1, _s.position.x*width, _s.position.y*height);\r\n}\r\n\r\nvoid drawSkeleton(SkeletonData _s) \r\n{\r\n  \/\/ Body\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_HEAD, \r\n  PKinect.NUI_SKELETON_POSITION_SHOULDER_CENTER);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_SHOULDER_CENTER, \r\n  PKinect.NUI_SKELETON_POSITION_SHOULDER_LEFT);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_SHOULDER_CENTER, \r\n  PKinect.NUI_SKELETON_POSITION_SHOULDER_RIGHT);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_SHOULDER_CENTER, \r\n  PKinect.NUI_SKELETON_POSITION_SPINE);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_SHOULDER_LEFT, \r\n  PKinect.NUI_SKELETON_POSITION_SPINE);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_SHOULDER_RIGHT, \r\n  PKinect.NUI_SKELETON_POSITION_SPINE);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_SPINE, \r\n  PKinect.NUI_SKELETON_POSITION_HIP_CENTER);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_HIP_CENTER, \r\n  PKinect.NUI_SKELETON_POSITION_HIP_LEFT);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_HIP_CENTER, \r\n  PKinect.NUI_SKELETON_POSITION_HIP_RIGHT);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_HIP_LEFT, \r\n  PKinect.NUI_SKELETON_POSITION_HIP_RIGHT);\r\n\r\n  \/\/ Left Arm\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_SHOULDER_LEFT, \r\n  PKinect.NUI_SKELETON_POSITION_ELBOW_LEFT);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_ELBOW_LEFT, \r\n  PKinect.NUI_SKELETON_POSITION_WRIST_LEFT);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_WRIST_LEFT, \r\n  PKinect.NUI_SKELETON_POSITION_HAND_LEFT);\r\n\r\n  \/\/ Right Arm\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_SHOULDER_RIGHT, \r\n  PKinect.NUI_SKELETON_POSITION_ELBOW_RIGHT);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_ELBOW_RIGHT, \r\n  PKinect.NUI_SKELETON_POSITION_WRIST_RIGHT);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_WRIST_RIGHT, \r\n  PKinect.NUI_SKELETON_POSITION_HAND_RIGHT);\r\n\r\n  \/\/ Left Leg\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_HIP_LEFT, \r\n  PKinect.NUI_SKELETON_POSITION_KNEE_LEFT);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_KNEE_LEFT, \r\n  PKinect.NUI_SKELETON_POSITION_ANKLE_LEFT);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_ANKLE_LEFT, \r\n  PKinect.NUI_SKELETON_POSITION_FOOT_LEFT);\r\n\r\n  \/\/ Right Leg\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_HIP_RIGHT, \r\n  PKinect.NUI_SKELETON_POSITION_KNEE_RIGHT);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_KNEE_RIGHT, \r\n  PKinect.NUI_SKELETON_POSITION_ANKLE_RIGHT);\r\n  DrawBone(_s, \r\n  PKinect.NUI_SKELETON_POSITION_ANKLE_RIGHT, \r\n  PKinect.NUI_SKELETON_POSITION_FOOT_RIGHT);\r\n}\r\n\r\nvoid DrawBone(SkeletonData _s, int _j1, int _j2) \r\n{\r\n  noFill();\r\n  stroke(255, 200, 0);\r\n  if (_s.skeletonPositionTrackingState[_j1] != PKinect.NUI_SKELETON_POSITION_NOT_TRACKED &#038;&#038;\r\n    _s.skeletonPositionTrackingState[_j2] != PKinect.NUI_SKELETON_POSITION_NOT_TRACKED) {\r\n    line(_s.skeletonPositions[_j1].x*width, \r\n    _s.skeletonPositions[_j1].y*height, \r\n    _s.skeletonPositions[_j2].x*width, \r\n    _s.skeletonPositions[_j2].y*height);\r\n  }\r\n}\r\n\r\nvoid appearEvent(SkeletonData _s) \r\n{\r\n  if (_s.trackingState == PKinect.NUI_SKELETON_NOT_TRACKED) \r\n  {\r\n    return;\r\n  }\r\n  synchronized(bodies) {\r\n    bodies.add(_s);\r\n  }\r\n  println(\"appearing ...\" + _s.dwTrackingID);\r\n}\r\n\r\nvoid disappearEvent(SkeletonData _s) \r\n{\r\n  synchronized(bodies) {\r\n    for (int i=bodies.size()-1; i>=0; i--) \r\n    {\r\n      if (_s.dwTrackingID == bodies.get(i).dwTrackingID) \r\n      {\r\n        bodies.remove(i);\r\n      }\r\n    }\r\n  }\r\n  println(\"Disappearing ... \" + _s.dwTrackingID);\r\n}\r\n\r\nvoid moveEvent(SkeletonData _b, SkeletonData _a) \r\n{\r\n  if (_a.trackingState == PKinect.NUI_SKELETON_NOT_TRACKED) \r\n  {\r\n    return;\r\n  }\r\n  synchronized(bodies) {\r\n    for (int i=bodies.size()-1; i>=0; i--) \r\n    {\r\n      if (_b.dwTrackingID == bodies.get(i).dwTrackingID) \r\n      {\r\n        bodies.get(i).copy(_a);\r\n        break;\r\n      }\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<br \/>\nThe Processing code and library can be <a href=\"http:\/\/www.magicandlove.com\/software\/PKinect_Skel004.zip\">downloaded here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the new version of the Kinect for Processing library using the Kinect for Windows 1.5 SDK. In this version, I only implement the skeleton tracking, without the previous RGB image and depth image yet. For all the field, method and constant names, I try to use the same ones as in the SDK. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[40,62],"class_list":["post-1085","post","type-post","status-publish","format-standard","hentry","category-software-2","tag-kinect","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/comments?post=1085"}],"version-history":[{"count":3,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1085\/revisions"}],"predecessor-version":[{"id":1088,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1085\/revisions\/1088"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1085"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}