Last active
January 26, 2022 02:52
-
-
Save 3t14/f054924c60da68212c32693c27383469 to your computer and use it in GitHub Desktop.
OpenAI GymをColab上で動作させるための実行環境準備
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 update | |
!apt -qq -y install libnvtoolsext1 | |
!apt -qq -y install xvfb freeglut3-dev ffmpeg | |
!pip -q install gym | |
!pip -q install pyglet | |
!pip -q install pyopengl | |
!pip -q install pyvirtualdisplay | |
# 結果出力描画のための準備 | |
from pyvirtualdisplay import Display | |
display = Display(visible=0, size=(1024, 768)) | |
display.start() | |
import cv2 | |
from IPython.display import HTML | |
import numpy as np | |
from base64 import b64encode | |
# 得られたフレーム画像をframes.webmとして保存し、再生するためのHTMLを返す | |
def showFrames(frames): | |
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, 30.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' % step, (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