3

My opencv installation recently stopped working for reasons I'm not sure of. I have two scripts that all give different errors:

Script A:

import cv2
cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()
    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)

    cv2.imshow('frame', rgb) 

gives me this error:

select timeout
VIDIOC_DQBUF: Resource temporarily unavailable
Traceback (most recent call last):
  File "camera.py", line 19, in <module>
    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.error: OpenCV(4.0.1-dev) /home/me/Packages/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
import cv2
cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()
    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)

    cv2.imshow('frame', rgb)

And Script B:

import cv2

def show_webcam(mirror=False):
    cam = cv2.VideoCapture(0)
    while True:
        ret_val, img = cam.read()
        if mirror: 
            img = cv2.flip(img, 1)
        cv2.imshow('my webcam', img)
        if cv2.waitKey(1) == 27: 
            break  # esc to quit
    cv2.destroyAllWindows()

def main():
    show_webcam(mirror=True)

gives me this error:

select timeout
VIDIOC_DQBUF: Resource temporarily unavailable
Traceback (most recent call last):
  File "camera3.py", line 26, in <module>
    main()
  File "camera3.py", line 22, in main
    show_webcam(mirror=True)
  File "camera3.py", line 15, in show_webcam
    cv2.imshow('my webcam', img)
cv2.error: OpenCV(4.0.1-dev) /home/david/Packages/opencv/modules/highgui/src/window.cpp:352: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow' 

Here's the main issue: I've used OpenCV before and both of those errors usually occur when opencv can't find the webcam. But I do have a webcam attached, and when I open Cheese Webcam Booth it works fine, and takes pictures fine. Is there a way to repair this without reinstalling OpenCV?

I'm on Ubuntu 18.04.

NewCTwo
  • 31
  • 1
  • 2

3 Answers3

0

I would review the OpenCV installation, since Script A works just fine by me, on the same version of the library and similar OS (Mint 19).

import cv2
    cap = cv2.VideoCapture(0)

    while(True):
        ret, frame = cap.read()
        rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)

        cv2.imshow('frame', rgb) 
        cv2.waitKey(10)

The installation through

pip install opencv-python

is known for having "problems" with 3rd party modules (https://github.com/opencv/opencv/issues/8471).

Try building and installing from source, this way the interface modules will be built and linked as well (V4L, FFMpeg, etc).

Guilherme Vogt
  • 315
  • 3
  • 10
  • I still believe there should be some dependency issue. Check you cmake log and maybe run this guy (https://github.com/jayrambhia/Install-OpenCV/blob/master/Ubuntu/dependencies.sh) before doing it a _third_ time. – Guilherme Vogt Feb 05 '19 at 22:35
  • I have done that, and it's now giving me a cuda/cudnn error. – NewCTwo Feb 07 '19 at 15:08
0

Two possible solutions: 1) set the correct fps; 2) upgrade to the newer version of opencv for python.

GBY
  • 1,090
  • 1
  • 10
  • 13
0

in both cases, the camera was activated but is now occupied. So now, in the main "while" loop try putting

while(True):
      ret_val, img = cap.read()
      if(ret_val==False)
            cap.open(0)
            continue
      ...