9

I have created a docker image for opencv and facial reckognition to simplify the setup process.

But the recognize.py script needs X Server to show the image result. Here is what I have done so far:

sudo docker run -t -d --name opencv opencv:latest
sudo docker exec -it opencv bash /extract-embeddings.sh
sudo docker exec -it opencv bash /train-model.sh

All is fine so far. The last step is the actual comparison that displays the result in an image.

sudo docker exec -it opencv bash /face-recognition.sh

It gives the output:

[INFO] loading face detector...
[INFO] loading face recognizer...
No protocol specified
: cannot connect to X server :0

I have tried running the container with the following command:

sudo docker run -t -d --name opencv -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix opencv:latest

But it doesn't help.

Kasper Hansen
  • 6,307
  • 21
  • 70
  • 106

3 Answers3

12

Try running this,

xhost +

sudo docker run --rm -ti --net=host --ipc=host -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --env="QT_X11_NO_MITSHM=1" <image_name> <arguments>

Other might face issue regarding the image not getting rendered on screen or getting a blank screen with no image, for them add --env="_X11_NO_MITSHM=1" to the above script while running the docker image. It will solve the problem.

For further information, I would recommend you guys check out the below references.

Reference 1
Reference 2

Afroz Chakure
  • 174
  • 1
  • 6
  • 1
    Thanks. I'm getting `: cannot connect to X server /private/tmp/com.apple.launchd.zRvN9T3LeV/org.macosforge.xquartz:0` – loretoparisi May 26 '20 at 19:35
4

It looks like the xauth is the issue for viewing of the image. The details are at Can you run GUI applications in a Docker container?

lokrao
  • 66
  • 3
1

It may happen that also the XAuthority is needed.

First, make sure that the host's $XAUTHORITY is defined. And second, add the following parameters to the docker run command:

-v $XAUTHORITY:/tmp/.XAuthority -e XAUTHORITY=/tmp/.XAuthority

An example of a complete command:

sudo docker run --rm -ti --net=host --ipc=host -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v $XAUTHORITY:/tmp/.XAuthority -e XAUTHORITY=/tmp/.XAuthority --env="QT_X11_NO_MITSHM=1" <image_name> <arguments>
Andrej Debenjak
  • 1,774
  • 1
  • 15
  • 10