Last active
January 28, 2022 09:20
-
-
Save 3t14/62d2305720fb3ca7d34d0effe7dc2b3a to your computer and use it in GitHub Desktop.
Colab上でNumPy配列の複数の画像を描画するためのコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!apt -qq -y install xvfb freeglut3-dev ffmpeg | |
!pip -q install pyopengl | |
!pip -q install pyvirtualdisplay | |
# 結果出力描画のための準備 | |
from pyvirtualdisplay import Display | |
import cv2 | |
from IPython.display import HTML | |
import numpy as np | |
from base64 import b64encode | |
display = Display(visible=0, size=(1024, 768)) | |
display.start() | |
# 得られたフレーム画像をframes.webmとして保存し、再生するためのHTMLを返す | |
def showFrames(frames, msg=""): | |
if type(frames) == list: | |
frames = np.array(frames) | |
filename = 'frames.webm' | |
fourcc = cv2.VideoWriter_fourcc('V', 'P', '8', '0') | |
video_writer = cv2.VideoWriter(filename, fourcc, 5.0, (frames.shape[2], frames.shape[1]), isColor=True) | |
step = 0 | |
for frame in frames: | |
# numpy配列に変換 | |
cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) | |
cv2.putText(frame, 'Step.%d :%s' % (step, msg), (10, 32), cv2.FONT_HERSHEY_DUPLEX, 1.0, (0, 0, 0), thickness=1) | |
video_writer.write(frame) | |
step += 1 | |
video_writer.release() | |
webm = open(filename,'rb').read() | |
data_url = "data:video/webm;base64," + b64encode(webm).decode() | |
return HTML(""" | |
<video width=400 controls> | |
<source src="%s" type="video/webm"> | |
</video> | |
""" % data_url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment