Class: AlprCam::FrameCapture

Inherits:
Object
  • Object
show all
Defined in:
lib/alpr_cam/frame_capture.rb

Constant Summary collapse

CAPTURE_TIMEOUT =
10

Instance Method Summary collapse

Constructor Details

#initialize(device: "/dev/video0") ⇒ FrameCapture

Returns a new instance of FrameCapture.



11
12
13
# File 'lib/alpr_cam/frame_capture.rb', line 11

def initialize(device: "/dev/video0")
  @device = device
end

Instance Method Details

#captureObject



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
# File 'lib/alpr_cam/frame_capture.rb', line 15

def capture
  tmpfile = Tempfile.new(["alpr_frame_", ".jpg"])
  path = tmpfile.path
  tmpfile.close

  cmd = [
    "gst-launch-1.0",
    "v4l2src", "device=#{@device}", "num-buffers=1",
    "!", "videoconvert",
    "!", "jpegenc",
    "!", "filesink", "location=#{path}"
  ]

  begin
    _stdout, stderr, status = Timeout.timeout(CAPTURE_TIMEOUT) do
      Open3.capture3(*cmd)
    end
  rescue Timeout::Error
    raise CaptureError, "Frame capture timed out after #{CAPTURE_TIMEOUT}s for device #{@device}"
  end

  unless status.success?
    raise CaptureError, "Frame capture failed (exit #{status.exitstatus}): #{stderr}"
  end

  unless File.exist?(path) && File.size(path) > 0
    raise CaptureError, "Frame capture produced no output file"
  end

  path
end