1

I am using docker for the first time. I am using this image opencvcourses/opencv-docker. I try to run a scrip with openCV, but first i am going to explain how i run a simple code with print and numpy

I have my script (ej.py) on my ubuntu local directory: /home/noemi/Escritorio/contenedores/cont1/ej.py

import numpy as np
print('Esto es una prueba ok')
print(np.pi)

I run docker with the next command

docker run -it -v /home/noemi/Escritorio/contenedores/cont1:/home opencvcourses/opencv-docker

And, my script works! I obtain this

Esto es una prueba ok
3.141592653589793

But when I add to my script (ej.py) openCV code

import cv2
import numpy as np

img1 = np.zeros((6,8,1),np.uint8)

cv2.imshow('imagen-zeros',img1)

cv2.waitKey(0)
cv2.destroyAllWindows()

and try with the same command

docker run -it -v /home/noemi/Escritorio/contenedores/cont1:/home opencvcourses/opencv-docker

appear this error

Traceback (most recent call last):
  File "ej.py", line 6, in <module>
    cv2.imshow('imagen-zeros',img1)
cv2.error: OpenCV(4.5.1) /home/opencv/modules/highgui/src/window_gtk.cpp:624: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'

Please, I don't know what happened

2 Answers 2

3

I found the solution reading this post https://www.programmersought.com/article/15656181875/

I have a script named ej.py on my ubuntu location /home/noemi/Escritorio/contenedores/cont1

import cv2
import numpy as np

print('Esto es una prueba ok')
print(np.pi)
img = cv2.imread('gato.jpg')

cv2.imshow('imagen-zeros',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

To run my code using docker image opencvcourses/opencv-docker follow the next steps, its works for me:

$ xhost +local:docker
$ docker run -it --device /dev/video0:/dev/video0 -v $(pwd):/home -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -p 5000:5000 -p 8888:8888 opencvcourses/opencv-docker

the previous command create a container and open the image bash. Here write python3 ej.py like this:

root@ee4316d99815:/home# python3 ej.py

and it works! My code show the image

If you want execute the container again try with this:

  1. Identify the ID of container with: $ docker ps -a
  2. Start the container with: $ docker start (ID container like this ee4316d99815)
  3. Execute the container with: $ docker exec -it ee4316d99815 /bin/bash
  4. And try again with python3 ej.py

Maybe if you want close xhost, you should use $ xhost -local:docker

Sign up to request clarification or add additional context in comments.

1 Comment

The "xhost +local:docker" command was what I was missing! Thanks
0

docker run command

docker run -it -v /home/noemi/Escritorio/contenedores/cont1:/home opencvcourses/opencv-docker

Explanation

  1. Sets DISPLAY environment variable value to be the same as HOST's.

--env DISPLAY=$DISPLAY

  1. Helps in X11 forwarding so that we can use functions like cv::imshow

-v /tmp/.X11-unix:/tmp/.X11-unix

And let me know if this works to update this answer accordingly.

Sources

7 Comments

Hi om-ha, I prove the three commands and not work for me, I obtain the same error... Maybe you know other solution that I can use... thanks for your answer :)
Updated my answer, try it out and let me know.
Waiting for your input @Noemí
Sorry, no work for me :( I try this: $ docker run -it --device /dev/video0:/dev/video0 -v $(pwd):/home -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -p 5000:5000 -p 8888:8888 opencvcourses/opencv-docker ...my code prints (np.pi), but not cv2.imshow. I obtain the same error: No protocol specified Traceback (most recent call last): File "ej.py", line 8, in <module> cv2.imshow('imagen-zeros',img1) cv2.error: OpenCV(4.5.1) /home/opencv/modules/highgui/src/window_gtk.cpp:624: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'
Dear om-ha! I read this post programmersought.com/article/15656181875 and I try with: $ xhost +local:docker and later with: docker run -it --device /dev/video0:/dev/video0 -v $(pwd):/home -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -p 5000:5000 -p 8888:8888 opencvcourses/opencv-docker ...and my code WORKS! thanks for your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.