- 操作系統: Ubuntu 18.04
- 運行鏡像: continuumio/anaconda3, based on debian
Step 1) 安裝 Docker
# update the apt package index
sudo apt-get update
# install packages to allow apt to use a repository over HTTPS
sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common
# add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# set up the stable repository
sudo add-apt-repository \
"deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
$(lsb_release -cs) \
stable"
# update the apt package index
sudo apt-get update
# install the latest version of Docker Engine and containerd
sudo apt-get install docker-ce docker-ce-cli containerd.io
允許當前非 root 用戶管理 Docker:
sudo groupadd docker
sudo usermod -aG docker $USER
參考:
- Install Docker Engine on Ubuntu:
https://docs.docker.com/engine/install/ubuntu/ - Docker CE 清華源:
https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/ - Post-installation steps for Linux:
https://docs.docker.com/engine/install/linux-postinstall/
Step 2) 准備鏡像
拉取 OpenCV 鏡像,用其顯示 GUI:
docker pull joinaero/anaconda3-opencv3:1.0.0
Step 3) xhost 添加 local
$ xhost +local:docker
non-network local connections being added to access control list
Step 4) OpenCV 預覽圖片
# 運行鏡像,指明 DISPLAY
docker run -it --rm \
--name myenv \
-e DISPLAY \
-e QT_X11_NO_MITSHM=1 \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v $HOME/.Xauthority:/root/.Xauthority \
joinaero/anaconda3-opencv3:1.0.0
# 激活 myenv 環境
conda activate myenv
# 預覽 GoCoding.png
python - <<EOF
import cv2
while True:
im = cv2.imread("/tmp/GoCoding.png")
im = cv2.resize(im, (256, 256))
cv2.imshow("GoCoding", im)
key = cv2.waitKey(10) & 0xFF
if key == 27 or key == ord('q'):
break
EOF
Step 5) OpenCV 預覽相機
# docker --device 指明 video 設備
docker run -it --rm \
--name myenv \
-e DISPLAY \
-e QT_X11_NO_MITSHM=1 \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v $HOME/.Xauthority:/root/.Xauthority \
--device /dev/video0 \
--device /dev/video1 \
joinaero/anaconda3-opencv3:1.0.0
# 激活 myenv 環境
conda activate myenv
# 預覽相機圖像
python - <<EOF
import sys
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
sys.exit("Read the next frame failed")
cv2.imshow("GoCoding", frame)
key = cv2.waitKey(10) & 0xFF
if key == 27 or key == ord('q'):
break
cap.release()
EOF
結語
Go coding!
分享 Coding 中實用的小技巧、小知識!歡迎關注,共同成長!