Skip to content

Minimal webcam working examples #146

Open
@myroslambda

Description

@myroslambda

Hi,

I'm trying to make this work and create a tiny example.

The following CPP code works just fine:

#include "opencv2/opencv.hpp"
using namespace cv;
int main(int argc, char** argv)
{
    VideoCapture cap;
    // open the default camera, use something different from 0 otherwise;
    // Check VideoCapture documentation.
    if(!cap.open(0))
        return 0;
    for(;;)
    {
          Mat frame;
          cap >> frame;
          if( frame.empty() ) break; // end of video stream
          imshow("this is you, smile! :)", frame);
          if( waitKey(10) == 27 ) break; // stop capturing by pressing ESC
    }
    // the camera will be closed automatically upon exit
    // cap.close();
    return 0;
}

My attempt at creating something based on this fails (it compiles, it runs, it does not crash, but the webcam light does not turn green and no window is shown):

module Main where

import Control.Monad
import OpenCV
import OpenCV.VideoIO.Types

main :: IO ()
main = do
  win <- makeWindow "Test"
  cap <- newVideoCapture
  exceptErrorIO $ videoCaptureOpen cap (VideoDeviceSource 0 (Just VideoCapV4l))
  forever $ do
    mbFrameRaw <- videoCaptureRetrieve cap
    case mbFrameRaw of
      Nothing -> putStrLn "No frame"
      Just frameRaw ->
        imshow win frameRaw

The V4l was Nothing before, and that did not change anything. I tried to execute videoCaptureGrab before the loop, but that did not help either. I've already looked inside the API, as well as in the examples included with the library, but they tend to be quite convoluted and they did not help me understand what's wrong with this particular code.

I would appreciate some help getting this minimal example working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions