Mike H-R Mike H-R - 10 months ago 59
Ruby Question

Can you use a webcam with ruby-opencv in realtime?

I'm thinking of building a quick and dirty script to help a friend out. It will need to take webcam input and perform very simple computation on it (think delta brightness of the pixels over time, relatively computationally simple).

I am currently leaning towards c++ like so along with opencv but I was wondering, since it's such a computationally inexpensive task, (relatively speaking of course) if there was a way to simply use ruby-opencv or some other techniques to keep it in a scripting language.

I haven't been able to find any way to import realtime webcamera images into a ruby script since I've been looking but I'd be more than happy to have my search skills proven inadequate by the wonderful SO community!

Places I've looked so far:


c++ script (what I'm leaning towards)

using an ffi

In summary; Is there a way to import realtime webcamera images into a ruby script to perform simple computation on them? (I was thinking with opencv but am not tied to any particular idea.


OpenCV has Python bindings which will call native C++ code under the hood. They also have many tutorials on how to use the Python API. A basic example of opening a video feed can be found here, and a longer list of tutorials is here. I highly recommend looking through them if you are interested in doing much work with OpenCV.

A brief summary of the first tutorial:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

    # Capture frame-by-frame
    ret, frame = cap.read()

    # Do image processing here, using frame

    # Display the resulting frame
    if cv2.waitKey(1) & 0xFF == ord('q'):

# When everything done, release the capture