global /io/opencv/modules/videoio/src/cap_v4l.cpp (880) open VIDEOIO(V4L2): can't find camera device

asked 2020-07-27 17:56:41 -0600

updated 2020-08-05 23:24:32 -0600

supra56 gravatar image

I keep getting this error when I run this code on the ubuntu bash 18.04 terminal: Here is my code, the tensorflow part works just fine it's only the opencv part that is giving me an error.

import cv2
from darkflow.net.build import TFNet
import numpy as np
import time

options = {
    'model': 'cfg/yolo.cfg',
    'load': 'bin/yolo.weights',
    'threshold': 0.2,
    'gpu': 1.0
}

tfnet = TFNet(options)
colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]

capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

while True:
    stime = time.time()
    ret, frame = capture.read()
    if ret:
        results = tfnet.return_predict(frame)
        for color, result in zip(colors, results):
            tl = (result['topleft']['x'], result['topleft']['y'])
            br = (result['bottomright']['x'], result['bottomright']['y'])
            label = result['label']
            confidence = result['confidence']
            text = '{}: {:.0f}%'.format(label, confidence * 100)
            frame = cv2.rectangle(frame, tl, br, color, 5)
            frame = cv2.putText(
                frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
        cv2.imshow('frame', frame)
        print('FPS {:.1f}'.format(1 / (time.time() - stime)))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

capture.release()
cv2.destroyAllWindows()

python: 3.6.7 ubuntu: 18.04 opencv: 4.3.0.36

edit retag flag offensive close merge delete

Comments

it's not your code, you have a hardware / driver problem (and we probably can't help)

berak gravatar imageberak ( 2020-07-28 02:51:57 -0600 )edit

As for Ubuntu. Try this capture = cv2.VideoCapture(-1). Is your webcam is USB?

supra56 gravatar imagesupra56 ( 2020-08-06 07:57:54 -0600 )edit