cv2.imshow() crashes Kernel

16,244

Solution 1

I can't explain the behavior of your code right now but you can use the below code to achieve the above behavior.

%matplotlib inline
from matplotlib import pyplot as plt
import cv2
image = cv2.imread('images/input.jpg')
plt.imshow(image)
plt.show()

Solution 2

To view images with cv2.imshow on AWS, you need to enable X11 forwarding so the graphics can be run on the server and displayed locally. This can be done by ssh-ing with the -Y option:

ssh -Y username@hostname

If the images are larger, you will also need to compress the data using -C:

ssh -Y -C username@hostname

The terminal used to ssh into AWS will need to be left open as long as you're displaying images from the notebook.

Share:
16,244
Mike de H
Author by

Mike de H

Updated on June 06, 2022

Comments

  • Mike de H
    Mike de H almost 2 years

    I am running OpenCV through Jupyter Notebooks and whenever I try to run cv2.imshow() the kernel crashes, no error message or helpful hint - just a plain

    The Kernel appears to have died. It will restart automatically.

    Here is the code I am running...

    import cv2 
    input = cv2.imread('images/input.jpg')
    cv2.imshow('Hello World', input)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    The code works (albeit differently) when I run the below...

    %matplotlib inline
    from matplotlib import pyplot as plt
    import cv2
    image = cv2.imread('images/input.jpg')
    plt.imshow(image)
    plt.show()
    

    FYI I am using a completely unaltered copy of BitFusion on AWS.

    https://aws.amazon.com/marketplace/pp/B01EYKBEQ0?ref=cns_srchrow

    Any idea what could be going wrong?