Microsoft Small Basic

Program Listing: LGR919-0
' Sample Code for KinectFaceList Object
' Program ID LGR919-0

title = "KinectFaceList Sample"
GraphicsWindow.Title = title
Init()
KinectWindow.Show("Color")
KinectFaceList.FacesChanged = OnFacesChanged
KinectFaceList.IsAvailableChanged = OnIsAvailableChanged
KinectFaceList.StartTracking()
While "True"
If facesChanged Then
'KinectFaceList.StopTracking("False")
CheckFaceList()
facesChanged = "False"
'KinectFaceList.StartTracking()
EndIf
EndWhile

Sub Init
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
gw = 598
gh = 500
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BackgroundColor = "LightGray"
GraphicsWindow.BrushColor = "Black"
tbox = Controls.AddMultiLineTextBox(10, 10)
Controls.SetSize(tbox, (gw / 2) - 10, gh - 20)
pos = "1=Left;2=Top;3=Right;4=Bottom;"
iPos = Array.GetAllIndices(pos)
nPos = Array.GetItemCount(pos)
pointType = "1=EyeLeft;2=EyeRight;3=Nose;4=MouthCornerLeft;"
pointType = pointType + "5=MouthCornerRight;"
iPnt = Array.GetAllIndices(pointType)
nPnt = Array.GetItemCount(pointType)
faceProperty = "1=Happy;2=Engaged;3=WearingGlasses;4=LeftEyeClosed;"
faceProperty = faceProperty + "5=RightEyeClosed;6=MouthOpen;"
faceProperty = faceProperty + "7=MouthMoved;8=LookingAway;"
iProp = Array.GetAllIndices(faceProperty)
nProp = Array.GetItemCount(faceProperty)
EndSub

Sub CheckFaceList
buf = ""
cnt = KinectFaceList.Count
For index = 1 To cnt
If KinectFaceList.IsTracked(index) Then
buf = buf + "index=" + index + CRLF
buf = buf + CRLF + "GetBoundingBoxInColorSpace:" + CRLF
box = KinectFaceList.GetBoundingBoxInColorSpace(index)
For j = 1 To nPos
buf = buf + " " + pos[iPos[j]] + "=" + box[pos[iPos[j]]] + CRLF
EndFor
ShowFace()
buf = buf + CRLF + "GetBoundingBoxInInfraredSpace:" + CRLF
box = KinectFaceList.GetBoundingBoxInInfraredSpace(index)
For j = 1 To nPos
buf = buf + " " + pos[iPos[j]] + "=" + box[pos[iPos[j]]] + CRLF
EndFor
buf = buf + CRLF + "GetFacePointInColorSpace:" + CRLF
For j = 1 To nPnt
point = KinectFaceList.GetFacePointInColorSpace(index, pointType[iPnt[j]])
buf = buf + " " + pointType[iPnt[j]] + "=" + point["x"] + "," + point["y"] + CRLF
p[pointType[iPnt[j]]] = point
EndFor
ShowFacePoint()
buf = buf + CRLF + "GetFaceProperty:" + CRLF
For j = 1 To nProp
ans = KinectFaceList.GetFaceProperty(index, faceProperty[iProp[j]])
buf = buf + " " + faceProperty[iProp[j]] + "=" + ans + CRLF
EndFor
buf = buf + CRLF + "GetFeatures:" + CRLF
features = KinectFaceList.GetFeatures(index)
iFeat = Array.GetAllIndices(features)
nFeat = Array.GetItemCount(features)
For j = 1 To nFeat
buf = buf + " " + iFeat[j] + "=" + features[iFeat[j]] + CRLF
EndFor
buf = buf + CRLF + "GetRotationQuaternion:" + CRLF
rotation = KinectFaceList.GetRotationQuaternion(index)
iRot = Array.GetAllIndices(rotation)
nRot = Array.GetItemCount(rotation)
For j = 1 To nRot
buf = buf + " " + iRot[j] + "=" + rotation[iRot[j]] + CRLF
EndFor
buf = buf + CRLF + "GetTrackingId:" + CRLF
id = KinectFaceList.GetTrackingId(index)
buf = buf + " id=" + id + CRLF
EndIf
EndFor
If buf <> "" Then
Controls.SetTextBoxText(tbox, buf)
EndIf
EndSub

Sub OnFacesChanged
facesChanged = "True"
EndSub

Sub OnIsAvailableChanged
If KinectFaceList.IsAvailable Then
GraphicsWindow.Title = title + " - Ready"
Else
GraphicsWindow.Title = title + " - Not Ready"
EndIf
EndSub

Sub ShowFace
' param box - face bounding box
scaleX = 0.5
scaleY = 0.5
x = box["Left"]
y = box["Top"]
width = box["Right"] - x
height = box["Bottom"] - y
If face <> "" Then
Shapes.Remove(face)
EndIf
GraphicsWindow.BrushColor = "White"
face = Shapes.AddEllipse(width * scaleX, height * scaleY)
Shapes.Move(face, x * scaleX, y * scaleY)
EndSub

Sub ShowFacePoint
' param p - face point array
size = height / 10
x = p["EyeLeft"]["x"]
y = p["EyeLeft"]["y"]
If eyeLeft <> "" Then
Shapes.Remove(eyeLeft)
EndIf
GraphicsWindow.BrushColor = "Black"
eyeLeft = Shapes.AddEllipse(size * scaleX, size * scaleY)
Shapes.Move(eyeLeft, x * scaleX, y * scaleY)
x = p["EyeRight"]["x"]
y = p["EyeRight"]["y"]
If eyeRight <> "" Then
Shapes.Remove(eyeRight)
EndIf
GraphicsWindow.BrushColor = "Black"
eyeRight = Shapes.AddEllipse(size * scaleX, size * scaleY)
Shapes.Move(eyeRight, x * scaleX, y * scaleY)
x = p["Nose"]["x"]
y = p["Nose"]["y"]
If nose <> "" Then
Shapes.Remove(nose)
EndIf
GraphicsWindow.BrushColor = "Pink"
nose = Shapes.AddTriangle(0, size * scaleX, size * scaleX / 2, 0, size * scaleY, size * scaleY)
Shapes.Move(nose, x * scaleX, y * scaleY)
x1 = p["MouthCornerLeft"]["x"]
y1 = p["MouthCornerLeft"]["y"]
x2 = p["MouthCornerRight"]["x"]
y2 = p["MouthCornerRight"]["y"]
If mouth <> "" Then
Shapes.Remove(mouth)
EndIf
GraphicsWindow.PenColor = "Red"
mouth = Shapes.AddLine(x1 * scaleX, y1 * scaleY, x2 * scaleX, y2 * scaleY)
GraphicsWindow.PenColor = "Black"
EndSub