6

我需要编写一个简单的项目,我正在使用 opencv、ruby 和 mac。我已经通过 brew 安装了 opencv,并通过 gem install 安装了 rb_webcam。

# -*- coding: utf-8 -*-

require "opencv"
require "rb_webcam"

capture = Webcam.new

此代码抛出

$ 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>'

我不知道我怎么能指出 highgui 在哪里。

UPD:通过从这里下载 gem 解决它https://github.com/TyounanMOTI/rb_webcam并手动构建和安装它。

4

5 回答 5

4

不确定您使用的是哪个包装器,但您应该看看这个:Ruby/OpenCV - An OpenCV Ruby wrapper

人脸检测示例:

#!/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

分类器可以在这里下载。

编辑

以下代码使用 OpenCV、rb_webcam gem 和 RMagick 从网络摄像头捕获图像并将其保存为 jpg 文件:

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' 
于 2012-04-18T18:52:56.427 回答
2

也许为时已晚,但我也在苦苦挣扎,让我的网络摄像头工作,直到我不小心尝试了绝望的愤怒:

#!/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

rb_webcam希望这会有所帮助,因为我在一个多星期的 highgui 麻烦后放弃了。

于 2016-03-25T23:23:42.913 回答
1

使用ruby-opencv宝石:

require "opencv"

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

(在 macOS 上安装 OpenCV brew install homebrew/science/opencv --HEAD:)

于 2016-10-08T22:37:54.810 回答
0

开箱即用的最佳跨平台

<!doctype html>
<html lang="de">
<head>
<style type="text/css">
        form > input { margin-right: 15px; }
        #results { float:center; margin:5px; padding:5px; border:1px solid; background:#ccc; }
    </style>
</head>
<table class="reference notranslate"> 
 <tr>
    <th>Webcam</th>
    <th>Your captured image will appear here..</th>
 </tr>
 </tr>
    <td>
<video autoplay id="vid" width="320" height="240" style="border:1px solid #d3d3d3;"></video></br>
<button onclick="snapshot()">Take Picture</button>
    </td>
    <td>
<div id="results">Your captured image will appear here...<
<canvas id="canvas" width="320" height="240"></canvas>
</div>
<br>

 <button onclick="upload()">upload</button>
    </td>
 </tr>
</table>
<script type="text/javascript">

    var video = document.querySelector("#vid");
    var canvas = document.querySelector('#canvas');
    var ctx = canvas.getContext('2d');
    ctx.scale(0.5,0.5);
    var localMediaStream = null;      



    var onCameraFail = function (e) {
        console.log('Camera did not work.', e);
    }

    function snapshot() {
        if (localMediaStream) {
            ctx.drawImage(video, 0, 0);

    } 
    }
    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||  navigator.mozGetUserMedia || navigator.msGetUserMedia;
    window.URL = window.URL || window.webkitURL;
    navigator.getUserMedia({video:true}, function (stream) {
        video.src = window.URL.createObjectURL(stream);
        localMediaStream = stream;
    }, onCameraFail);

 redirectTime = "1500";

function timedRedirect() {

    setTimeout("history.back();",redirectTime);
};

function upload()  {
var oCanvas = document.getElementById("canvas");  
Canvas2Image.saveAsPNG(oCanvas);  // will prompt the user to save the image as PNG 
var oImgPNG = Canvas2Image.saveAsPNG(oCanvas, true);   
 JavaScript:timedRedirect()

};


</script>

-cartasu-:-)忘记了他的密码

于 2016-10-10T13:30:34.653 回答
0

至少在 Windows 上,我们应该在 load_library 中指定版本号,如下 rb_webcam.rb 第 7 行:

load_library("opencv_highgui2413")
于 2016-10-11T05:19:35.000 回答