1

I'm trying to create a GUI for playing a video that fills up the entire screen, while the button for Snapshot is still visible at the bottom. Right now, What i manage to do is just set the app window itself to fullscreen, resulting a small sized video playing at the top and a huge "snapshot" button at the button. Is there a way to make the video fill up the entire screen?

thanks!

from PIL import Image, ImageTk
import Tkinter as tk
import argparse
import datetime
import cv2
import os

class Application:
    def __init__(self, output_path = "./"):
        """ Initialize application which uses OpenCV + Tkinter. It displays
            a video stream in a Tkinter window and stores current snapshot on disk """
        self.vs = cv2.VideoCapture('Cat Walking.mp4') # capture video frames, 0 is your default video camera
        self.output_path = output_path  # store output path
        self.current_image = None  # current image from the camera

        self.root = tk.Tk()  # initialize root window
        self.root.title("PyImageSearch PhotoBooth")  # set window title
        # self.destructor function gets fired when the window is closed
        self.root.protocol('WM_DELETE_WINDOW', self.destructor)

        self.panel = tk.Label(self.root)  # initialize image panel
        self.panel.pack(padx=10, pady=10)

        # create a button, that when pressed, will take the current frame and save it to file
        btn = tk.Button(self.root, text="Snapshot!", command=self.take_snapshot)
        btn.pack(fill="both", expand=True, padx=10, pady=10)

        # start a self.video_loop that constantly pools the video sensor
        # for the most recently read frame
        self.video_loop()


    def video_loop(self):
        """ Get frame from the video stream and show it in Tkinter """
        ok, frame = self.vs.read()  # read frame from video stream
        if ok:  # frame captured without any errors
            cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)  # convert colors from BGR to RGBA
            self.current_image = Image.fromarray(cv2image)  # convert image for PIL
            imgtk = ImageTk.PhotoImage(image=self.current_image)  # convert image for tkinter
            self.panel.imgtk = imgtk  # anchor imgtk so it does not be deleted by garbage-collector
            self.root.attributes("-fullscreen",True)
            #self.oot.wm_state('zoomed')
            self.panel.config(image=imgtk)  # show the image

        self.root.after(1, self.video_loop)  # call the same function after 30 milliseconds

    def take_snapshot(self):
        """ Take snapshot and save it to the file """
        ts = datetime.datetime.now() # grab the current timestamp
        filename = "{}.jpg".format(ts.strftime("%Y-%m-%d_%H-%M-%S"))  # construct filename
        p = os.path.join(self.output_path, filename)  # construct output path
        self.current_image.save(p, "JPEG")  # save image as jpeg file
        print("[INFO] saved {}".format(filename))

    def destructor(self):
        """ Destroy the root object and release all resources """
        print("[INFO] closing...")
        self.root.destroy()
        self.vs.release()  # release web camera
        cv2.destroyAllWindows()  # it is not mandatory in this application

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", default="./",
    help="path to output directory to store snapshots (default: current folder")
args = vars(ap.parse_args())

# start the app
print("[INFO] starting...")
pba = Application(args["output"])
pba.root.mainloop()
David
  • 315
  • 3
  • 14

1 Answers1

1

It's not a hard task if you don't care about execution time! We knew that resizing of an image isn't a rocket science for common user, but under the hood it takes some time to resize each frame. And if you really wonder about time and options - there're many options to play around from numpy/scipy to skimage/skvideo.

But let's try to do something with your code "as is" so we have two options to play with: cv2 and Image. For testing I grabbed 20 secs of "Keyboard Cat" video from youtube (480p) and resize each frame upto 1080p, and GUI looks like this (fullscreen 1920x1080):

enter image description here

Resize Methods / timeit elapsed time of showing frames:

As you see - no big difference between theese two so here's a code (only Application class and video_loop changed):

#imports
try:
    import tkinter as tk
except:
    import Tkinter as tk
from PIL import Image, ImageTk
import argparse
import datetime
import cv2
import os


class Application:
    def __init__(self, output_path = "./"):
        """ Initialize application which uses OpenCV + Tkinter. It displays
            a video stream in a Tkinter window and stores current snapshot on disk """
        self.vs = cv2.VideoCapture('KeyCat.mp4') # capture video frames, 0 is your default video camera
        self.output_path = output_path  # store output path
        self.current_image = None  # current image from the camera

        self.root = tk.Tk()  # initialize root window
        self.root.title("PyImageSearch PhotoBooth")  # set window title

        # self.destructor function gets fired when the window is closed
        self.root.protocol('WM_DELETE_WINDOW', self.destructor)
        self.root.attributes("-fullscreen", True)

        # getting size to resize! 30 - space for button
        self.size = (self.root.winfo_screenwidth(), self.root.winfo_screenheight() - 30)

        self.panel = tk.Label(self.root)  # initialize image panel
        self.panel.pack(fill='both', expand=True)

        # create a button, that when pressed, will take the current frame and save it to file
        self.btn = tk.Button(self.root, text="Snapshot!", command=self.take_snapshot)
        self.btn.pack(fill='x', expand=True)

        # start a self.video_loop that constantly pools the video sensor
        # for the most recently read frame
        self.video_loop()

    def video_loop(self):
        """ Get frame from the video stream and show it in Tkinter """
        ok, frame = self.vs.read()  # read frame from video stream
        if ok:  # frame captured without any errors
            cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)  # convert colors from BGR to RGBA
            cv2image = cv2.resize(cv2image, self.size, interpolation=cv2.INTER_NEAREST)
            self.current_image = Image.fromarray(cv2image) #.resize(self.size, resample=Image.NEAREST)  # convert image for PIL
            self.panel.imgtk = ImageTk.PhotoImage(image=self.current_image)
            self.panel.config(image=self.panel.imgtk)  # show the image

            self.root.after(1, self.video_loop)  # call the same function after 30 milliseconds

But you knew - do such a things "on fly" isn't a good idea, so lets try to resize all frames first and then do all stuff(only Application class and video_loop method changed, resize_video method added):

class Application:
    def __init__(self, output_path = "./"):
        """ Initialize application which uses OpenCV + Tkinter. It displays
            a video stream in a Tkinter window and stores current snapshot on disk """
        self.vs = cv2.VideoCapture('KeyCat.mp4') # capture video frames, 0 is your default video camera
        ...
        # init frames
        self.frames = self.resize_video()
        self.video_loop()

def resize_video(self):
    temp = list()
    try:
        temp_count_const = cv2.CAP_PROP_FRAME_COUNT
    except AttributeError:
        temp_count_const = cv2.cv.CV_CAP_PROP_FRAME_COUNT

    frames_count = self.vs.get(temp_count_const)

    while self.vs.isOpened():
        ok, frame = self.vs.read()  # read frame from video stream
        if ok:  # frame captured without any errors
            cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)  # convert colors from BGR to RGBA
            cv2image = cv2.resize(cv2image, self.size, interpolation=cv2.INTER_NEAREST)
            cv2image = Image.fromarray(cv2image)  # convert image for PIL
            temp.append(cv2image)
            # simple progress print w/o sys import
            print('%d/%d\t%d%%' % (len(temp), frames_count, ((len(temp)/frames_count)*100)))
        else:
            return temp

def video_loop(self):
    """ Get frame from the video stream and show it in Tkinter """
    if len(self.frames) != 0:
        self.current_image = self.frames.pop(0)
        self.panel.imgtk = ImageTk.PhotoImage(self.current_image)
        self.panel.config(image=self.panel.imgtk)
        self.root.after(1, self.video_loop)  # call the same function after 30 milliseconds

timeit elapsed time of showing pre-resized frames: ~78.78 s.

As you see - resizing isn't a main problem of your script, but a good option!

CommonSense
  • 4,232
  • 2
  • 14
  • 38
  • Hey CommonSense, thanks for the great answer. Trying you code, I get cv2image = cv2.resize(cv2image, self.size, interpolation=cv2.INTER_NEAREST) error: C:\Users\David\Downloads\opencv-master\opencv-master\modules\core\src\alloc.cpp:52: error: (-4) Failed to allocate 9191424 bytes in function cv::OutOfMemoryError , any idea why? (im using 32 bit python) – David Apr 05 '17 at 05:37
  • How much RAM do you have and what your fullscreen resolution and length of a clip? Anyway, OpenCV is very sensetive to platform, so I tried code on 64-bit Win/64-bit Python/8 Gb RAM and got no error with 20 sec clip! But what about resizing with `Image` library? Did you try it? Just comment `cv2image = cv2.resize(...)` line, and rplace `cv2image = Image.fromarray(cv2image)` with `cv2image = Image.fromarray(cv2image).resize(self.size, resample=Image.NEAREST)`. – CommonSense Apr 05 '17 at 07:38
  • CommonSense, more or less same, when i reach 17% process, i get the mem error. I'm on a pretty strong PC, i7 7700k + 32gb ram on win10 64. I had to use 32 bit python because I probably built the openCV/openCV contrib modules in 32bit. You think that is my problem? I can try building openCV for 64bit – David Apr 05 '17 at 08:05
  • Sure you can try if it's only because of openCV. And your machine is far more superior than mine, so I think it's worth it. By the way, openCV (and many other usefull) packages are already builded for you [here](http://www.lfd.uci.edu/~gohlke/pythonlibs/) - just download and install `whl` with `pip`. – CommonSense Apr 05 '17 at 08:16
  • Anyway, your question was about "how to fullscreen this" not "why my memory leaking, should I build 64-bit OpenCV". If your really want to stick with OpenCV - create separate question and describe all what we got here, if 64-bit is not a solution - you will attract more people to the problem, because now we are alone here. But if you ain't an OpenCV's husband - try alternative packages like [ImageIO](http://stackoverflow.com/a/29742156/6634373) (much friendlier) or [scikit-image](https://github.com/danielballan/scikit-image/blob/video-guide/doc/source/user_guide/video.txt). – CommonSense Apr 05 '17 at 08:35
  • At last, I'm curious what you will get with 64-bit python, because with 32-bit you limited to 4gb of RAM, so feel free for callback! – CommonSense Apr 05 '17 at 09:09
  • Sure. Ill first try to build a 64biy version. Nonetheless you helped me alot. Ill get back tou after i try it out. Thanks! – David Apr 05 '17 at 17:08