0
votes

i am working with kinect 360 and windows sdk 1.8. i have an application developed in c# wpf that can move mouse cursor with hand gestures and able to click also. the problem is whenever i want to move cursor to corner i need to take a step in front of kinect to do that. but i want to scale the cursor in such way so i only have to move my hand and cursor moves to all the screen.

Here is the code i am trying

 void sensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
    {
        bool receivedData = false;

        using (SkeletonFrame SFrame = e.OpenSkeletonFrame())
        {
            if (SFrame == null)
            {
                return;
            }
            else
            {
                skeletons = new Skeleton[SFrame.SkeletonArrayLength];
                SFrame.CopySkeletonDataTo(skeletons);
                receivedData = true;
            }
        }

        if (receivedData)
        {

            Skeleton currentSkeleton = (from s in skeletons
                                        where s.TrackingState == SkeletonTrackingState.Tracked
                                        select s).FirstOrDefault();

            if (currentSkeleton != null)
            {
                processing(currentSkeleton.Joints[JointType.HandRight]);
                click(currentSkeleton.Joints[JointType.HandLeft], currentSkeleton.Joints[JointType.Head]);
            }
        }
    }
    private void processing(Joint handright)
    {
        Microsoft.Kinect.SkeletonPoint vector = new Microsoft.Kinect.SkeletonPoint();
        vector.X = ScaleVector(1600, handright.Position.X);
        vector.Y = ScaleVector(900, -handright.Position.Y);
        vector.Z = handright.Position.Z;
        handright.Position = vector;

        zAxis = handright.Position.Z;



        leftofscreen = Convert.ToInt32(handright.Position.X);
        topofscreen = Convert.ToInt32(handright.Position.Y);

        SetCursorPos(leftofscreen, topofscreen);

    }
    private void click(Joint handleft, Joint head)
    {
        if (handleft.Position.Y > head.Position.Y)
        {
            mouse_event(LEFTDOWN, leftofscreen, topofscreen, 0, 0);
            mouse_event(LEFTUP, leftofscreen, topofscreen, 0, 0);

        }
    }

    private float ScaleVector(int length, float position)
    {
        float value = (((((float)length) / 1f) / 2f) * position) + (length / 2);
        if (value > length)
        {
            return (float)length;
        }
        if (value < 0f)
        {
            return 0f;
        }
        return value;
    }

i have tried this kinect for c# dev and similar to youtube video code but since in this tutorial he is using sdk 1.0 and some methods are absolute so i am unable to track my hands can someone help me how i can scale my hand movement with cursor properly so i can move cursor to all the screen with a slight movement of my hand?

1
Are you able to use kinect sdk v2? if you can, the problem would be solved.Otherwise i can try to solve your issuebingcheng45
@bingcheng45 i did not tried sdk v2. can you suggest me a link or article where i can find how to do the same stuff i am doing in this code with sdk v2 but if you can help me with my current code that will be very nice of you. thank you in advance.Muhammad Zeeshan
I can't really find an article on this, but i can have my code that only have hovering and some default gestures function up on github for you to see and try out. if you found that useful do give me an upvote XPbingcheng45

1 Answers

0
votes

Here is the project solution in Kinect sdk v2 https://github.com/bingcheng45/Kinect-v2-Hovering-Example

Assuming you added the using Microsoft.Kinect reference already

Basically first you add this section into the MainWindow.xaml

<k:KinectRegion x:Name="kinectRegion">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <Grid Grid.Row="0" Margin="10 0 10 20">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>


                <k:KinectUserViewer Grid.Column="1" Height="100" HorizontalAlignment="Center" VerticalAlignment="Top" />
                <TextBlock Grid.Column="1" HorizontalAlignment="Right" Margin="0 0 -1 0" VerticalAlignment="Bottom"  FontSize="18">Controls Basics</TextBlock>
            </Grid>

            <ContentControl Grid.Row="1" x:Name="navigationRegion">
                <Grid x:Name="kinectRegionGrid" Margin="10,20,10,20">
                    <Button x:Name="btnCenter" Content="CLICK HERE" HorizontalAlignment="Left" Margin="174,48,0,0" Style="{StaticResource 
                        FrameSelectorButtonStyle}" VerticalAlignment="Top" Width="158" Height="76"  MouseLeave="btnCenter_MouseLeave" Click="btnCenter_Click" MouseEnter="btnCenter_MouseEnter" Background="#FFC9C9C9" TouchEnter="btnCenter_TouchEnter"/>
                    <Label x:Name="lbtext" Content="Label" HorizontalAlignment="Left" Margin="51,31,0,0" VerticalAlignment="Top"/>

                </Grid>
            </ContentControl>
        </Grid>
    </k:KinectRegion>

then just add this line of code in your Mainwindow.Xaml.cs and you are good to go!

public MainWindow()
        {
            InitializeComponent();

            //hovering start
            KinectRegion.SetKinectRegion(this, kinectRegion);

            App app = ((App)Application.Current);
            app.KinectRegion = kinectRegion;

            // Use the default sensor
            this.kinectRegion.KinectSensor = KinectSensor.GetDefault();
            //hovering end
        }

Cheers!