opencv.imshow will cause jupyter notebook crash

后端 未结 8 1450
借酒劲吻你
借酒劲吻你 2021-01-31 07:46

I check other question on google or stackoverflow, they are talking about run cv2.imshow in script, but my code run in jupyter notebook.

Here is my configuration:

<
8条回答
  •  清酒与你
    2021-01-31 08:44

    I am not sure if you can open a window from Jupyter Notebook. cv2.imshow expects a waitKey which doesn't work in Jupyter.

    Here is what I have done (using OpenCV 3.3):

    from IPython.display import display, HTML
    import cv2
    import base64
    
    def imshow(name, imageArray):
         _, png = cv2.imencode('.png', imageArray)
         encoded = base64.b64encode(png)
         return HTML(data='''{0}'''.format(name, encoded.decode('ascii')))
    
    img = cv2.imread('./media/baboon.jpg',cv2.IMREAD_COLOR)
    imshow('baboon', img)
    

    If you don't need to use cv2, just:

    from IPython.display import Image
    Image('./media/baboon.jpg')
    

提交回复
热议问题