Ask Your Question
0

How to check if webcam is busy

asked 2013-07-04 05:00:43 -0500

JeffElse gravatar image

updated 2013-07-04 08:41:01 -0500

Hello,

is there a way to check if webcam is busy before taking an image? Python.exe would crash if camera is in use and following code is running

Python code:

from cv2 import *
while True:
    #need to check here if camera is busy
    cam = VideoCapture(0)
    s, img = cam.read()
    if s:    # frame captured without any errors
        imwrite("filename.jpg",img) #save image
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-07-04 06:29:52 -0500

Basically what you need to do before reading an image from a cam element.

VideoCapture cap(0); // open the default camera
if(!cap.isOpened())  // check if we succeeded
    return -1;

This code will check if the cam opening actually worked. If the camera is busy, it will not get opened, since access isn't allowed, giving you a false for an opened camera socket.

edit flag offensive delete link more

Comments

Thx. I've tried that, but it still crashes.

JeffElse gravatar imageJeffElse ( 2013-07-04 08:29:46 -0500 )edit

Can you identify exactly at which command it crashes?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-07-04 08:39:39 -0500 )edit

it fails on release(). I can see the print output before it

if cam.isOpened(): print "release" cam.release()

JeffElse gravatar imageJeffElse ( 2013-07-04 13:13:25 -0500 )edit

it also crashes before cam = VideoCapture(0)

JeffElse gravatar imageJeffElse ( 2013-07-04 13:59:23 -0500 )edit

Try this:

  • Create a new VideoCapture element but do not initialize it yet by using VideoCapture capture;
  • Then use the bool success = capture.open(0);
  • Now check if the boolean value is true or false, before continuing.
StevenPuttemans gravatar imageStevenPuttemans ( 2013-07-05 02:29:59 -0500 )edit

not sure if I'm doing it right, python still crashes before cc = VideoCapture()

    cam = VideoCapture()
    success = False
    if not cam.isOpened():
        success = cam.open(0)

    if success: 
        ok, img = cam.read()
JeffElse gravatar imageJeffElse ( 2013-07-25 15:16:27 -0500 )edit

You are actually doing my suggestion, but yeah it seems you don't have succeed in getting the videoCapture class. Are u sure that you have all the correct libraries included?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-07-26 03:57:13 -0500 )edit

How do I know if all libraries are correct? If camera is not busy, it works. So I suppose, libraries are correct. Does it work for you?

JeffElse gravatar imageJeffElse ( 2013-07-26 04:17:20 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-07-04 05:00:43 -0500

Seen: 2,913 times

Last updated: Jul 04 '13