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!