Python Forum
Python + OpenCV + Tkinter playing video help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python + OpenCV + Tkinter playing video help
#1
Hey, all.
So here's my problem:
I need a GUI to get a live webcam stream from openCV (or just open a video file), pass it to the Tkinter GUI window, and add some buttons and stuff to it.
when I run the code and play the video, the video plays with proper FPS (same speed as just opening it with winows media player), however, the video window is small.
It's very important that i get the video to full screen dimentions, perhaps a tad less so I can still see the GUI button.

If i try to resize the video for full screen, by adding 
self.current_image= self.current_image.resize([2560,1440],PIL.Image.ANTIALIAS), my FPS drop and video is playing very slowly.

going with self.root.attributes("-fullscreen",True) , the window just goes full screen but the video size itself is same. so that's no good either.

How can I fill the screen with the video while keeping proper FPS? is it a code error or just slow processing?

here is the code: thanks evryone
import PIL 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('Kaabil Hoon (Kaabil) Hrithik Roshan (2K Ultra HD 1440p)-(HDLoft.Com).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)         self.root.config(cursor="arrow")         # 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 #        frame = cv2.resize(frame, (1500,1000))         if ok:  # frame captured without any errors             key = cv2.waitKey(1000)             cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)  # convert colors from BGR to RGBA             self.current_image = Image.fromarray(cv2image)  # convert image for PIL             #self.current_image= self.current_image.resize([1280,1024],PIL.Image.ANTIALIAS)             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.panel.config(image=imgtk)  # show the image             #self.root.attributes("-fullscreen",True)         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()
Reply
#2
I could be wrong here, but I think you're attempting to resize the area you are grabbing from the camera. However, you should be stretching the image after grabbing the normal resolution camera feed. Equally, you'd need to increase the Tkinter GUI window size after to make it match. If possible, resize the camera feed image to match the window size variables. Although, you may get aspect ratio problems then, so that'd be the next thing to improve upon.

All the best,

Joseph
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to receive rtmp video in Python? aksdjfh 0 822 Feb-27-2025, 02:58 PM
Last Post: aksdjfh
  Is there a Python solution to select unique frames from a video? rownong 1 1,336 Feb-02-2025, 09:56 AM
Last Post: Larz60+
  Playing a video in tkinter moon 10 13,983 Jul-25-2024, 04:04 PM
Last Post: moon
  Problem with using opencv in python Raunak1023984765 21 17,377 Feb-21-2024, 04:25 PM
Last Post: Pedroski55
  How would I be able to detect a media player app playing a video. phpjunkie 2 1,951 Oct-16-2023, 02:09 PM
Last Post: phpjunkie
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 3,130 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Shapes over video in tkinter finndude 1 2,452 Oct-04-2022, 06:14 PM
Last Post: deanhystad
  Tkinter Drawing/Annotating on live video KDog 4 5,178 Jan-24-2022, 03:18 PM
Last Post: Minimaxer
  How to output Python in Spout or any other video sharing network? buzzdarkyear 4 4,268 Jan-11-2022, 11:37 AM
Last Post: buzzdarkyear
  Python OpenCV window not opening in fullscreen mode Zman350x 0 5,783 Apr-29-2021, 07:54 PM
Last Post: Zman350x

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.