Skip to content Skip to sidebar Skip to footer

Opencv Imshow Function Display Black Image In Jupyter Notebook

I use the following code copied from opencv website : import cv2 cap = cv2.VideoCapture(0) while(True): # Capture frame-by-frame ret, frame = cap.read() cv2.imshow('fr

Solution 1:

Try this, you can use isOpened() to ensure that you can connect to the camera.

from threading import Thread
import cv2, time

classVideoStreamWidget(object):
    def__init__(self, src=0):
        self.capture = cv2.VideoCapture(src)
        # Start the thread to read frames from the video stream
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True
        self.thread.start()

    defupdate(self):
        # Read the next frame from the stream in a different threadwhileTrue:
            if self.capture.isOpened():
                (self.status, self.frame) = self.capture.read()
            time.sleep(.01)

    defshow_frame(self):
        # Display frames in main program
        cv2.imshow('frame', self.frame)
        key = cv2.waitKey(1)
        if key == ord('q'):
            self.capture.release()
            cv2.destroyAllWindows()
            exit(1)

if __name__ == '__main__':
    video_stream_widget = VideoStreamWidget()
    whileTrue:
        try:
            video_stream_widget.show_frame()
        except AttributeError:
            pass

Solution 2:

I've had the same issue with my webcams with spyder. What worked for me was to change from python 3.7 to python 3.6

-> conda install python=3.6

Solution 3:

If you are using an external webcam then you have use cv2.VideoCapture(1) instead of cv2.VideoCapture(0).

Because here 0 represent internal webcam and 1 represent external webcam

import cv2
cap = cv2.VideoCapture(1)
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Post a Comment for "Opencv Imshow Function Display Black Image In Jupyter Notebook"