42

运行 OpenCV 视频处理 python 教程中的示例时,它们都会在专用窗口中弹出。我知道 IPython notebook 可以显示来自磁盘和 YouTube 的视频,所以我想知道是否有办法将 OpenCV 视频播放定向到 Notebook 浏览器并让它在输出单元格而不是单独的窗口中播放(最好不保存它到磁盘,然后从那里播放)。

以下是 OpenCV 教程中的代码。

import cv2

cap = cv2.VideoCapture('/path/to/video') 

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()
4

5 回答 5

7

为了使显示更快,只IPython.display.display在笔记本内部使用 JPG 格式而不是 PNG。(注意cv2.imshow在笔记本外以本地方式显示要快得多,但这不是问题所要求的):

下面的代码将测试所有支持的文件格式以找到最快的文件格式(使用正则表达式提取__doc__,不可靠)

from IPython.display import clear_output, Image, display, HTML
import cv2

# Read one frame from the camera for testing
video = cv2.VideoCapture(0)
_, frame = video.read()
video.release()

import re
from timeit import timeit
import math

extensions=re.findall(r"\\\*(\.\w*)", cv2.imread.__doc__)

def test(extension):
    try:
        totalTime=0
        numTry=3
        for _ in range(numTry):
            totalTime+=timeit(lambda: display(Image(data=cv2.imencode(extension, frame)[1])), number=1)
            clear_output(wait=True)
        return totalTime/numTry, extension

    except cv2.error as e: #usually "unsupported file type"
        return (math.inf, extension, e)
for x in sorted(
    [test(extension) for extension in extensions], key=lambda x: x[0]
): print(x)

就我而言,.jpeg是最快的。确保浏览器显示也支持该扩展:

Image(data=cv2.imencode(".jpeg", frame)[1].tobytes())

然后,播放视频:

import cv2
from IPython.display import display, Image

video = cv2.VideoCapture(0)
display_handle=display(None, display_id=True)
try:
    while True:
        _, frame = video.read()
        frame = cv2.flip(frame, 1) # if your camera reverses your image
        _, frame = cv2.imencode('.jpeg', frame)
        display_handle.update(Image(data=frame.tobytes()))
except KeyboardInterrupt:
    pass
finally:
    video.release()
    display_handle.update(None)

update每次都比clear_output+快一点;display但是与渲染相比,它并不是一个显着的改进。

于 2020-12-31T08:20:04.460 回答
7

你可以用 Bokeh 做到这一点,而且可能会快一点。

from bokeh.plotting import figure
from bokeh.io import output_notebook, show, push_notebook
import cv2
import time
output_notebook()

cap = cv2.VideoCapture(0)
ret, frame = cap.read()
frame=cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) # because Bokeh expects a RGBA image
frame=cv2.flip(frame, -1) # because Bokeh flips vertically
width=frame.shape[1]
height=frame.shape[0]
p = figure(x_range=(0,width), y_range=(0,height), output_backend="webgl", width=width, height=height)
myImage = p.image_rgba(image=[frame], x=0, y=0, dw=width, dh=height)
show(p, notebook_handle=True)
while True:
    ret, frame = cap.read()
    frame=cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    frame=cv2.flip(frame, -1)
    myImage.data_source.data['image']=[frame]
    push_notebook()
    time.sleep(0.3)
于 2018-10-28T17:50:11.423 回答
2

视频编码数据(如果是浏览器可以解码的格式,例如 ISO mp4 容器中的 h264 编码)可以使用 HTML<video>标签显示IPython.core.display.HTML(),这将提供标准播放性能。

<video>可以是链接,也可以嵌入 base64 的数据(例如,后者就是这样做matplotlib.animation的),并且它的数据当然可以使用 OpenCV(例如VideoWriter)在您的笔记本中生成。

于 2017-07-05T16:18:16.770 回答
2

是的。但它会slooowwww....

从网络摄像头读取的 Python 3 和 OpenCV 3.3 代码(从文件中,只需更改 cv2.VideoCapture("filename.mp4")):

from IPython.display import clear_output, Image, display, HTML
import numpy as np
import cv2
import base64

def arrayShow (imageArray):
    ret, png = cv2.imencode('.png', imageArray)
    encoded = base64.b64encode(png)
    return Image(data=encoded.decode('ascii'))
video = cv2.VideoCapture(0)
while(True):
    try:
        clear_output(wait=True)
        _, frame = video.read()
        lines, columns, _ =  frame.shape
        frame = cv2.resize(frame, (int(columns/4), int(lines/4))) 
        img = arrayShow(frame)
        display(img)
    except KeyboardInterrupt:
        video.release()

您可能需要更改 IOPub 数据速率限制。您可以在 .jupyter 配置中更改它,或者只运行 jupyter notebook --NotebookApp.iopub_data_rate_limit=1000000000

但是,键盘中断不能正常工作。

于 2018-03-28T04:45:04.493 回答
0
from IPython.display import clear_output, Image, display

import ipywidgets

import cv2

video = cv2.VideoCapture(0)

display_handle=display(None, display_id=True)

image_widget = ipywidgets.Image(format='jpeg')


while True:

    try:
        clear_output(wait=True)
        _, frame = video.read()
        lines, columns, _ =  frame.shape
        frame = cv2.resize(frame, (int(columns/4), int(lines/4))) 
        image_widget.value =cv2.imencode('.jpeg', frame)[1].tobytes()
        display(image_widget)
    except KeyboardInterrupt:
        video.release()
        break
于 2021-09-26T19:30:17.383 回答