下面我有一些代码,这些代码使用Kinect识别的不同的检测用户,并根据玩家的身份分配给他们一个特定的随机标识范围。我用这个来抓取他们左手和右手的x,y坐标。我试着让Kinect得到至少6个人与代码的坐标。我是采取了正确的方法,还是应该采取不同的做法?我正在使用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)
球员身份:
'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)
确保当前播放器是否被跟踪:
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)
播放器x,y显示:
' 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
选择特定的玩家:
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
正如你所看到的,我从1-4到1-4(可能是0-4),5-9对于玩家2,以此类推。如果有人可以改进这段代码,或者知道我没有使用的任何方法,请在下面发布一个解决方案。对不起,长代码帖子,但如果有人认为我已经评论掉的是不相关的,请编辑这个,并删除评论。我计划使用多个Kinect代码,我评论说,以后做更多的6名玩家,但希望得到4-6名球员目前的工作。编辑:我删除了不重要的部分。重要的部分是这里的sensor.SkeletonStream.ChooseSkeletons(s.TrackingId)线。
有关更多解释:http://msdn.microsoft.com/en-us/library/microsoft.kinect.skeletonstream.chooseskeletons.aspx
我想要一个特定的用户被环通过,如玩家1's坐标检索第一,然后第二,等等,所以它似乎不慢。但我需要最好的方法。问题是: ChooseSkeletons只处理两个人。我可以循环每个人传递他们的id来选择骨架,这给了我4-6个人,但是这看起来效率低下,因为选择骨架花费的时间太长了(也许使用不当?)
发布于 2013-12-05 19:12:13
我只想回顾一下这里的情况:
if
语句来检查同一个变量。使用if-else
块或Select Case
https://codereview.stackexchange.com/questions/36707
复制相似问题