0

If I use OpenCV to play video into its own window using this sort of logic:

cap = cv2.VideoCapture('video.mp4',cv2.CAP_FFMPEG)
 while True:
        ret, frame = cap.read()
        if(ret):
            cv2.imshow('', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

then it works well and smoothly. But if I use what appears to be the recommended way of playing into my own tkinter window, using the window.after() technique like this snippet:

 def update(self):
    # Get a frame from the video source
    ret, frame = cap.read()
    self.photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(frame))
    self.canvas.create_image(0, 0, image = self.photo, anchor = tk.NW)
    self.update_id = self.window.after(self.VIDEO_READ_DELAY, self.update)

it is slow and stutters badly. I've played with the update delay without any real success, so I'm guessing that the processing overhead of the image conversion is what's causing the problem.

Can imshow() be made to play into my tkinter canvas directly?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
ceperman
  • 405
  • 2
  • 8
  • The following [post](https://stackoverflow.com/questions/16366857/show-webcam-sequence-tkinter) may be helpful. – Rotem Nov 29 '21 at 21:25
  • Tkinter has definite downsides. you just found one: `after()`. -- no, OpenCV's imshow can't "play into" Tkinter. -- the "usual" way with tkinter is using a label rather than a canvas. – Christoph Rackwitz Nov 30 '21 at 09:42
  • Why would you use a label rather than a canvas? I can see that most examples out there use a label, but a canvas seems more logical to me. Is there a performance implication? – ceperman Nov 30 '21 at 17:19

0 Answers0