I wrote Qiita like this (Ubuntu, Python, OpenCV environment created on Docker) before, but cv2.imshow ()
Did not work and I was doing a lot of trial and error.
In this article, I will write how to make cv2.imshow ()
work properly even in Docker environment.
The host OS is MacOS Catalina.
https://www.xquartz.org/
After installation, check Allow connections from network clients on the Security tab in XQuartz Preferences.
xhost +
docker run -it \
-v $(pwd):/code \
-v ~/.Xauthority:/root/.Xauthority \ #Required to interact with Docker and GUI
-e DISPLAY=$(hostname):0 \ #Required to interact with Docker and GUI
-p 8888:8888 \ #For Jupyter
--name opencv \
ubuntu /bin/bash
Do the following in the container
apt-get -y update && apt-get -y upgrade && \
apt-get -y install python3-pip vim libgl1-mesa-dev libgtk2.0-dev && \
pip3 install numpy opencv-python jupyterlab pandas matplotlib scikit-learn seaborn scipy
Launch XQuartz on the host PC
Start Jupyter (in container)
jupyter lab --ip=0.0.0.0 --allow-root --LabApp.token=''
Access http: // localhost: 8888 / with a browser on the host PC
Do the following in Jupyter
import cv2
img = cv2.imread('test.jpeg') #Put some suitable image
img.shape # => (Vertical length of the image,Horizontal length, 3 )Is returned
cv2.imshow('img', img) #If you want to display an image, execute it with this set of 3 rows.
cv2.waitKey(0)
cv2.destroyAllWindows
If the above works without error, it's OK !! Thank you for your hard work !!!
Recommended Posts