Versioned fork of the OpenCV gem for Ruby
Go to file
ser1zw bcf9dc5a0f
Merge pull request #99 from fdeschenes/experimental/opencv3
Basic OpenCV 3.4.1 DNN Support
2018-07-29 15:50:14 +09:00
examples Added example. 2018-07-24 22:44:08 -07:00
ext/opencv Fixes segmentation faults when deleting layers. 2018-07-27 11:18:25 -07:00
lib add CvCapture 2016-04-10 20:11:32 +09:00
test Tweaked the test a bit. 2018-07-26 22:55:30 -07:00
.gitignore fix #77 : ignore symlinks for development 2016-05-11 03:17:57 +09:00
.yardopts update .yardopts 2014-03-30 17:43:44 +09:00
Gemfile fix Gemfile 2016-04-03 05:37:13 +09:00
LICENSE.txt experimental implementation for OpenCV 3 2016-04-03 05:19:48 +09:00
README.md Updated to support 3.3.1+. 2018-07-25 22:20:20 -07:00
Rakefile experimental implementation for OpenCV 3 2016-04-03 05:19:48 +09:00
ruby-opencv.gemspec change module name to Cv 2016-04-03 07:47:06 +09:00
yard_extension.rb fix documentation script 2016-05-02 03:31:30 +09:00

README.md

ruby-opencv

An OpenCV wrapper for Ruby.

Requirement

Install

Linux/Mac

  1. Install OpenCV
  2. Install ruby-opencv
$ gem install ruby-opencv -- --with-opencv-dir=/path/to/opencvdir

Note: /path/to/opencvdir is the directory where you installed OpenCV.

Windows

You can use pre-build binary for Windows (mswin32).

  1. Install OpenCV
  2. Set path to OpenCV libraries. When you installed OpenCV to C:\opencv, add C:\opencv\build\x86\vc10\bin to the systems path.
  3. Install ruby-opencv
$ gem install ruby-opencv

Sample code

Load and Display an Image

A sample to load and display an image. An equivalent code of this tutorial.

require 'opencv'

if ARGV.size == 0
  puts "Usage: ruby #{__FILE__} ImageToLoadAndDisplay"
  exit
end

image = nil
begin
  image = Cv::imread(ARGV[0], Cv::CV_LOAD_IMAGE_COLOR) # Read the file.
rescue
  puts 'Could not open or find the image.'
  exit
end

window = Cv::Window.new('Display window') # Create a window for display.
window.show(image) # Show our image inside it.
Cv::wait_key # Wait for a keystroke in the window.

Face Detection

A sample to detect faces from an image.

require 'opencv'

if ARGV.length < 1
  puts "Usage: ruby #{__FILE__} image"
  exit
end

classifier = Cv::CascadeClassifier.new('examples/haarcascade_frontalface_alt.xml')
image = Cv::imread(ARGV[0], -1)

color = Cv::Scalar.new(0, 255, 255)
classifier.detect_multi_scale(image).each do |r|
  pt1 = Cv::Point.new(r.x, r.y)
  pt2 = Cv::Point.new(r.x + r.width, r.y + r.height)
  image.rectangle!(pt1, pt2, color, thickness: 3, line_type: Cv::CV_AA)
end

window = Cv::Window.new('Face detection')
window.show(image)
Cv::wait_key

Image Classification

A samples to classify objects in an image.

require 'opencv'

classes = []
File.open("./examples/synset_words.txt", "r") do |f|
  f.each_line { |line|
    _, value = line.strip.split(" ", 2)
    classes << value.split(",", 2).first
  }
  f.close
end

net = Cv::Dnn.read_net_from_caffe("./examples/bvlc_googlenet.prototxt", "./examples/bvlc_googlenet.caffemodel")
net.set_input(Cv::Dnn.blob_from_image(Cv.imread("./examples/images/stuff.jpg", Cv::IMREAD_UNCHANGED), size: Cv::Size.new(224, 224), mean: Cv::Scalar.new(104, 117, 123)))
predictions = net.forward

for i in 0..(predictions.cols - 1)
  confidence = predictions.at(0, i)[0]
  puts "#{classes[i]} #{confidence}" if confidence > 0.1
end

For more samples, see examples/*.rb

LICENSE:

The MIT Liscense

see LICENSE.txt