Cannot figure out Dockerfile template and start.sh

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

Thanks

Hi,

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.

1 Like

The following is the git repo im trying to run on Xserver. Kindly help!

Hi,

There are a couple of problems with your code.

  • You are missing a COPY operation in your dockerfile to actually move the files into your docker image.
  • You are trying to start the new_video.py script, which in it’s path is missing the leading /
  • If you want to use a start.sh you need to call the start.sh script as CMD in your dockerfile.

Please take a look at the referenced example repository and we are happy to help with specific questions.

1 Like

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.

RUN install_packages
xserver-xorg-core
xserver-xorg-input-all
xserver-xorg-video-fbdev
xorg
libxcb-image0
libxcb-util0
xdg-utils
libdbus-1-3
libnotify4
libgnome-keyring0
libgconf-2-4
libasound2
libcap2
libcups2
libxtst6
libxss1
libnss3
libsmbclient
libssh-4
fbset
libexpat1
python3-tk

ERROR

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

Hey, a quick google search gave me this: https://askubuntu.com/questions/815874/importerror-no-named-tkinter-please-install-the-python3-tk-package. I believe you need to install the specific tkinter version for the python on the system.

1 Like

Hi, thanks for your reply. I tried installing the compatible version with no success.
I tried installing python3.5-tk but the error persists

Hi,
It seems like you didn’t install tcl or tk (https://stackoverflow.com/questions/5459444/tkinter-python-may-not-be-configured-for-tk). Maybe this would help.

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

Dockerfile.template

# Build stage
##
#FROM balenalib/%%BALENA_MACHINE_NAME%%-node:10-build as build
#FROM balenalib/i386-debian-python:latest
FROM balenalib/raspberry-pi-python:3.6-stretch-build as build	


#RUN pip install --upgrade pip

RUN install_packages \ 
  tcl \
  python3-tk

WORKDIR /usr/src/app
	
#RUN apt-get install python3-tk
#RUN apt-get --upgrade pip
#RUN apt-get update \
# PIP

#RUN pip install Pillow

#RUN install_packages blt-demo tk8.6 tix python3-tk-dbg tcl-tclreadline

RUN pip install Pillow==2.2.1

COPY . ./

#RUN --update add python py-pip openssl ca-certificates py-openssl wget
#RUN --update add --virtual build-dependencies libffi-dev openssl-dev python-dev py-pip build-base \
#  && pip install --upgrade pip \
#  && pip install -r requirements.txt 

# Install runtime dependencies
#RUN install_packages \ tcl \ tk \ apt-utils
	
#RUN pip install -r requirements.txt

# Start app
# CMD ["./start.sh"]
CMD ["python","-u","new_video.py"]

new_video.py

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'

@lakshaygroverr have you searched for these error messages online? I’m having a quick look and there are quite a few suggestions for things to try.

https://askubuntu.com/questions/815874/importerror-no-named-tkinter-please-install-the-python3-tk-package (Stevche suggested this one above but it doesn’t look like you’ve tried the suggestion there in the current version of your Dockerfile)

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.