Issue
On my raspberry pi cv2.imshow('text', frame)
works fine when run from root. However, when run from another user (myname), I get the following error:
Unable to init server: Could not connect: Connection refused Traceback (most recent call last): File "my_file.py", line 7, in cv.imshow('text', frame) cv.error: OpenCV(4.5.1) /tmp/pip-wheel-qd18ncao/opencv-python/opencv/modules/highgui/src/window_gtk.cpp:624: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'
my code:
import cv2
camera = cv2.VideoCapture(0)
success, frame = camera.read()
if not success:
stop('camera not connected')
cv2.imshow('text', frame)
cv2.waitKey(1000)
My group permission for the user (id -a
) in case that helps:
uid=1001(myname) gid=1001(myname) groups=1001(myname),27(sudo),29(audio),44(video)
As everything is working as expected for root (pi) I suspect that I don't have access or the right permissions to specific packages from user 'myname'? However, I don't know how to troubleshoot this.
Solution
As I said in the comments of the question, using this command:
echo $DISPLAY
It prints the current X server configured in the environment variables for the user. If this variable is different for "pi" and "myname" (for any reason), then you should change the value of "myname" to be equal to that of "pi". If they are the same, you can try this command:
xhost +si:localuser:myname
This will change the X server permissions for user "myname".
Answered By - Doch88