Ask Your Question

Revision history [back]

Tkinter dont allow me to use memory for refreshing GUI

There is a my sample kod. I want to do video rendering. For this propose, I wrote two thread one capture the screen from the camera consistently and save to disk and other one take this image from disk and update the my GUI. I use Tkinter for UI rendering.My problem is starting here. Speed of the refreshing is too low. The image comes to GUI intermittently.I want to use memory for transfer the images to GUI but Tkinter only allow me using disk, not memory. Do anyone give me any suggestions please? How I solve this problem on memory? :)

        # import the necessary packages
    import io
    from picamera.array import PiRGBArray
    from picamera import PiCamera
    import time
    import cv2
    import sys
    import traceback
    import threading
    from PIL import Image, ImageTk
    import numpy as np
    # GUI side imports
    from Tkinter import *
    import os
    from shutil import copyfile

    class ReaderThread(threading.Thread):

            def __init__(self):
                    super(ReaderThread, self).__init__()

            def run(self):
                    readMe()

    class UpdaterThread(threading.Thread):

            def __init__(self):
                    super(UpdaterThread, self).__init__()

            def run(self):
                    renderMe()

    def readMe():
        for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
            # grab the raw NumPy array representing the image, then initialize the timestamp
            # and occupied/unoccupied text
            try:
                    image = frame.array
                    cv2.imwrite("photo.jpg",image)
                    # clear the stream in preparation for the next frame
                    rawCapture.truncate(0)
            except:
                    exc_type, exc_value, exc_traceback = sys.exc_info()
                    traceback.print_exception(exc_type, exc_value, exc_traceback)

    # initialize the camera and grab a reference to the raw camera capture
    sWidth = 800
    sHeight = 600
    camera = PiCamera()
    camera.resolution = (sWidth, sHeight)
    camera.framerate = 15
    rawCapture = PiRGBArray(camera, size=(sWidth, sHeight))

    # allow the camera to warmup
    time.sleep(0.1)

    mainWindow = Tk()
    # Render area definition
    renderFrame = Frame(mainWindow, width = 800, height = 600)
    renderFrame.grid(row = 0, column = 1, padx = 10, pady =2)
    renderLabel = Label(renderFrame)
    renderLabel.grid(row = 0, column = 0, padx = 0, pady = 0)
    renderLabel.pack()

    # update frames on screen
    def renderMe():
            while 1:
                    if os.path.isfile("photo.jpg"):
                       copyfile("photo.jpg", "photo2.jpg")     
                       renderLabel["image"] = ImageTk.PhotoImage(file="photo2.jpg")
                    key = cv2.waitKey(1) & 0xFF
                    # if the `q` key was pressed, break from the loop
                    if key == ord("q"):
                       camera.close()
                       break

                    os.system('exit')

    # GUI interaction functions area
    def freeMouseChoice():
            print "You will choose the mouse my buddy"

    def main():
            try:
               rThread = ReaderThread()
               rThread.start()
               print "Reader thread started..."
               uThread = UpdaterThread()
               uThread.start()
               print "Updater thread started..."
            except:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                traceback.print_exception(exc_type, exc_value, exc_traceback)
                camera.close()

            mainWindow.wm_title("Con sinov - Bustard the BEKCI")
            mainWindow.config(background = "#000000")

            #Preparing toolbar
            toolFrame = Frame(mainWindow, width=200, height = 600)
            toolFrame.grid(row = 0, column = 0, padx = 10, pady = 2)

            mouseChooseButton = Button(toolFrame, text = "Fare Secim", command = freeMouseChoice)
            mouseChooseButton.grid(row = 0, column = 0, padx = 5, pady = 2)

            #renderLabel.pack()

            mainWindow.mainloop()

    if __name__ == "__main__":
        main()

Tkinter dont allow me to use memory for refreshing GUI

There is a my sample kod. I want to do video rendering. For this propose, I wrote two thread one capture the screen from the camera consistently and save to disk and other one take this image from disk and update the my GUI. I use Tkinter for UI rendering.My problem is starting here. Speed of the refreshing is too low. The image comes to GUI intermittently.I want to use memory for transfer the images to GUI but Tkinter only allow me using disk, not memory. Do anyone give me any suggestions please? How I solve this problem on memory? :)

        # import the necessary packages
    import io
    from picamera.array import PiRGBArray
    from picamera import PiCamera
    import time
    import cv2
    import sys
    import traceback
    import threading
    from PIL import Image, ImageTk
    import numpy as np
    # GUI side imports
    from Tkinter import *
    import os
    from shutil import copyfile

    class ReaderThread(threading.Thread):

            def __init__(self):
                    super(ReaderThread, self).__init__()

            def run(self):
                    readMe()

    class UpdaterThread(threading.Thread):

            def __init__(self):
                    super(UpdaterThread, self).__init__()

            def run(self):
                    renderMe()

    def readMe():
        for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
            # grab the raw NumPy array representing the image, then initialize the timestamp
            # and occupied/unoccupied text
            try:
                    image = frame.array
                    cv2.imwrite("photo.jpg",image)
                    # clear the stream in preparation for the next frame
                    rawCapture.truncate(0)
            except:
                    exc_type, exc_value, exc_traceback = sys.exc_info()
                    traceback.print_exception(exc_type, exc_value, exc_traceback)

    # initialize the camera and grab a reference to the raw camera capture
    sWidth = 800
    sHeight = 600
    camera = PiCamera()
    camera.resolution = (sWidth, sHeight)
    camera.framerate = 15
    rawCapture = PiRGBArray(camera, size=(sWidth, sHeight))

    # allow the camera to warmup
    time.sleep(0.1)

    mainWindow = Tk()
    # Render area definition
    renderFrame = Frame(mainWindow, width = 800, height = 600)
    renderFrame.grid(row = 0, column = 1, padx = 10, pady =2)
    renderLabel = Label(renderFrame)
    renderLabel.grid(row = 0, column = 0, padx = 0, pady = 0)
    renderLabel.pack()

    # update frames on screen
    def renderMe():
            while 1:
                    if os.path.isfile("photo.jpg"):
                       copyfile("photo.jpg", "photo2.jpg")     
                       renderLabel["image"] = ImageTk.PhotoImage(file="photo2.jpg")
                    key = cv2.waitKey(1) & 0xFF
                    # if the `q` key was pressed, break from the loop
                    if key == ord("q"):
                       camera.close()
                       break

                    os.system('exit')

    # GUI interaction functions area
    def freeMouseChoice():
            print "You will choose the mouse my buddy"

    def main():
            try:
               rThread = ReaderThread()
               rThread.start()
               print "Reader thread started..."
               uThread = UpdaterThread()
               uThread.start()
               print "Updater thread started..."
            except:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                traceback.print_exception(exc_type, exc_value, exc_traceback)
                camera.close()

            mainWindow.wm_title("Con sinov - Bustard the BEKCI")
            mainWindow.config(background = "#000000")

            #Preparing toolbar
            toolFrame = Frame(mainWindow, width=200, height = 600)
            toolFrame.grid(row = 0, column = 0, padx = 10, pady = 2)

            mouseChooseButton = Button(toolFrame, text = "Fare Secim", command = freeMouseChoice)
            mouseChooseButton.grid(row = 0, column = 0, padx = 5, pady = 2)

            #renderLabel.pack()

            mainWindow.mainloop()

    if __name__ == "__main__":
        main()