ipython笔记本视频网络摄像头捕获实现

时间:2018-07-18 09:14:58

标签: opencv video ipython jupyter-notebook webcam-capture

我尝试实施Webcam based image processing in iPython notebooks

  • 问题1:vc = cv2.VideoCapture(0)不起作用。 cv2.VideoCapture(0)始终为False。是否需要更改笔记本设置才能连接到我的网络摄像头?

  • 问题2:cv2.VideoCapture('vid.mp4')运行,但仅显示空白并在末尾显示错误

错误:

34         frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)    # makes the blues image look real colored
         35         webcam_preview.set_data(frame)
         36         plt.draw()

    error: OpenCV(3.4.1) /opt/conda/conda-bld/opencv-suite_1530789967746/work/modules/imgproc/src/color.cpp:11115: error: (-215) scn == 3 || scn == 4 in function cvtColor
  

注意:我在云端运行笔记本服务器(ubuntu 16.04),并希望   ipython Notebook能够连接到访客网络摄像头...

import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logging.debug("test")

%matplotlib notebook

import cv2
import matplotlib.pyplot as plt
import signal
import sys
from IPython.display import clear_output, Image, display, HTML
import numpy as np

def signal_handler(signal, frame):
    # KeyboardInterrupt detected, exiting
    global is_interrupted
    is_interrupted = True

vc = cv2.VideoCapture(0)

plt.ion()

if vc.isOpened(): # try to get the first frame
    is_capturing, frame = vc.read()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)    # makes the blues image look real colored
    webcam_preview = plt.imshow(frame)    
else:
    is_capturing = False

while is_capturing:
    try:    # Lookout for a keyboardInterrupt to stop the script
        is_capturing, frame = vc.read()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)    # makes the blues image look real colored
        webcam_preview.set_data(frame)
        plt.draw()

        try:    # Avoids a NotImplementedError caused by `plt.pause`
            plt.pause(0.05)
        except Exception:
            pass
    except KeyboardInterrupt:
        vc.release()
#DEBUG:matplotlib.backends:backend nbAgg version unknown

0 个答案:

没有答案