首页 > 解决方案 > 在 jupyter notebook 中将 HTML 表单转换为 python 代码

问题描述

我能够运行除此 colab 页面中的最后一个单元之外的所有单元。

https://colab.research.google.com/github/tensorflow/models/blob/master/research/deeplab/deeplab_demo.ipynb

“在示例图像上运行”部分中有一个表格。我如何使用脚本而不是那种形式?我正在使用不支持创建此类表单的 Jupyter 笔记本。

标签: pythonjupyter-notebookgoogle-colaboratory

解决方案


如果你双击 colab 中的表单,你可以看到它后面的代码:

SAMPLE_IMAGE = 'image1'  # @param ['image1', 'image2', 'image3']
IMAGE_URL = ''  #@param {type:"string"}

_SAMPLE_URL = ('https://github.com/tensorflow/models/blob/master/research/'
               'deeplab/g3doc/img/%s.jpg?raw=true')


def run_visualization(url):
  """Inferences DeepLab model and visualizes result."""
  try:
    f = urllib.request.urlopen(url)
    jpeg_str = f.read()
    original_im = Image.open(BytesIO(jpeg_str))
  except IOError:
    print('Cannot retrieve image. Please check url: ' + url)
    return

  print('running deeplab on image %s...' % url)
  resized_im, seg_map = MODEL.run(original_im)

  vis_segmentation(resized_im, seg_map)


image_url = IMAGE_URL or _SAMPLE_URL % SAMPLE_IMAGE
run_visualization(image_url)

如果您将此代码原样复制到您自己的 jupyter 实例中的新单元格中,您现在可以为@param字段填充您喜欢的任何值并正常运行该单元格。


推荐阅读