0

So I'm working on a Jetson Nano running Ubuntu 18.04. I'd like to create a script that at startup automatically runs, taking a video feed from the connected webcam, and opening up a window to view the feed.

Before fully testing out my main script, I wanted to do a proof of concept that just opens up an image to display upon startup. To do so, I followed this tutorial to get a script to run on startup, and added the part to read & open the image: https://www.linuxshelltips.com/run-python-script-ubuntu-startup/

The modified script is this:

rom os.path import expanduser
import datetime
import cv2
import matplotlib
import matplotlib.pyplot as plt
from PIL import Image

img = cv2.imread('/home/sam/Documents/2022-08-31-220946_4.jpg')
window_name = 'test_img'

cv2.imshow(window_name,img)

while True:
    k = cv2.waitKey(0) & 0xFF
    if k==27:
        break
cv2.destroyAllWindows()

file = open(expanduser("~") + '/Desktop/i_was_created.txt', 'w')
file.write("This LinuxShellTips Tutorial Actually worked!\n" + str(datetime.datetime.now()))
file.write('OpenCV Version: {}\n'.format(str(cv2.__version__)))
file.write('Matplotlib Version: {}\n'.format(str(matplotlib.__version__)))
file.write('img size: {}\n'.format(str(img.shape)))
file.write('img size: {}'.format(str(image.format)))
file.close()

Now, when I run this script on startup, it doesn't work. The imshow() window doesn't appear anywhere, and the rest of the script doesn't execute after hitting the escape button. But, if I comment out the following lines, the script runs from start to finish without issue from startup:

cv2.imshow(window_name,img)

while True:
    k = cv2.waitKey(0) & 0xFF
    if k==27:
        break
cv2.destroyAllWindows()

So it seems the issue really does lie with the imshow() function. Testing with a simple

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

instead also prevents the lines after from running.

However, when I run the script from the terminal manually, it works completely fine without issue, showing the image until the escape key is hit, then creating the txt file.

Also to note, using other image display libraries such as PIL & Matplotlib leads to the same issue, so don't think it's necessarily specific to OpenCV.

So, I'm wondering what's causing this issue, and how it might be able to be resolved. Any help anyone can offer is much appreciated!

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Sam Skinner
  • 376
  • 3
  • 12
  • at the time the script runs, is there a graphical environment already? or does your script run too early? does anything prevent the script from creating a GUI, permission-wise? -- please debug this by trying to create a tkinter gui. if that also fails, it's **not an OpenCV problem** – Christoph Rackwitz Oct 22 '22 at 11:21
  • In line 11 should be cv2.namedWindow('test_img') I didn't test it. – toyota Supra Oct 22 '22 at 11:47
  • @ChristophRackwitz I tried with a tkinter gui, and again, it didn't work. So yes, seems it's not an OpenCV problem. Any idea what might be preventing windows from opening at startup/login then? – Sam Skinner Oct 27 '22 at 05:51
  • yes, the graphical stuff (X server or something) hasn't been started yet. best ask that on https://superuser.com/ they know how to wrangle linux. you will likely have to model this as a "service" and make it depend on certain things. – Christoph Rackwitz Oct 27 '22 at 10:05

0 Answers0