Thursday, 5 June 2014

webcam-photos-with-ruby

# require 'av_capture'
#
# # Create a recording session
# session = AVCapture::Session.new
#
# # Find the first video capable device
# dev = AVCapture.devices.find(&:video?)
#
# # Output the camera's name
# $stderr.puts dev.name
#
# # Connect the camera to the recording session
# session.run_with(dev) do |connection|
#
# # Capture an image and write it to $stdout
# $stdout.write connection.capture
# end
# require 'av_capture'
# require 'io/console'
#
# session = AVCapture::Session.new
# dev = AVCapture.devices.find(&:video?)
#
# session.run_with(dev) do |connection|
# loop do
# case $stdin.getch
# when 'q' then break # quit when you hit 'q'
# else
# IO.popen("open -g -f -a /Applications/Preview.app", 'w') do |f|
# f.write connection.capture
# end
# end
# end
# end
# Server code
# Here is our server code:
require 'av_capture'
require 'drb'
class PhotoServer
attr_reader :photo_request, :photo_response
def initialize
@photo_request = Queue.new
@photo_response = Queue.new
@mutex = Mutex.new
end
def take_photo
@mutex.synchronize do
photo_request << "x"
photo_response.pop
end
end
end
server = PhotoServer.new
Thread.new do
session = AVCapture::Session.new
dev = AVCapture.devices.find(&:video?)
session.run_with(dev) do |connection|
while server.photo_request.pop
server.photo_response.push connection.capture
end
end
end
URI = "druby://localhost:8787"
DRb.start_service URI, server
DRb.thread.join
# Client Code
# ------------------------------------------------
require 'drb'
SERVER_URI = "druby://localhost:8787"
photoserver = DRbObject.new_with_uri SERVER_URI
print photoserver.take_photo
# ------------------------------
# Running the code
# In one terminal, run the server code like this:
#
# $ ruby server.rb
# Then in another terminal, run the client code like this:
# $ ruby client.rb | open -f -a /Applications/Preview.app
# You should have a photo show up in Preview.app. You can kill the server program by doing Ctrl-C.
http://tenderlovemaking.com/2014/03/26/webcam-photos-with-ruby.html
view raw gistfile1.rb hosted with ❤ by GitHub

No comments:

Post a Comment