I have some code below that takes different detected users recognized by the Kinect and assigns them a specific random id range based on the player they are. I use this to grab the x,y coordinates of their left or right hand. I was trying to have the Kinect get at least 6 peoples coordinates with the code. Am I taking the approach right or should I perform it differently? I am using the Kinect SDK v1.8.
Using skeletonFrameData As SkeletonFrame = e.OpenSkeletonFrame
If skeletonFrameData Is Nothing Then
Exit Sub
End If
' sensor.SkeletonStream.AppChoosesSkeletons = True
Dim allSkeletons(skeletonFrameData.SkeletonArrayLength - 1) As Skeleton
skeletonFrameData.CopySkeletonDataTo(allSkeletons)
Player Identification:
'identify specific players by ID
'ResetValues()
For j = 0 To playerid.Length - 1
'all players
playerid(i) = CInt(Rnd() * 4 + (i * 5))
allSkeletons(i).TrackingId = playerid(0)
Next j
'force each player to be moved to first blank spot
Dim tempList As New List(Of Skeleton)(allSkeletons)
tempList.RemoveAll(Function(sk) IsNothing(sk))
allSkeletons = tempList.ToArray
Log("Tracking id for player#1: " + allSkeletons(0).TrackingId.ToString)
Log("Tracking id for player#2: " + allSkeletons(1).TrackingId.ToString)
Log("Tracking id for player#3: " + allSkeletons(2).TrackingId.ToString)
Make sure current player is getting tracked or not:
If IsNothing(s) Then
Exit Sub
End If
If s.TrackingState = SkeletonTrackingState.Tracked Then
activeCount = activeCount + 1
End If
If s.TrackingState = SkeletonTrackingState.PositionOnly Then
passiveCount = passiveCount + 1
End If
If s.TrackingState = SkeletonTrackingState.NotTracked Then
nottracked = nottracked + 1
End If
totalplayers = activeCount + passiveCount + nottracked
'Log("passive count: " + passiveCount.ToString + " date: " + Now.ToString)
Player x,y display:
' the first found/tracked skeleton moves the mouse cursor
If s.TrackingState = SkeletonTrackingState.Tracked Then
' make sure both hands are tracked
'If Skeleton.Joints(JointType.HandLeft).TrackingState = JointTrackingState.Tracked AndAlso Skeleton.Joints(JointType.HandRight).TrackingState = JointTrackingState.Tracked Then
Dim cursorX, cursorY As Integer
' get the left and right hand Joints
Dim jointRight As Joint = s.Joints(JointType.HandRight)
Dim jointLeft As Joint = s.Joints(JointType.HandLeft)
' scale those Joints to the primary screen width and height
Dim scaledRight As Joint = jointRight.ScaleTo(CInt(Fix(SystemParameters.PrimaryScreenWidth)), CInt(Fix(SystemParameters.PrimaryScreenHeight)), SkeletonMaxX, SkeletonMaxY)
Dim scaledLeft As Joint = jointLeft.ScaleTo(CInt(Fix(SystemParameters.PrimaryScreenWidth)), CInt(Fix(SystemParameters.PrimaryScreenHeight)), SkeletonMaxX, SkeletonMaxY)
' relativemouselocation.Content = jointRight.Position
' figure out the cursor position based on left/right handedness
If LeftHand.IsChecked.GetValueOrDefault() Then
cursorX = CInt(Fix(scaledLeft.Position.X))
cursorY = CInt(Fix(scaledLeft.Position.Y))
Else
cursorX = CInt(Fix(scaledRight.Position.X))
cursorY = CInt(Fix(scaledRight.Position.Y))
End If
Dim leftClick As Boolean
' figure out whether the mouse button is down based on where the opposite hand is
If (LeftHand.IsChecked.GetValueOrDefault() AndAlso jointRight.Position.Y > ClickThreshold) OrElse ((Not LeftHand.IsChecked.GetValueOrDefault()) AndAlso jointLeft.Position.Y > ClickThreshold) Then
leftClick = True
' MsgBox("clicked")
Else
leftClick = False
End If
'if i is less then the total amount of players then send players coordinates for person that is active.
If i <= 5 Then
Select Case True
Case Is = s.TrackingId >= 1 And s.TrackingId <= 4
i = 0
playeractive(i) = True
player1xy.Content = cursorX & ", " & cursorY & ", " & leftClick
Status.Text = "player1 identified" & cursorX.ToString & ", " & cursorY.ToString & ", " & leftClick.ToString
' sensor.SkeletonStream.ChooseSkeletons(1, 2)
Case Is = s.TrackingId >= 5 And s.TrackingId <= 9
i = 1
playeractive(i) = True
player2xy.Content = cursorX & ", " & cursorY & ", " & leftClick
Status.Text = "player2 identified" & cursorX.ToString & ", " & cursorY.ToString & ", " & leftClick.ToString
Case Is = s.TrackingId >= 10 And s.TrackingId <= 14
i = 2
playeractive(i) = True
player3xy.Content = cursorX & ", " & cursorY & ", " & leftClick
Status.Text = "player3 identified" & cursorX.ToString & ", " & cursorY.ToString & ", " & leftClick.ToString
Case Is = s.TrackingId >= 15 And s.TrackingId <= 19
i = 3
playeractive(i) = True
player4xy.Content = cursorX & ", " & cursorY & ", " & leftClick
Status.Text = "player4 identified" & cursorX.ToString & ", " & cursorY.ToString & ", " & leftClick.ToString
Case Is = s.TrackingId >= 20 And s.TrackingId <= 24
i = 4
playeractive(i) = True
player5xy.Content = cursorX & ", " & cursorY & ", " & leftClick
Status.Text = "player5 identified" & cursorX.ToString & ", " & cursorY.ToString & ", " & leftClick.ToString
Case Is = s.TrackingId >= 25 And s.TrackingId <= 29
i = 5
playeractive(i) = True
player6xy.Content = cursorX & ", " & cursorY & ", " & leftClick
Status.Text = "player6 identified" & cursorX.ToString & ", " & cursorY.ToString & ", " & leftClick.ToString
End Select
Log("person #: " + i.ToString + "Tracking ID: " + s.TrackingId.ToString)
currentperson.Content = i.ToString + "Tracking ID: " + s.TrackingId.ToString
End If
'If playeractive(i) = True And i >= 0 And frame.SkeletonArrayLength > 0 Then
'NativeMethods.SendMouseInput(cursorX, cursorY, CInt(Fix(SystemParameters.PrimaryScreenWidth)), CInt(Fix(SystemParameters.PrimaryScreenHeight)), leftClick, totalplayers, i)
'End If
'if total players is 1 or greater send coordinate data.
If totalplayers >= 1 Then
DefineMouseData(cursorX, cursorY, leftClick)
End If
'make the below code active when I get multiple player tracking working
If playeractive(i) = True Then
' Environment.SetEnvironmentVariable("Player" + i.ToString + "xcoords", player(i).bytex.ToString, EnvironmentVariableTarget.Machine)
' Environment.SetEnvironmentVariable("Player" + i.ToString + "ycoords", player(i).bytey.ToString, EnvironmentVariableTarget.Machine)
'Environment.SetEnvironmentVariable("Player" + i.ToString + "leftclick", player(i).leftclick.ToString, EnvironmentVariableTarget.Machine)
'System.Threading.Thread.Sleep(300)
End If
If i <= 5 Then
If playeractive(5) = True Then
'if 6th player exit for loop and open next frame.
playeractive(5) = False
Exit For
Else
'd = d + 1
End If
End If
End If
NumPassive.Content = "passive count: " + passiveCount.ToString
Numactive.Content = "active count: " + activeCount.ToString
nottrackedplayers.Content = "not tracked players: " + nottracked.ToString
playeractive(i) = False
Choose specific player:
For h = 0 To activeCount - 1
If h >= 2 Then
If activeCount - 1 > 0 Then
sensor.SkeletonStream.ChooseSkeletons(playerid(h), playerid(h + 1))
End If
End If
Next
Next
ResetValues()
End Using
As you can see I go from 1-4 for player one (it might be 0-4), 5-9 for player 2, and so on for each player. If anyone can improve this code or know of any methods I am not using please post a solution below. Sorry for the long code post but if someone thinks that what I have commented out is irrelevant please edit this and remove the comments. I plan on using the multiple Kinect code too I commented out to do more then 6 players later but wish to get 4-6 players working for now. Edit: I removed the unimportant parts. The important part is the sensor.SkeletonStream.ChooseSkeletons(s.TrackingId) Line here.
For more explanation: http://msdn.microsoft.com/en-us/library/microsoft.kinect.skeletonstream.chooseskeletons.aspx
I want a specific user to be looped through such as player 1's coordinates retrieved first, then second, and so on so it does not seem slow. But I need the best method to do so. The problem is: ChooseSkeletons only does 2 people. I can loop through each person passing their id to chooseskeleton and this gives me 4-6 people but this seems inefficient because chooseskeleton takes too long (maybe improper usage?).
Sub ProcedureName
up toEnd Sub
. You've posted an incompleteUsing
scope... \$\endgroup\$