[Python] OpenCV environment construction with Docker (cv2.imshow () also works)

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.

1. Install XQuartz

https://www.xquartz.org/

After installation, check Allow connections from network clients on the Security tab in XQuartz Preferences.

2. Open xhost

xhost +
  1. Docker run
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

4. Various updates and installations

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

5. Operation check

  1. Launch XQuartz on the host PC

  2. Start Jupyter (in container)

jupyter lab --ip=0.0.0.0 --allow-root --LabApp.token=''
  1. Access http: // localhost: 8888 / with a browser on the host PC

  2. 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

[Python] OpenCV environment construction with Docker (cv2.imshow () also works)
Prepare python3 environment with Docker
Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
Get started with Python! ~ ① Environment construction ~
Build Mysql + Python environment with docker
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
pytorch @ python3.8 environment construction with pipenv
Data science environment construction with Docker
[docker] python3.5 + numpy + matplotlib environment construction
OpenCV3 & Python3 environment construction on Ubuntu
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Cv2.imshow now works properly with Homebrew's OpenCV3
Build Jupyter Lab (Python) environment with Docker
Python environment construction
Environment construction (python)
Create Python + uWSGI + Nginx environment with Docker
Created Ubuntu, Python, OpenCV environment on Docker
python environment construction
Python --Environment construction
Python environment construction
Launch environment with LineBot + Heroku + Docker + Python
python environment construction
Let's get along with Python # 0 (Environment construction)
Collecting information from Twitter with Python (Environment construction)
Prepare the execution environment of Python3 with Docker
Analytical environment construction with Docker (jupyter notebook + PostgreSQL)
[Python] Build a Django development environment with Docker
Create Nginx + uWSGI + Python (Django) environment with docker
Poetry-virtualenv environment construction with python of centos-sclo-rh ~ Notes
Pillow environment construction --For Docker + iPython (and OpenCV)
First python ① Environment construction with pythonbrew & Hello World !!
From Python environment construction to virtual environment construction with anaconda
Build PyPy and Python execution environment with Docker
Environment construction: GCP + Docker
python windows environment construction
Binarization with OpenCV / Python
homebrew python environment construction
Python development environment construction
Use python with docker
Python environment with docker-compose
python2.7 development environment construction
Mac environment construction Python
Virtual environment with Python 3.6
Python environment construction @ Win7
Python local development environment construction template [Flask / Django / Jupyter with Docker + VS Code]
Easy Python data analysis environment construction with Windows10 Pro x VS Code x Docker
Japanese can be used with Python in Docker environment
From Kafka to KSQL --Easy environment construction with docker
Data analysis environment construction with Python (IPython notebook + Pandas)
Web application made with Python3.4 + Django (Part.1 Environment construction)
WSL2 environment construction example Python, Node.js, Java, git-secrets, Docker
Pepper-kun remote control environment construction with Docker + IPython Notebook
QGIS3 Python plugin development environment construction with VSCode (macOS)
Python + Anaconda + Pycharm environment construction
"Apple processing" with OpenCV3 + Python3
Install Python environment with Anaconda
Manage python environment with virtualenv
Image editing with python OpenCV
Camera capture with Python + OpenCV
Python environment construction (Windows10 + Emacs)
[Python] Using OpenCV with Python (Basic)