|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# This is a more or less a straight ruby port of the "record" utility |
| 3 | +# included in the libfreenect fakenect directory using the |
| 4 | +# ffi-libfreenect ruby class wrappers. |
| 5 | +# |
| 6 | +# This was really implemented just to see if ffi-libfreenect was working. |
| 7 | +# However, the output should be completely compatible the C version fakenect. |
| 8 | +# |
| 9 | +# usage: record.rb output_dir |
| 10 | +# |
| 11 | + |
| 12 | +begin |
| 13 | + require 'rubygems' |
| 14 | +rescue LoadError |
| 15 | +end |
| 16 | + |
| 17 | +$: << File.expand_path(File.join(File.dirname(__FILE__), "../lib")) |
| 18 | +require 'freenect' |
| 19 | + |
| 20 | +$last_timestamp = 0 |
| 21 | +$record_running = true |
| 22 | + |
| 23 | +def open_dump(type, timestamp, extension) |
| 24 | + $last_timestamp = timestamp |
| 25 | + filename = "%s-%f-%u.%s" % [ type, Time.now.to_f, timestamp, extension] |
| 26 | + STDERR.puts "Writing: #{File.join($out_dir, filename)}" |
| 27 | + File.open(File.join($out_dir,"INDEX.txt"), "a"){|f| f.puts(filename) } |
| 28 | + File.open(File.join($out_dir, filename), "wb") {|f| yield f} |
| 29 | +end |
| 30 | + |
| 31 | +orig_dir = Dir.pwd |
| 32 | +unless $out_dir = ARGV.shift |
| 33 | + STDERR.puts "usage: #{File.basename $0} output_dir" |
| 34 | + exit 1 |
| 35 | +end |
| 36 | +Dir.mkdir($out_dir) unless File.directory?($out_dir) |
| 37 | + |
| 38 | +trap('INT') do |
| 39 | + STDERR.puts "Caught INT signal cleaning up" |
| 40 | + $record_running = false |
| 41 | +end |
| 42 | + |
| 43 | +ctx = Freenect.init() |
| 44 | +dev = ctx.open_device(0) |
| 45 | + |
| 46 | +dev.set_depth_format(Freenect::DEPTH_11BIT) |
| 47 | +dev.set_video_format(Freenect::VIDEO_RGB) |
| 48 | +dev.start_depth() |
| 49 | +dev.start_video() |
| 50 | + |
| 51 | +dev.set_depth_callback do |device, depth, timestamp| |
| 52 | + open_dump('d', timestamp, "pgm") do |f| |
| 53 | + f.puts("P5 %d %d 65535\n" % [ Freenect::FRAME_W, Freenect::FRAME_H ] ) |
| 54 | + f.write(depth.read_string_length(Freenect::DEPTH_11BIT_SIZE)) |
| 55 | + end |
| 56 | +end |
| 57 | + |
| 58 | +dev.set_video_callback do |device, video, timestamp| |
| 59 | + open_dump('r', timestamp, 'ppm') do |f| |
| 60 | + f.puts("P6 %d %d 255\n" % [ Freenect::FRAME_W, Freenect::FRAME_H ] ) |
| 61 | + f.write(video.read_string_length(Freenect::RGB_SIZE)) |
| 62 | + end |
| 63 | +end |
| 64 | + |
| 65 | +while $record_running and (ctx.process_events >= 0) |
| 66 | + open_dump('a', $last_timestamp, "dump") do |f| |
| 67 | + state = dev.get_tilt_state |
| 68 | + f.write(state.to_ptr.read_string_length(state.size)) |
| 69 | + end |
| 70 | +end |
| 71 | + |
| 72 | +Dir.chdir(orig_dir) |
| 73 | +dev.stop_depth |
| 74 | +dev.stop_video |
| 75 | +dev.close |
| 76 | +ctx.close |
0 commit comments