如何关闭Holoviews Bokeh服务器应用程序(“ BokehRenderer.app”方法)并继续运行脚本

时间:2019-12-04 00:22:27

标签: python bokeh holoviews

我正在创建一个脚本,该脚本需要一些用户输入来进行图像注释,并且我正在使用BokehRenderer.app来执行此任务。现在,当我运行脚本时,让图像由用户注释,我找不到自动关闭应用程序并继续运行脚本的方法。我发现的唯一(不明确的)方法是返回命令行并执行CTRL+C几次以继续运行。您对如何更优雅地进行此操作有任何建议吗?下面是我的代码:

import argparse
import numpy as np
import cv2
import holoviews as hv
from holoviews import streams

def annotate_video(image):
    """ annotate_video
    Uses Holoviews to show and provide interactive interface to annotate coordinates in image

    input: image
                numpy 2D array
    output: pointDraw_stream
                dictionary where pointDraw_stream['x'] and ['y'] are annotated coordinates
    """
    renderer = hv.renderer('bokeh').instance(mode='server')

    #Make reference image the base image on which to draw
    img_disp = hv.Image((np.arange(image.shape[1]), np.arange(image.shape[0]), image))
    img_disp.opts(width=int(image.shape[1]),
                  height=int(image.shape[0]),
                  invert_yaxis=True,
                  cmap='gray',
                  colorbar=True,
                  toolbar='above',
                  title="Annotate center points of wells in image")
    # Generate Point Draw Tool
    points = hv.Points([]).opts(active_tools=['point_draw'],
                                color='yellow',
                                size=5,
                                width=int(image.shape[1]),
                                height=int(image.shape[0]))
    pointdraw_stream = streams.PointDraw(source=points, num_objects=10)

    # display with hv bokeh server application
    app = renderer.app((img_disp * points).opts(height=image.shape[0]*3, width=image.shape[1]*3),
                       show=True,
                       new_window=True)

    app.stop()

    return pointdraw_stream

if __name__ == '__main__':

    AP = argparse.ArgumentParser()
    AP.add_argument("-f",
                    "--file_path",
                    required=True,
                    help="fpath of the mean background image of a video")
    ARGS = vars(AP.parse_args())

    IMG = cv2.imread(ARGS['file_path'])
    IMG = cv2.cvtColor(IMG, cv2.COLOR_BGR2GRAY)

    #get point annotations
    ANNOTATED_PTS = annotate_video(IMG)

    print('x coordinates: ', ANNOTATED_PTS.data['x'])
    print('y coordinates: ', ANNOTATED_PTS.data['y'])

我正在使用:

python                    3.6.7             he025d50_1005    conda-forge
bokeh                     1.4.0                    py36_0    conda-forge
holoviews                 1.12.3                     py_2    conda-forge
opencv                    3.4.2            py36h40b0b35_0

我浏览了Holoviews和bokeh服务器文档,但找不到答案。

0 个答案:

没有答案