Grabbing snapshots from webcams in ruby

2019-02-10 18:21发布

How can I take snapshots from a webcam in ruby? I know the webcam device is on /dev/video0, but how do I get a picture from it?

5条回答
劳资没心,怎么记你
2楼-- · 2019-02-10 18:46

The Video4Linux API involves sending special ioctls to the /dev/video* device, with data in packed structures; not something easy to do from Ruby. (It's not all that much fun from C, either.)

ruby-v4l is an extension library for capture pictures in Ruby using Video4Linux.

Orphaned in Debian because its maintainer hasn't been active since 2005, so if it doesn't work I don't think you'll be able to get much support.

A more modern solution would probably be to create proper libv4l binding for Ruby. However, I don't know of any present work on that.

查看更多
该账号已被封号
3楼-- · 2019-02-10 18:49

Since you're using Linux, try videodog. It's a small program, which does one thing and does that well: capturing a frame from your /dev/video0 device. From that point you can use any Ruby technique you want to process the JPEG image you got from videodog.

查看更多
Bombasti
4楼-- · 2019-02-10 18:56

With the ruby-opencv gem:

require "opencv"

capture = OpenCV::CvCapture.open
sleep 1 # Warming up the webcam
capture.query.save("image.jpg")
capture.close

And to install OpenCV on macOS: brew install homebrew/science/opencv --HEAD.

查看更多
贼婆χ
5楼-- · 2019-02-10 18:58

I've never actually used it but hornetseye looks good. This question has also already been asked here

查看更多
地球回转人心会变
6楼-- · 2019-02-10 19:04

I'm the developer of Hornetseye. You can capture images with the V4L2-interface of HornetsEye as follows.

require 'rubygems'
require 'hornetseye_v4l2'
require 'hornetseye_xorg'
require 'hornetseye_rmagick'
include Hornetseye
input = V4L2Input.new '/dev/video0'
img = X11Display.show { input.read }
img.to_ubytergb.save_ubytergb 'test.png'

Currently supported colourspaces are UYVY, YUYV, YUV420, GREY, RGB24. Note that other colourspaces such as MJPEG are not supported at the moment.

查看更多
登录 后发表回答