Deep Learning (ud730) - らんだむな記憶なんだけど、.ipynb を突っ込んだ場合に img src
での画像参照がインラインで表示されない。
- python - How to display an image within the notebook in Google Colab (like in Anacondan Jupyter Notebook)? - Stack Overflow
- html - How can I open images in a Google Colaboratory notebook cell from uploaded png files? - Stack Overflow
- How to insert an inline image in Google Colaboratory markdown call from local path - Stack Overflow
のようにすれば良いらしいので・・・
import os, sys, subprocess, glob, shutil from google.colab import drive drive.mount('/content/drive') img_dir = "/content/drive/My Drive/PATH/TO/IMG_DIR" shutil.copytree(img_dir, "/usr/local/share/jupyter/nbextensions/")
みたいな感じにコードを実行させてGoogle drive上の画像データをColabのコンテナにコピーして使う。
.ipynb は以下のようなコードで画像の参照パスを予め変更しておく。jsonを真面目にパースして分割するのは面倒臭いので正規表現で逃げる・・・。
#! /usr/bin/env python # -*- coding: utf-8 -*- import os, sys, re def insert_code_cell_between_markdowns(lines, line, prefix=""): lines[-1] = re.sub(r"\s*,\s*$", "", lines[-1]) lines.append(f' ]\n }},\n {{\n "cell_type": "code",\n "metadata": {{}},\n "source": [{prefix}') lines.append(line) lines.append(' ]\n },\n {\n "cell_type": "markdown",\n "metadata": {},\n "source": [') def proc_img_line(lines, line): line = re.sub(r"\s*,\s*$", "", line) line = re.sub(r"'(\S+)'", r"'/nbextensions/\1'", line) line = re.sub(r"\\\"(\S+)\\\"", r"\"/nbextensions/\1\"", line) insert_code_cell_between_markdowns(lines, line, prefix='\n "%%html\\n",') def proc_img_line2(lines, line): """ A helper function like below is assumed to be defined in advance: def display_image(url): from IPython.display import Image, display url = os.path.join(root_dir, url) display(Image(url)) """ m = re.search(r"'(\S+)'", line) if m: url = m.group(1) line = f' "display_image(\\\"{url}\\\")"' insert_code_cell_between_markdowns(lines, line) def main(): path = sys.argv[1] dirname = os.path.dirname(path) basename, ext = os.path.splitext(os.path.basename(path)) out_path = os.path.join(dirname, "{}_colab{}".format(basename, ext)) lines = [] with open(path) as fin: for line in fin.readlines(): line = line.rstrip() if re.search(r"img.*src", line): proc_img_line2(lines, line) else: lines.append(line) with open(out_path, "w") as fout: for line in lines: print(line, file=fout) if __name__ == "__main__": main()