How to read video from a webcam using OpenCV?

I am following the official documentation trying to read a video from a webcam. When I ran part of the code from the documentation:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

it does not work with the following output:

OpenCV error: statement failed (scn == 3 || scn == 4) in cvtColor, file /io/opencv/modules/imgproc/src/color.cpp, line 9748
Traceback (last last call):
  File "capture. py ", line 11, in
    gray = cv2.cvtColor (frame, cv2.COLOR_BGR2GRAY)
cv2.error: /io/opencv/modules/imgproc/src/color.cpp: 9748: error: (-215) scn == 3 || scn == 4 in the cvtColor function

After examining it, it turned out that:

  • retalways False.
  • cap.get(3) ( ) 0.0.
  • cap.isOpened(), False.
  • cap.open(0), open False.
  • cap.open("/dev/video0") .
  • cap = cv2.VideoCapture(0) cap = cv2.VideoCapture("/dev/video0") .

/dev/video0 VLC -. , -. .

?

+4
1

, FFMPEG. OpenCV , , .., ffmpeg.

FFmpeg Windows http://www.wikihow.com/Install-FFmpeg-on-Windows. Linux .

0

Source: https://habr.com/ru/post/1678469/


All Articles