How to record the host's desktop from the program in docker ( c++ opencv in ubuntu)?

Viewed 157

I have copy the opencv code in c++ from the internet to record the desktop"

#include <iostream>
#include <opencv2/opencv.hpp>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <cstdint>
#include <cstring>
#include <vector>

using namespace std;
using namespace cv;
void ImageFromDisplay(std::vector<uint8_t>& Pixels, int& Width, int& Height, int& BitsPerPixel)
{
    Display* display = XOpenDisplay(nullptr);
    Window root = DefaultRootWindow(display);
    
    XWindowAttributes attributes = {0};
    XGetWindowAttributes(display, root, &attributes);
    
    Width = attributes.width;
    Height = attributes.height;
    
    XImage* img = XGetImage(display, root, 0, 0 , Width, Height, AllPlanes, ZPixmap);
    BitsPerPixel = img->bits_per_pixel;
    Pixels.resize(Width * Height * 4);
    
    memcpy(&Pixels[0], img->data, Pixels.size());
    
    XDestroyImage(img);
    XCloseDisplay(display);
}
int main()
{
    int Width = 0;
    int Height = 0;
    int Bpp = 0;
    std::vector<std::uint8_t> Pixels;
    while(true)
    {
        namedWindow("Display window",WINDOW_NORMAL);
        ImageFromDisplay(Pixels, Width, Height, Bpp);
        
        if (Width && Height)
        {
            Mat img = Mat(Height, Width, Bpp > 24 ? CV_8UC4 : CV_8UC3, &Pixels[0]); //Mat(Size(Height, Width), Bpp > 24 ? CV_8UC4 : CV_8UC3, &Pixels[0]);
            
            namedWindow("WindowTitle", WINDOW_AUTOSIZE);
            imshow("Display window", img);
            
            waitKey(1);
        }
    }
    return 0;
}

Anything works fine. enter image description here The imshow window show the exactly my desktop.

However, my boss said that every programs should be in docker. I wrote a dockerfile and put the program in it

FROM x11docker/xfce
ENV TZ=Asia/Taibei
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update && apt-get install -y \ 
    build-essential \
    cmake \
    git \
    libatlas-base-dev \
    libgtk2.0-dev \
    libjpeg-dev \
    libpng-dev \
    libtiff-dev \
    libvtk6-dev \
    pkg-config \
    qt5-default \
    wget \
    zlib1g-dev 

RUN apt-get install cmake-qt-gui
   
RUN git clone https://github.com/opencv/opencv.git && \
    cd ./opencv && \
    mkdir build && \
    cd build && \
    cmake  \
        -D BUILD_SHARED_LIBS=ON \ 
        -D WITH_QT=ON \
        -D WITH_OPENGL=ON \ 
        -D FORCE_VTK=ON \
        -D WITH_TBB=ON \ 
        -D WITH_GDAL=ON \ 
        -D WITH_V4L=ON \
        -D WITH_XINE=ON \ 
        -D BUILD_EXAMPLES=OFF \
        -D ENABLE_PRECOMPILED_HEADERS=OFF \
        -D BUILD_DOCS=OFF \
        -D BUILD_PERF_TESTS=OFF \
        -D BUILD_TESTS=OFF \
        -D BUILD_opencv_apps=OFF \
        -D CMAKE_INSTALL_PREFIX=/home/gino/opencv_install \
        .. && \
    make -j8 && \
    make install && \
    ldconfig

ADD ./ /home/gino/cvtest/
WORKDIR /home/gino/cvtest/
RUN make

ENV HOME /home/developer 

In order to show the GUI I use x11docker to run the image. But the program in docker can only get the image's desktop. enter image description here

How do I record the host's desktop by the program which is in the docker?

1 Answers

Find the answer on another guy's blog

first, use the command to make the x11 can be accessed by other users:

xhost +

the second, run the image ( the image's name is "catchdesktop" )

sudo docker run --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" catchdesktop

now I can see my host desktop appear in the opencv program in docker.

enter image description here

Related