在jupyter noteb中使用opencv显示网络摄像头中的图像

2024-03-29 13:45:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试显示网络摄像头拍摄的图像。但是,存储捕获图像的变量是空的。此问题仅在使用网络摄像头时出现,而不是在播放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

enter image description here


Tags: thepy图像import版本网络asvideo