File: video_snapshot.rb

package info (click to toggle)
libfreenect 1%3A0.5.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,156 kB
  • sloc: ansic: 7,417; cpp: 7,265; cs: 2,062; python: 992; ruby: 873; java: 730; xml: 49; sh: 27; makefile: 23
file content (67 lines) | stat: -rwxr-xr-x 1,501 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby
# Actual video and depth capture work similarly to eachother.
# But they're still both pretty un-sugary.
#
# Future versions will probably abstract this and try to make it more 
# ruby-ish.
#
# The example below shows how to capture a single video frame to a PPM file.

$: << File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
require 'freenect'
ctx = Freenect.init()

devs = ctx.num_devices

STDERR.puts "Number of Kinects detected: #{devs}"
unless devs > 0
  STDERR.puts "Error: no kinect detected"
  exit 1
end

STDERR.puts "Selecting device 0"
dev = ctx.open_device(0)

dev.led = :green   # play with the led

dev.video_mode = Freenect.video_mode(:medium, :rgb)
dev.start_video()

# Or to grab depth:
# dev.depth_mode = Freenect.depth_mode(:medium, :depth_11bit)
# dev.start_depth()


$snapshot_finished = nil

STDERR.puts "Attempting snapshot"
dev.set_video_callback do |device, video, timestamp|
  if not $snapshot_finished
    fname = "%i.ppm" % timestamp
    STDERR.puts "Writing #{fname}"
    File.open(fname, "w") do |f|
      f.puts("P6 %d %d 255\n" % [ dev.video_mode.width, dev.video_mode.height ] )
      f.write(video.read_string_length(dev.video_mode.bytes))
    end
    $snapshot_finished = true
  end
end

ret = -1
until $snapshot_finished 
  break if (ret=ctx.process_events) < 0
end

if ret < 0
  STDERR.puts "Error: unable to take snapshot. process_events code=#{ret}"
end

dev.led = :off
dev.stop_video
# Or
# dev.stop_depth

dev.close
ctx.close