在 jupyter notebook 中使用 opencv 显示来自网络摄像头的图像

김진우*_*김진우 5 python opencv python-3.x

我正在尝试显示网络摄像头捕获的图像。但是,存储捕获图像的变量是空的。此问题仅在使用网络摄像头时出现,在播放 mp4 等视频时不会出现。这个问题是不是python版本导致的?我的 OpenCV 版本是 3.4.3,Python 版本是 3.6.5。

import numpy as np
import cv2
from IPython import display
import matplotlib.pyplot as py
%matplotlib inline

def showVideo():
    try:
        print('???? ?????.')
        cap = cv2.VideoCapture(0)
    except:
        print('??? ?? ??')
        return
    print(cap.isOpened())
    print(cap.read())
    cap.set(3, 480)
    cap.set(4, 320)

    try:
        while(True):
            # Capture frame-by-frame
            ret, frame = cap.read()
            if not ret:
                # Release the Video Device if ret is false
                cap.release()
                # Message to be displayed after releasing the device
                print ("Released Video Resource")
                break
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            # Turn off the axis
            py.axis('off')
            # Title of the window
            py.title("Input Stream")
            # Display the frame
            py.imshow(frame)
            py.show()
            # Display the frame until new frame is available
            display.clear_output(wait=True)
    except KeyboardInterrupt:
        # Release the Video Device
        cap.release()
        # Message to be displayed after releasing the device
        print("Released Video Resource")
    pass
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明