Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal webcam working examples #146

Open
myroslambda opened this issue Dec 31, 2019 · 4 comments
Open

Minimal webcam working examples #146

myroslambda opened this issue Dec 31, 2019 · 4 comments

Comments

@myroslambda
Copy link

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.

@myroslambda
Copy link
Author

Hey, @yorickvP . Do you know what may be going on here?

PS. I see that you are the only public member in the LumiGuide organization, that's why I pinged you. Sorry if you are not the person to ping about this.

@yorickvP
Copy link
Contributor

@roelvandijk

@myroslambda
Copy link
Author

@roelvandijk @yorickvP Hi! Have you had a chance to look into this?

@KommuSoft
Copy link

I would appreciate some help getting this minimal example working.

Hi, locally the following works to render the webcam image to a window:

main :: IO ()
main = do
    cap <- createCaptureArg
    w <- CV.videoCaptureGetI cap VideoCapPropFrameWidth
    h <- CV.videoCaptureGetI cap VideoCapPropFrameHeight
    CV.withWindow "video" $ loop cap
  where
    loop cap window = do
      _ok <- CV.videoCaptureGrab cap
      mbImg <- CV.videoCaptureRetrieve cap
      case mbImg of
        Just img -> do
          CV.imshow window (CV.unsafeCoerceMat img)
          key <- CV.waitKey 20
          -- Loop unless the escape key is pressed.
          unless (key == 27) $ loop cap window

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants