-3

When i try the below code in colab i didn't get any error and also didn't get any output. Also I tried the javascript to access the webcam.

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()
cigien
  • 57,834
  • 11
  • 73
  • 112

1 Answers1

0

As stated in here most probably the ret value returns false. As a result, the program can't get frames.

  • Option #1: Make sure you run the code with administrator privileges, since the program may not access to the camera.

  • Option #2: You could use if ret==True: statement, before converting the frame to the grayscale.

    For instance:

    while(True):
        ret, frame = cap.read()
        if ret:
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            # Rest of the code ..
    
Ahmet
  • 7,527
  • 3
  • 23
  • 47