0

I am trying to display the camera feed for my camera in a Tkinter window with a Raspberry pi (3b+) and the RPI camera module (v2).

Everything works fine when I keep the default settings (as set in the imutils lib). But when I change the camera resolution to the size of my display (1280x800) the framerate drops significantly.

Is it possible to show the full camera resolution at a 30+ fps? or keep the original resolution but stretch the image that is displayed in tkinter?

I am fairly new to development with the RPI and OpenCV, so I hope that the FPS can be fixed with code.

My code is below:

from tkinter import Tk, Frame, Label
from PIL import Image, ImageTk
import imutils
import cv2

from imutils.video.pivideostream import PiVideoStream
import time

window = Tk()
window.attributes('-fullscreen', True)

cameraLabel = Label(window)
cameraLabel.pack()

vs = PiVideoStream(resolution=(1280, 800)).start()
time.sleep(2.0)


def show_pi_video():
    while True:
        frame = vs.read()
        frame = imutils.resize(frame)
        cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
        img = Image.fromarray(cv2image)
        imgtk = ImageTk.PhotoImage(image=img)
        cameraLabel.config(image=imgtk)
        window.update()
    cv2.destroyAllWindows()
    vs.stop()

show_pi_video()
window.mainloop()
evanhutomo
  • 627
  • 1
  • 11
  • 24
RMCS
  • 383
  • 3
  • 17
  • Try to analyse what causes the slowdown by separating the image acquisition from the processing from the display. So, comment out everything in your loop except `frame=vs.read()` and run your program for 1000 frames and see how long the acquisition alone takes. Then run it again with just the resizing and colour conversion so you can see how long the processing takes. Then run it again with just the creation of the Tk label and the window update and see how fast that is. Once you know the numbers, update your question and hopefully you'll get a useful answer. – Mark Setchell May 15 '20 at 09:15
  • ***can i increase the FPS***: You are running `window.update()` without delay, therefore **No**, it's the limit of your hardware. – stovfl May 15 '20 at 09:19
  • You can also find the bottleneck by measuring and printing the time each step in your code takes. [example](https://stackoverflow.com/a/56928886/10699171) – J.D. May 15 '20 at 12:35

0 Answers0