Hello, I have been trying to run a python script that uses openCV to show videos and images.
The main directory contains 1 folder that is cache ( used to store media) , the python script, Dockerfile.template and start.sh. After several tried I am unable to display the output of Python script using XServer. Since I am very new to this, could anyone please help me write the template and start.sh
To give you a starting point, we have an example project here that uses xserver https://github.com/balena-io-playground/x11-window-manager maybe you can find some more hints there. If you can share your project with us, weâll probably be able to help you get started.
Hi, the above solution worked well and managed to run the python script well. I have installed the below mentioned packages and still I am getting error of import.
Traceback (most recent call last):
26.02.20 13:11:20 (+0530) main File ânew_video.pyâ, line 6, in
26.02.20 13:11:20 (+0530) main import tkinter
26.02.20 13:11:20 (+0530) main File â/usr/local/lib/python3.5/tkinter/init.pyâ, line 35, in
26.02.20 13:11:20 (+0530) main import _tkinter # If this fails your Python may not be configured for Tk
26.02.20 13:11:20 (+0530) main ImportError: No module named â_tkinterâ
26.02.20 13:12:16 (+0530) main Traceback (most recent call last):
26.02.20 13:12:16 (+0530) main File ânew_video.pyâ, line 6, in
26.02.20 13:12:16 (+0530) main import tkinter
26.02.20 13:12:16 (+0530) main File â/usr/local/lib/python3.5/tkinter/init.pyâ, line 35, in
26.02.20 13:12:16 (+0530) main import _tkinter # If this fails your Python may not be configured for Tk
26.02.20 13:12:16 (+0530) main ImportError: No module named â_tkinterâ
Also I am using PIL,openCV and tkinter to build the application. What should be the imports in requirements.txt or dockerfile.template since when I am trying, I am getting the above errors. Thanks in advance
Okay, so after every possible solution I could try, I did. No success, but things got better when I used build version instead of run FROM balenalib/raspberry-pi-python:3.6-stretch-build as build
But still a long way to go. I am posting my Dockerfile.template and the python code. Please see what can be done since there are many errors and ultimately display screen using Xserver has to be. Thanks
if sys.version_info[0] == 2: # the tkinter library changed it's name from Python 2 to 3.
import Tkinter
print("Version 2 it is")
tkinter = Tkinter #I decided to use a library reference to avoid potential naming conflicts with people's programs.
else:
import tkinter
from PIL import Image, ImageTk
import time
import glob
import cv2
root = tkinter.Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set()
canvas = tkinter.Canvas(root,width=w,height=h)
canvas.pack()
canvas.configure(background='black')
def showPIL(pilImage):
#imgWidth, imgHeight = pilImage.size
# resize photo to full screen
#ratio = min(w/imgWidth, h/imgHeight)
#imgWidth = int(imgWidth*ratio)
#imgHeight = int(imgHeight*ratio)
#pilImage = pilImage.resize((imgWidth,imgHeight), Image.NEAREST)
image = ImageTk.PhotoImage(pilImage)
imagesprite = canvas.create_image(w/2,h/2,image=image)
#root.update_idletasks()
root.update()
# root.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.quit()))
names = glob.glob('cache/*')
print(names)
while True:
for file in names:
if file.split('.')[-1] == 'mp4':
print('video')
start = time.time()
video = cv2.VideoCapture(file)
while True:
ret,frame = video.read()
if ret:
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
frame = Image.fromarray(frame)
showPIL(frame)
else:
print('time taken:',(time.time()-start))
break
elif file.split('.')[-1] == 'jpg' or file.split('.')[-1] == 'png':
print(file)
media_file=Image.open(file)
showPIL(media_file)
sleep_duration = file.split('.')[0].split('_')[-1]
print(sleep_duration)
time.sleep(int(sleep_duration))
else:
print('format not supported')
ERROR
27.02.20 14:25:42 (+0530) main Traceback (most recent call last):
27.02.20 14:25:42 (+0530) main File "new_video.py", line 7, in <module>
27.02.20 14:25:42 (+0530) main import tkinter
27.02.20 14:25:42 (+0530) main File "/usr/local/lib/python3.6/tkinter/__init__.py", line 36, in <module>
27.02.20 14:25:42 (+0530) main import _tkinter # If this fails your Python may not be configured for Tk
27.02.20 14:25:42 (+0530) main ModuleNotFoundError: No module named '_tkinter'
https://stackoverflow.com/questions/5459444/tkinter-python-may-not-be-configured-for-tk - Again Andy posted this above, but if youâre not able to get it working you can install your own version of Python as it suggests here, you donât necessarily have to use the base image. Iâm also interested to know why youâre using Stretch? Iâd suggest balenalib/raspberry-pi-debian-python:3.7-buster-build or balenalib/raspberry-pi-debian:-buster-build if you are going to install your own version of Python.