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


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

  • 相关阅读:
    php极光网络一键登录(yii框架)
    Sublime Text3将多行转为为一行 | Sublime Text 快速分别独立选中多行
    mysql 将时间戳转换成日期格式
    Vant主题定制修改颜色样式
    TypeError: this.getOptions is not a function 引入less一直报错
    export defaul 和 export定义和区别
    Vue vant引入,tabbar封装使用示例
    php去除富文本编辑器中的内容格式
    ES6:高级数组函数,filter/map/reduce
    [BZOJ2793][Poi2012]Vouchers
  • 原文地址:https://www.cnblogs.com/gocodinginmyway/p/12985302.html
Copyright © 2020-2023  润新知