はじめに
実験で機器の数値を目視で読み取る作業が面倒!
特に5秒おきで読み取るなんて作業は苦痛でしかない
というか数値をデジタル出力できない計測機器ってどうなんだ?
まぁお金の話もあるし機器の話は置いといて、Pythonで画像解析したらいいと思うので、まずは画像解析の環境を整えよう。
カメラ入力、画面出力などは別途設定が必要なので、その設定も行う。
概要
- Pythonの画像解析ライブラリ
OpenCV
を動かすdocker環境の構築 - デフォルトではdocker紐付くディスプレイがないので、ホストマシンのディスプレイと繋げる。
実行環境
ホストPC: Mac
docker-composeでjupyter(Pythonが動くところ)を構築し、そこで画像解析する
ホストマシン(Mac)の設定
-
Mac用X11インストール
$ brew cask install xquartz
できたら再起動しよう。
しないと次でxhost: command not found
になる。 -
ディスプレイを外からアクセス可能にする
$ xhost + ... access control disabled, clients can connect from any host
※作業が終わったら無効にするのが良い
$ xhost -
-
コンテナからのアクセスを待ち受ける
$ brew install socat $ socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"
実行後は待ち受け状態になるので、別ターミナルに切り替える
実行環境(Docker)の設定
dockerfile
- opencv-pythonの依存関係にある外部ライブラリをインストール
RUN apt-get update && \ apt-get install -y libopencv-dev
- opencv-pythonのインストール
RUN set -x && \ pip install -U pip && \ pip install opencv-python
docker-compose
-
ホストPCのX11の認証情報をコンテナのボリュームにマウントする
volumes: - $HOME/.Xauthority/:/root/.Xauthority - /tmp/.X11-unix/:/tmp/.X11-unix
-
ホストPCのIPを環境変数で渡す
※localhost・127.0.0.1ではダメです。
Macのネットワーク環境設定からIPを読み取ってください
最後の:0
を忘れずに!environment: - DISPLAY=192.168.0.18:0
全体のdocker関連のコード
※jupyter_notebookの設定も入ってます
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.4" | |
services: | |
python3: | |
restart: always # OS起動時にコンテナを自動起動 | |
build: . | |
tty: true | |
volumes: | |
- ./volume/opt/jupyter:/opt/python/jupyter | |
- ./volume/opt/jupyterlab:/opt/python/jupyterlab | |
- ./volume/opt/library:/opt/python/library | |
- $HOME/.Xauthority/:/root/.Xauthority | |
- /tmp/.X11-unix/:/tmp/.X11-unix | |
environment: | |
- DISPLAY=192.168.0.18:0 | |
ports: | |
- 8888:8888 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM python:3.7.3-slim-stretch | |
ENV PYTHONPATH "/opt/python/library" | |
COPY ./requirements.txt /tmp/requirements.txt | |
RUN apt-get update && \ | |
apt-get install -y libopencv-dev | |
RUN set -x && \ | |
pip install -U pip && \ | |
pip install -r /tmp/requirements.txt && \ | |
mkdir -p /opt/python/library && \ | |
mkdir -p /opt/python/jupyter && \ | |
mkdir -p /opt/python/jupyterlab && \ | |
mkdir ~/.jupyter && \ | |
cp /tmp/requirements.txt /opt/python/jupyterrequirements.txt && \ | |
rm /tmp/requirements.txt | |
COPY ./jupyter_notebook_config.py /root/.jupyter/jupyter_notebook_config.py | |
EXPOSE 8888 | |
CMD ["jupyter", "lab", "--allow-root"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requests | |
jupyterlab==0.35.4 | |
matplotlib==3.0.3 | |
numpy==1.16.2 | |
pandas==0.24.2 | |
plotly==3.7.1 | |
seaborn==0.9.0 | |
statsmodels | |
scikit-learn | |
opencv-python |
コメント