Dokcer use opencv on windows11

My system is windows11 and install the docker.

How to setting the docker parameter to use the opencv (imshow)?

PS. export DISPLAY=:0.0 or export DISPLAY=:0 are not work for me.

Error log

Unable to init server: Could not connect: Connection refused
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.7.0) /tmp/opencv/modules/highgui/src/window_gtk.cpp:635: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'

Aborted

main.cpp

// g++ opencv-camera.cpp -o a.out `pkg-config --cflags --libs opencv`
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main() {
    //VideoCapture cap(0);
    VideoCapture cap = cv2.VideoCapture("chaplin.mp4")

    if (!cap.isOpened()) {
        cout << "Cannot open camera\n";
        return 1;
    }

    Mat frame;
    Mat gray;
    //namedWindow("live", WINDOW_AUTOSIZE); 
    while (true) {
        bool ret = cap.read(frame); // or cap >> frame;
        if (!ret) {
            cout << "Can't receive frame (stream end?). Exiting ...\n";
            break;
        }
        cvtColor(frame, gray, COLOR_BGR2GRAY);
        imshow("live", frame);
        if (waitKey(1) == 'q') {
            break;
        }
    }
    return 0;
}

This is a Docker forum. It might help if you share your Dockerfile or docker command or docker-compose.yml.

Yes, it is the docker issue for windows system. There is a $DISPLAY parameter on linux, but I don’t know for windows.
Installing the opencv cost a long time, so I provide the python version to speed up the debug time.

using this command run the docker

docker run -it onnxruntime-test bash

Then run

python3 video.py

The below are the files

dockerfile (or any dockerfile image with opencv )

ARG OPENVINO_VERSION=2023.0.0

# Build stage
FROM openvino/ubuntu20_runtime:${OPENVINO_VERSION} AS base-build
ENV WORKDIR_PATH=/home/openvino
WORKDIR $WORKDIR_PATH
ENV DEBIAN_FRONTEND noninteractive
ENV CPU_CORE_NUM 4
USER root

Run the docker and pip install -r requirements.txt
requirements.txt

matplotlib>=3.3
numpy>=1.22.2
opencv-python>=4.1.1

video.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2

#cap = cv2.VideoCapture(0)
cap = cv2.VideoCapture("chaplin.mp4")

if not cap.isOpened():
    print("Cannot open camera")
    exit()

#cv2.namedWindow("live", cv2.WINDOW_AUTOSIZE); 
while(True):

    ret, frame = cap.read()
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break


    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)


    cv2.imshow('live', frame)
    #cv2.imshow('live', gray)


    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

error log

qt.qpa.xcb: could not connect to display :0
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/usr/local/lib/python3.8/dist-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.

Aborted

Did you check the OpenVino docs? To speed processing up, you would need to use -device.

If you are running Windows, then Docker Desktop is normally running Docker images inside a Linux VM. I don’t think any graphical output is possible that way, no GUI, no OpenCV imshow(), only command line.

Follow this Developing on Remote Machines using SSH and Visual Studio Code,
I use vscode to run the docker.
Then using opencv to read the video and imshow it.
It can show windwos with opencv successfully.

But using windows 11 command prompt to run the docker. It can’t work.

I just want to know what can I add parameter for docker?
Let opencv can show windows on docker via windows 11.

By the way, I just want to show windows with opencv, so the openvino is not important.

Are you running Windows Desktop or did you install docker-ce inside a WSL2 distribution?

What’s the output of wsl --version (execute on Windows)

WSLg has been around in WSL2 for a while now. It starts a Wayland X-Server in the WSL2 utility vm, the x-server is available in every distribution via unix domain socket /tmp/.X11-unix/X0. The Wayland server will render the output on the Windows Host using rdp. The rdp client will be started with the distribution, but will be hidden until a GUI application is actually started.

You might want to try if adding these arguments helps: -v /tmp/.X11-unix/X0:/tmp/.X11-unix/X0 -e DISPLAY=:0

I am running Windows Desktop. (docker destop)
These arguments doesn’t helps: -v /tmp/.X11-unix/X0:/tmp/.X11-unix/X0 -e DISPLAY=:0

It show the same error.
There are not the /tmp/.X11-unix/X0 and DISPLAY=:0 on windows 11

qt.qpa.xcb: could not connect to display :0

wsl --version

WSL version: 1.2.5.0
core version: 5.15.90.1
WSLg version: 1.0.51
MSRDC version: 1.2.3770
Direct3D version: 1.608.2-61064218
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22621.2134

docker version

Client:
 Cloud integration: v1.0.35-desktop+001
 Version:           24.0.5
 API version:       1.43
 Go version:        go1.20.6
 Git commit:        ced0996
 Built:             Fri Jul 21 20:36:24 2023
 OS/Arch:           windows/amd64
 Context:           default

Server: Docker Desktop 4.22.1 (118664)
 Engine:
  Version:          24.0.5
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.6
  Git commit:       a61e2b4
  Built:            Fri Jul 21 20:35:45 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.21
  GitCommit:        3dce8eb055cbb6872793272b4f20ed16117344f8
 runc:
  Version:          1.1.7
  GitCommit:        v1.1.7-0-g860f061
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

I just checked it on Docker Desktop v4.24.2 using this example (I used first image I found that runs a x-application directly):

docker run -it --rm -e DISPLAY=:0 -v '/tmp/.X11-unix:/tmp/.X11-unix' mtabishk/firefox:v1.0.0

Result:

  • If executed in Windows: throws an error
  • if executed in a WSl2 distribution with enabled Docker integration: works like a charm, application window gets rendered on the host

The last result would be identical, if docker-ce was installed in a WSL2 distribution.

If you want to be able to execute the command in Windows, you apparently need the old-school approach that requires a xserver in Windows or need to include vnc in your image.

2 Likes