码迷,mamicode.com
首页 > 系统相关 > 详细

Docker: GUI 应用,Ubuntu 上如何运行呢?

时间:2020-05-29 09:41:46      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:show   local   准备   ide   图像   操作   --   cat   技巧   

  • 操作系统: 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

参考:

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 中实用的小技巧、小知识!欢迎关注,共同成长!

技术图片

Docker: GUI 应用,Ubuntu 上如何运行呢?

标签:show   local   准备   ide   图像   操作   --   cat   技巧   

原文地址:https://www.cnblogs.com/gocodinginmyway/p/12985302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!