Software Archive
Read-only legacy content
17061 Discussions

hand contour detection is not working in java

deepak_k_1
Beginner
179 Views

hi,

i want to detect a hand contour in java. it is showing black window.

can any one help me what i am doing wrong here.

 

code :

 

/*******************************************************************************

 INTEL CORPORATION PROPRIETARY INFORMATION
 This software is supplied under the terms of a license agreement or nondisclosure
 agreement with Intel Corporation and may not be copied or disclosed except in
 accordance with the terms of that agreement
 Copyright(c) 2014 Intel Corporation. All Rights Reserved.

 *******************************************************************************/
import intel.rssdk.*;
import java.lang.System.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.*;

public class contour
{
    static int cWidth  = 640;
    static int cHeight = 480;
    static int dWidth, dHeight;
    static boolean exit = false;


    public static void main(String s[])
    {

        PXCMSenseManager senseMgr = PXCMSenseManager.CreateInstance();
        pxcmStatus sts ;
        sts = senseMgr.EnableStream(PXCMCapture.StreamType.STREAM_TYPE_DEPTH, cWidth, cHeight);
        if (sts.compareTo(pxcmStatus.PXCM_STATUS_NO_ERROR)<0) {
            System.out.print("Failed to create Stream Instance\n");
            System.exit(1);
        }

        sts = senseMgr.EnableHand(null);
        if (sts.compareTo(pxcmStatus.PXCM_STATUS_NO_ERROR)<0) {
            System.out.print("Failed to enable HandAnalysis\n");
            System.exit(3);
        }

        sts = senseMgr.Init();

        System.out.println(sts);

        PXCMCapture.Device device = senseMgr.QueryCaptureManager().QueryDevice();
        PXCMCapture.Device.StreamProfileSet profiles = new PXCMCapture.Device.StreamProfileSet();
        device.QueryStreamProfileSet(profiles);
        device.SetMirrorMode(PXCMCapture.Device.MirrorMode.MIRROR_MODE_HORIZONTAL);

        dWidth = profiles.depth.imageInfo.width;
        dHeight = profiles.depth.imageInfo.height;

        Listener listener = new Listener();


        contour d_raw = new contour();
        DrawFrame d_df=new DrawFrame(dWidth, dHeight);
        JFrame dframe= new JFrame("Intel(R) RealSense(TM) SDK - Depth Stream");
        dframe.addWindowListener(listener);
        dframe.setSize(dWidth, dHeight);
        dframe.add(d_df);
        dframe.setVisible(true);

        //hand
        if (sts.compareTo(pxcmStatus.PXCM_STATUS_NO_ERROR)>=0) {
            PXCMHandModule handModule = senseMgr.QueryHand();
            PXCMHandConfiguration handConfig = handModule.CreateActiveConfiguration();
            handConfig.EnableAllGestures();
            handConfig.EnableAllAlerts();
            handConfig.EnableAlert(PXCMHandData.AlertType.ALERT_HAND_DETECTED);
            handConfig.EnableAlert(PXCMHandData.AlertType.ALERT_HAND_NOT_DETECTED);
            handConfig.ApplyChanges();
            handConfig.Update();
            PXCMHandData handData = handModule.CreateOutput();


            while (listener.exit == false)
            {
                sts = senseMgr.AcquireFrame(true);

                if (sts == pxcmStatus.PXCM_STATUS_NO_ERROR)
                {
                    PXCMCapture.Sample sample = senseMgr.QueryHandSample();
                    //PXCMCapture.Sample handSample = senseMgr.QueryHandSample();
                    int numOfHands = handData.QueryNumberOfHands();
                    PXCMPointI32[][] pointOuter = new PXCMPointI32[numOfHands][];
                    if (sample.depth != null)
                    {
                        PXCMImage image = sample.depth;
                        PXCMImage.ImageData dData = new PXCMImage.ImageData();
                        for (int j = 0; j < numOfHands; j++) {
                            int[] id = new int[1];
                            PXCMImage.ImageData data;
                            handData.QueryHandId(PXCMHandData.AccessOrderType.ACCESS_ORDER_BY_TIME, j, id);
                            PXCMHandData.IHand hData = new PXCMHandData.IHand();
                            handData.QueryHandData(PXCMHandData.AccessOrderType.ACCESS_ORDER_BY_TIME, j, hData);
                            if (handData != null) {
                                if (sts == hData.QuerySegmentationImage(image)) {
                                    sample.depth.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB32, dData);
                                    if (sts.compareTo(pxcmStatus.PXCM_STATUS_NO_ERROR) < 0) {
                                        System.out.println("Failed to AcquireAccess of depth image data");
                                        System.exit(3);
                                    }
                                }
                            }


                        /*
                        sample.depth.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB32, dData);
                        if (sts.compareTo(pxcmStatus.PXCM_STATUS_NO_ERROR)<0)
                        {
                            System.out.println ("Failed to AcquireAccess of depth image data");
                            System.exit(3);
                        }*/


                            int dBuff[] = new int[dData.pitches[0] / 4 * dHeight];
                            dData.ToIntArray(0, dBuff);
                            d_df.image.setRGB(0, 0, dWidth, dHeight, dBuff, 0, dData.pitches[0] / 4);
                            d_df.repaint();


                            handData.Update();


                            sts = sample.depth.ReleaseAccess(dData);
                            if (sts.compareTo(pxcmStatus.PXCM_STATUS_NO_ERROR) < 0) {
                                System.out.println("Failed to ReleaseAccess of depth image data");
                                System.exit(3);
                            }
                        }
                    }
                }
                else
                {
                    System.out.println("Failed to acquire frame");
                }

                senseMgr.ReleaseFrame();
            }

            senseMgr.Close();
            System.out.println("Done streaming");
        }
        else
        {
            System.out.println("Failed to initialize");
        }

        //cframe.dispose();
        dframe.dispose();
    }


}

class Listener extends WindowAdapter {
    public boolean exit = false;
    @Override public void windowClosing(WindowEvent e) {
        exit=true;
    }
}



class DrawFrame extends Component {
    public BufferedImage image;

    public DrawFrame(int width, int height) {
        image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    }

    public void paint(Graphics g) {
        ((Graphics2D) g).drawImage(image, 0, 0, null);

    }

    public void drawCenteredCircle(Graphics g, int x, int y, int r) {
        ((Graphics2D)g).drawImage(image, 0, 0, null);
        x = x-(r/2);
        y = y-(r/2);
        g.setColor(Color.blue);
        g.fillOval(x, y, r, r);
    }



}
0 Kudos
0 Replies
Reply