Friday, September 16, 2016

Delphi And Kinect...

Recently I purchased a Kinect, originally from an X-Box 360 bundle but adapted for use on a computer. I'd been looking for a low cost animation solution for my PC for sometime in hopes of replacing the laborious process of hand animating 3D characters as in this animation video I produced a year and a half ago as a promo for one of my books, The Butterfly Dragon...


FYI, the guy in the end of the video who gets pummeled by her combo move is not Wing Chun Tai Chi JKD Master Wong by the way. It's actually a mercenary character from the upcoming sequel to The Butterfly Dragon who was hired to deal with her and a the notorious Dragon Butterfly.

The tools used to produce this sequence were: Daz 3D, Lightwave 3D, Adobe Photoshop, Hitfilm 3 and Magix Music Maker 2015. The music in this animation by the way was all composed from scratch (no samples or loops) on an M-Audio KeyRig 49.

All of the animation sequence at the end of that video depicting Ai Yuanlin Ying (Heylyn Yates) as the Butterfly Dragon in action was hand animated using forward kinematics with a little help from interpolation. Forward kinematics is a slow and logical approach to animation. For instance animating a walk cycle would require first moving the left upper leg forward a bit, the left lower leg backwards a bit, the left foot down a bit while moving the right upper leg back a bit and the lower leg forward a bit... etc. All of this is done in terms of arbitrary rotations of the pivotal joints on the body of the animated character. When combining this approach to animating the scene with the camera motions as well, there were some depth ordering challenges that required the scene and each of the characters to be rendered separately from one another, with the Butterfly Dragon's wings being rendered once as a black mask and then in full color so that the whole scene could be composited correctly with the combination of the camera motion. Overall it took seven passes of rendering the characters with the scene being rendered by itself (with the shadows) and then composited in post production.

Forward kinematics adds the ability to animate by moving the end point in a series of connected joints to a specific goal. So walking with inverse kinematics might involve starting out by moving the feet of the character (the rest of the leg joints would follow according to a series of constraints) and then adjusting the rest of the legs as desired, and then proceeding a few frames ahead in time and making another set of adjustments to progress the animation cycle. Everything in between that occurs as frames in the final animation output would be interpolated by the animation software itself.

Still a laborious process and if you're working with more than one character and coordinating their actions in response to one another, then animating with either forward or inverse kinematics can become a nightmare. Especially if you physically and intuitively know what you want. The problem then is interface between you and the character or scene that you're animating and essentially that's what makes animation such a talent and skill. If you don't have that talent or skill or the time, budget and/or patience to acquire it through instruction, practice and perseverance, then there's always motion capture.

There are several setups for operating a motion capture environment ranging from marker based video capture systems to sensor based networks and finally to markerless sensorless capture systems like the Microsoft Kinect. With a Kinect you can essentially track the rotations of the joints of the human body (up to 18 of them) and in close up mode it is possible to track facial motion making it the perfect tool for a low cost motion capture rig.

The Kinect's ability to capture skeletal and facial motion data is based upon a camera, a series of sensors (infrared, depth and a seldom used accelerometer), a microphone array for directional detection of a voice or audio source and some tricky internal hardware processing all delivered through a USB 2.0 interface in the X-Box 360 version. This Kinect is can be used in development projects through it's Windows based SDK which has matured at version 1.8 and is available for direct download from Microsoft. Make sure you download the SDK and the Kinect Developer tools as well.

The more recent X-Box One Kinect ups the anti a considerable amount by increasing the resolution of skeletal tracking to 26 joints (including accurate hip and hand tracking) and the resolution of the camera itself to 30 frames per second HD resolution.

One of the added advantages of each of these inexpensive capture solutions is the fact that they can also be used as a low resolution 3D scanner. That is, you could scan your face and then load the output 3D data recorded from the sensors into a modeling program like Lightwave 3D, Maya, Houdini, Blender or any of the other modeling programs available that can import standard Alias Wavefront obj files.

Now right out of the box if you buy used on Amazon or Ebay, you're not going to be in for a $60 motion capture solution. You're going to have to do a bit of research (most of which I've taken care of for you here) and you may even have to buy some additional software depending upon what you're trying to do. Not to mention the whole issue of motion acting. My animation requirements depend on a fair level of martial arts expertise which as a martial artist myself, I am not quite qualified to provide without breaking my neck or my walls. Preferably neither. Paying for someone else's skills can be costly if you're on a budget as well, so keep in mind that if you are trying to do any physical acting for motion capture to remember safety first.

The first places that you might want to look are at Dave Brekel's work with the Kinect via his website. This industrious individual has developed a number of applications, including a couple of free applications geared towards motion capture and 3D scanning using the Kinect. I didn't have much luck getting the free applications going with my system (thanks to the difficult process of setting up OpenNI drivers and the lack of support related to them though that appears to be improving). With his pay applications there is however no such barrier as Pro Point Cloud, Pro Body and Pro Face all interface with the Kinect through it's native Windows driver and SDK which works seamlessly provided your Kinect is installed correctly.

Using these tools and some others such as Adobe's Creative Suite, creativity, persistence and talent (inspiration?) you might come up with a great look and effect such as with this animation which utilized Kinect as one of its primary motion capture sources:



That still doesn't get to the title of my post, which is kind of misleading isn't it? What about Delphi and Kinect? Is there support for Kinect in the Delphi version of the latest Windows SDK? It turns out there is but with some catches. Jim McKeeth has done some work porting the Kinect SDK to Delphi but that conversion has stopped at version 1.5 of the Kinect SDK. There is much information about using Delphi and Kinect here at this originally great Spanish site which I share with you through a translation link so you may read it in your language of choice. As well there's much info here from this Korean site and once again translated to a language of your choice.

However, the Google code repository for the source branch of this version of the Delphi translation done by Jim McKeeth has vanished and that's because it has been relocated to GitHub under Embarcadero's repository there. Here's a direct link to the Kinect branch with the Delphi Headers and an example as well.

Following the example code from some of the Windows examples it is possible to interface the Kinect through Delphi, however there is a bit of reliance on Windows Event Objects which require a bit of work with threads and jumping through a few hoops to understand the workflow in working with the SDK to achieve goals such as timing data capture and streaming data formats from one of it's sensors.

I've been working with both Visual Studio and Delphi in an attempt to build a demo and utility application for the purposes of simple motion capture (without stepping on the toes of those who've already created some great applications that utilize Kinect). Perhaps in the near or somewhat near future I'll release these as I progress.

One thing that I will release is a recompiled set of binaries of the Kinect D2D Explorer application with the camera output reversed to the correct direction. In the Windows SDK example, the camera image is actually mirrored versus reality (meaning things on the left side of the room appear on the right etc). So in these versions I've rewritten it to correct that problem so that the camera output is no longer mirrored.

This zip file includes both the x86 and the x64 compiled versions on Google Drive.

The MD5 for the zip file is: 814583ac1955a9b105b42466170c5ee1

If you want the source, it's available at the ShhhhDigital Visual Studio Git repository.

Enjoy while I try to figure this out.

Brian Joseph Johns
http://thebutterflydragon.blogspot.ca
http://talesofthesanctum.blogspot.ca
http://shhhhdigital.blogspot.ca
http://delphitidbits.blogspot.ca