joe*_*lom 28 video opencv ipython ipython-notebook jupyter
当从OpenCV视频处理python教程运行示例时,它们都会弹出一个专用窗口.我知道IPython笔记本可以显示来自磁盘和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()
Run Code Online (Sandbox Code Playgroud)
use*_*729 10
为了使显示速度更快,仅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)
Run Code Online (Sandbox Code Playgroud)
就我而言,.jpeg是最快的。确保浏览器显示也支持该扩展:
Image(data=cv2.imencode(".jpeg", frame)[1].tobytes())
Run Code Online (Sandbox Code Playgroud)
然后,播放视频:
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)
Run Code Online (Sandbox Code Playgroud)
update每次都比clear_output+快一点;display然而与渲染相比,这并不是一个显着的改进。
小智 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)
Run Code Online (Sandbox Code Playgroud)
视频编码数据(如果采用浏览器可以解码的格式,例如 ISO mp4 容器中的 h264 编码)可以使用 HTML 标签<video>和来显示IPython.core.display.HTML(),这将提供标准的播放性能。
它<video>可以是一个链接,或者嵌入了 base64'ed 数据(matplotlib.animation例如后者的作用),并且它的数据当然可以使用 OpenCV 在您的笔记本中生成(例如VideoWriter)。
| 归档时间: |
|
| 查看次数: |
13657 次 |
| 最近记录: |