Giter VIP home page Giter VIP logo

libfreenect-goodies's Introduction

This is a grand unifying demo of the python scientific computing environments. It lets you tinker with kinect data in OpenCV, OpenGL, and Matplotlib, all at the same time!

Installation

  1. You need to have installed: IPython, Matplotlib, OpenCV 2.1, PyOpengl, wxPython

  2. Build the latest version of libfreenect. https://github.com/openkinect/libfreenect

  3. Build and install the python wrappers for libfreenect

     cd libfreenect/wrappers/python
     python setup.py install
    
  4. Download the latest version of this project

     git clone https://github.com/amiller/libfreenect-goodies.git
     cd pykinect
    
  5. Test that python can find libfreenect by running:

     python demo_freenect.py
    

Usage instructions

  1. Please run this script using:

     ipython -pylab -wthread demo_pykinect.py
    
  2. You should see an opengl window pop up with a preview of a point cloud. You can pan and zoom with the mouse. Run the following commands:

     update()      # Grabs one frame from kinect and updates the point cloud
     update_on()   # Grabs frames from kinect on a thread - 3d Video! (might be slow!)
     update_off()  # Stops the update thread
    
  3. You can also use opencv:

     loopcv()      # Grab frames and display them as a cv image
     (ctrl+c to break)
    
  4. You can also use matplotlib:

     imshow(depth)
    
  5. Most importantly, you can reload any of the code without pausing or destroying your python instance:

     %run -i demo_pykinect.py
    

Try changing some of the code, like the downsampling factor (search: downsampling) or the point size (search: GL_POINT_SIZE) and update the code without quitting python.

This is an ideal environment for developing 3D point cloud algorithms and visualizations. All of your tools are right at hand.

Note to MATLAB users:
Yes, MATLAB already does most of this... there are plenty of reasons to prefer python, one of which is access to OpenGL drawing commands - scatter3 isn't adequate!

libfreenect-goodies's People

Contributors

amiller avatar iuliux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

libfreenect-goodies's Issues

Usage instructions - error on step 1

I followed the instruction and got the following 3 errors. Please help. Thank you.

sudo ipython -pylab -wthread demo_pykinect.py

[TerminalIPythonApp] Bad config encountered during initialization:
[TerminalIPythonApp] Unrecognized flag: '-wthread'

  1. demo_pykinect.py is missing in the repository. Could it have been removed?

sudo ipython --pylab=auto demo_pclview.py

Welcome to pylab, a matplotlib-based Python environment [backend: TkAgg].
For more information, type 'help(pylab)'.
[Stream 70] Expected 1748 data bytes, but got 948

Zoom in demo_pclview.py

The zoom amount (at least on my machine) is much too large - zooming behind the camera or down to a point.

This provides a more usable zoom amount.

zoomdist = np.power(0.5, -dy.001)

update_on() crash

When I manually update() a few times or when I apply update_on() to be handled by a thread, the wx Window freezes and no mouse events are processed. Anyone having the same problem?

strange point clouds as output for this code

import freenect
import sys
import cv2
import numpy as np
from PIL import Image
 
def depth2xyzuv(depth, u=None, v=None):
  if u is None or v is None:
    u,v = np.mgrid[:480,:640]
  
  # Build a 3xN matrix of the d,u,v data
  C = np.vstack((u.flatten(), v.flatten(), depth.flatten(), 0*u.flatten()+1))

  # Project the duv matrix into xyz using xyz_matrix()
  X,Y,Z,W = np.dot(xyz_matrix(),C)
  X,Y,Z = X/W, Y/W, Z/W
  xyz = np.vstack((X,Y,Z)).transpose()
  xyz = xyz[Z<0,:]


  U,V,_,W = np.dot(np.dot(uv_matrix(), xyz_matrix()),C)
  U,V = U/W, V/W
  uv = np.vstack((U,V)).transpose()    
  uv = uv[Z<0,:]       

  # Return both the XYZ coordinates and the UV coordinates
  return xyz, uv
def uv_matrix():

  rot = np.array([[ 9.99846e-01,   -1.26353e-03,   1.74872e-02], 
                  [-1.4779096e-03, -9.999238e-01,  1.225138e-02],
                  [1.747042e-02,   -1.227534e-02,  -9.99772e-01]])
  trans = np.array([[1.9985e-02, -7.44237e-04,-1.0916736e-02]])
  m = np.hstack((rot, -trans.transpose()))
  m = np.vstack((m, np.array([[0,0,0,1]])))
  KK = np.array([[529.2, 0, 329, 0],
                 [0, 525.6, 267.5, 0],
                 [0, 0, 0, 1],
                 [0, 0, 1, 0]])
  m = np.dot(KK, (m))
  return m

def xyz_matrix():
  fx = 594.21
  fy = 591.04
  a = -0.0030711
  b = 3.3309495
  cx = 339.5
  cy = 242.7
  mat = np.array([[1/fx, 0, 0, -cx/fx],
                  [0, -1/fy, 0, cy/fy],
                  [0,   0, 0,    -1],
                  [0,   0, a,     b]])
  return mat
# save information in csv File
def save_3d_information(xyz,uv,img):
	#print(uv)
	# use globals in context
	global taken_photos
	# create pointcloud file
	pointcloud_file = open('pointcloud-'+str(taken_photos)+'.pcd','wb')
	index = 0
	for i in range(0, len(xyz)):
		if xyz[i][0]==0 or xyz[i][1]==0 or xyz[i][2]==0 :
			continue
		index += 1
	
	# PLY HEADER
	pointcloud_file.write("# .PCD v.7 - Point Cloud Data file format\n")
	pointcloud_file.write("VERSION .7\n")
	pointcloud_file.write("FIELDS x y z rgb\n")
	pointcloud_file.write("SIZE 4 4 4 4\n")
	pointcloud_file.write("TYPE F F F F\n")
	pointcloud_file.write("COUNT 1 1 1 1\n")
	pointcloud_file.write("WIDTH "+str(index)+"\n")
	pointcloud_file.write("HEIGHT 1\n")
	pointcloud_file.write("VIEWPOINT 0 0 0 1 0 0 0\n")
	pointcloud_file.write("POINTS "+str(index)+"\n")
	pointcloud_file.write("DATA ascii\n")
	#Assign colors from the rectified image
 	for i in range(0, len(xyz)):
		if xyz[i][0]==0 or xyz[i][1]==0 or xyz[i][2]==0 :
			continue
		#print uv[i][0]	
		x=(int)(uv[i][0])	
		y=(int)(uv[i][1])
		g = img[x][y]
		#print g
		rgb = ((int)(g[0])) << 16 | ((int)(g[1])) << 8 | ((int)(g[2]))
		#print rgb
		#cloud_color.points[i].rgb = *(float*)(&rgb);
  		pointcloud_file.write(str(float(xyz[i][0]))+" "+str(float(xyz[i][1]))+" "+str(float(xyz[i][2]))+" "+str(float(rgb))+"\n")
	pointcloud_file.close()

if __name__ == "__main__":
    arg1 = sys.argv[1]
    arg2 = sys.argv[2]
    taken_photos = 0
    rgb = np.uint8(cv2.imread(arg1))
    depth = cv2.imread(arg2,-1)
    #arr = depth.astype(np.uint8)
    #cv2.imshow('RGB image',rgb)
    #cv2.imshow('Depth image',arr)
    #save_3d_information(depth,frame)
    u,v = np.mgrid[:480,:640]
    xyz,uv = depth2xyzuv(depth,u,v)
    print (xyz)
    print (uv)
    save_3d_information(xyz,uv,rgb)
                        
cv2.destroyAllWindows()

Demo instructions

Also the usage instructions:
ipython -pylab -wthread demo_pykinect.py

should that be:
ipython -pylab -wthread demo_pclview.py

Get the value of depth image in mm

Hello,
Plllllllllllz I need help!! I want to use kinect to get a depth map in millimeters, I find that I can use mode DEPTH_REGISTERED but I can't find an example of code that use this mode in python :( please can someone help me to know how can I use this mode in python code.
Thank you so much for any help.

cv.ShowImage requires a CvArr, not a numpy array

I tried to run demo_freenect.py, but it crashes as ShowImage (line 17) requires a CvArr, and not a numpy array as input.

An easy solution would be to change line 17 to:
cv.ShowImage('both', cv.fromarray(np.array(da[::2,::2,::-1])))

demo_pclview.py

demo_pclview.py seems to have a problem on line 17 . I had to convert nparray to CvMat using cv.fromarray()

hi i'm try to run python file from libfreenect and i get this

yotam-azriels-MacBook-Pro:python yotamazriel$ python demo_cv_async.py
Traceback (most recent call last):
File "demo_cv_async.py", line 2, in
import freenect
ImportError: dlopen(/Users/yotamazriel/libfreenect/wrappers/python/freenect.so, 2): Symbol not found: _libusb_get_parent
Referenced from: /usr/local/lib/libfreenect.0.5.dylib
Expected in: /usr/local/lib/libusb-1.0.0.dylib
in /usr/local/lib/libfreenect.0.5.dylib

plss help i'm depressed

Mirrored point clouds with calibkinect.

I seem to get mirrored point clouds with calibkinect.depth2xyzuv. Can anyone confirm this? All z-coordinates are negative, and if I mirror through the xy-plane, the point clouds become correct.

what is it that I am getting from my code?

Using calibkinect.py, i printed u,v array and x,y,z array using the following code line for a sample of pixels, as given in the example:
u,v = mgrid[10:120,50:80]
xyz,uv = depth2xyzuv(freenect.sync_get_depth()[v,u], u, v)

what are these values exactly? and how can I use these values to find the location of my object in those pixels, with respect to kinect's position... what am I missing here!!?? Please help!

Below is the screenshot for the reading of the pixels of the wall, which is approximately 120 cm away from my kinect.
screenshot from 2017-10-26 18-48-18

demo_pclview issue on MAC

When I run the following command
ipython --pylab --gui=wx demo_pclview.py

What I am getting is the message "Using matplotlib backend: MacOSX", the warning that "Using deprecated class PySimpleApp." and the wxWindow opens for a second then closes and nothing happens, anyway to fix that?

Thanks.
Val

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.