I need to write one simple project and i'm using opencv, ruby and mac. I've installed opencv through brew and rb_webcam through gem install.
# -*- coding: utf-8 -*-
require "opencv"
require "rb_webcam"
capture = Webcam.new
This code throws
$ ruby tracking.rb
/Users/evilgeniuz/.rvm/gems/ruby-1.9.3-p125/gems/nice-ffi-0.4/lib/nice-ffi/library.rb:98:in `load_library': Could not load highgui. (LoadError)
from /Users/evilgeniuz/.rvm/gems/ruby-1.9.3-p125/gems/rb_webcam-0.3.0/lib/rb_webcam.rb:7:in `<module:Highgui>'
from /Users/evilgeniuz/.rvm/gems/ruby-1.9.3-p125/gems/rb_webcam-0.3.0/lib/rb_webcam.rb:4:in `<top (required)>'
from /Users/evilgeniuz/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/rubygems/custom_require.rb:59:in `require'
from /Users/evilgeniuz/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/rubygems/custom_require.rb:59:in `rescue in require'
from /Users/evilgeniuz/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from tracking.rb:4:in `<main>'
I can't get how can i point where highgui is.
UPD: Solved it by downloading gem from here https://github.com/TyounanMOTI/rb_webcam and building and installing it manually.
Not sure which wrapper you are using, but you should take a look at this: Ruby/OpenCV - An OpenCV Ruby wrapper.
Face detection sample:
#!/usr/bin/env ruby
require 'opencv'
include OpenCV
# Load an image
img = IplImage.load('sample.jpg')
# Load the cascade for detecting faces
detector = CvHaarClassifierCascade::load('haarcascade_frontalface_alt.xml.gz')
# Detect faces and draw rectangles around them
detector.detect_objects(img) do |rect|
img.rectangle!(rect.top_left, rect.bottom_right, color: CvColor::Red)
end
# Create a window and show the image
window = GUI::Window.new('Face Detection')
window.show(img)
GUI::wait_key
The classifier can be downloaded here.
EDIT:
The following code uses OpenCV, the rb_webcam gem, and RMagick to capture an image from a webcam and save it as a jpg file:
require 'rb_webcam'
require 'RMagick'
capture = Webcam.new
image = capture.grab
width = image.size[:width]
rows = image.data.unpack("C*").each_slice(3).to_a.each_slice(width).to_a
capture.close
height = rows.length
img = Magick::Image.new width, height
rows.each_with_index do |r, i|
q = r.map {|b, g, r| Magick::Pixel.new r * 256, g * 256, b * 256, 0}
img.store_pixels(0, i, width, 1, q)
end
img.format = 'jpg'
img.write 'webcam.jpg'
Maybe it's too late, but I was struggling too, getting my webcam to work until in hopeless rage I accidentally tried:
#!/usr/bin/env ruby
require "rubygems"
gem "ruby-opencv"
require "opencv"
window = OpenCV::GUI::Window.new("webcam")
capture = OpenCV::CvCapture.open
while true
key = OpenCV::GUI::wait_key(1)
image = capture.query
window.show image
next unless key
case key.chr
when "\e"
exit
end
end
Hope this helps, cause I gave up on rb_webcam
after over a week highgui trouble.
With the ruby-opencv
gem:
require "opencv"
capture = OpenCV::CvCapture.open
sleep 1 # Warming up the webcam
capture.query.save("image.jpg")
capture.close
(Installing OpenCV on macOS: brew install homebrew/science/opencv --HEAD
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With