在jupyter notebook中将HTML表单转换为python代码



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

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

"在示例图像上运行"部分中有一个表单。如何使用脚本代替该表单?我正在使用不支持创建此类表单的 Jupyter 笔记本。

如果在 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字段填充您喜欢的任何值并正常运行单元格。

最新更新