For a project for uni, i have to make a GUI that show some telemetry data and video feed of a self-made drone. But for some reason i have problems with the FPS of my video feed. The code snippet below shows my function that receives the video frames from a socket. When i calculate my fps, it is very very volatile. I think it is due to these lines: self.video_label.image.paste(image) self.video_label.config(image=photo)
. What should i do? How do i improve it or fix it?
def receive_video_frames(self):
while self.imp_started == 0:
time.sleep(1)
startup_signal = "1"
self.tcp_video_socket.sendall(startup_signal.encode())
print("startup_signal is sent")
video_tuning_thread = threading.Thread(target=self.tune_video)
video_tuning_thread.start()
start_time = time.time()
data, addr = self.video_udp_socket.recvfrom(10000000) # Adjust buffer size as needed
image_cv2 = cv2.imdecode(np.frombuffer(data, dtype=np.uint8), cv2.IMREAD_COLOR)
image_cv2 = cv2.resize(image_cv2, (640, 360))
image = Image.fromarray(cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB))
photo = ImageTk.PhotoImage(image=image)
self.video_label.config(image=photo)
self.video_label.image = photo
start_average = time.time()
end_average = time.time()
counter = 0
end_time = None
while True:
try:
average_time = end_average - start_average
if average_time >= 1:
self.average_FPS = counter / average_time
counter = 0
start_average = time.time()
data, addr = self.video_udp_socket.recvfrom(10000000)
counter += 1
image_cv2 = cv2.imdecode(np.frombuffer(data, dtype=np.uint8), cv2.IMREAD_COLOR)
image_cv2 = cv2.resize(image_cv2, (640, 360))
image = Image.fromarray(cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB))
# Problem i think
self.video_label.image.paste(image)
self.video_label.config(image=photo)
self.video_label.image = photo
end_time_real = time.time()
self.FPS = 1 / (end_time_real - start_time)
except Exception as e:
print("Error opening video frame:", e)
pass
I timed every part of my code and i think the problem is due to two lines:
`self.video_label.image.paste(image), self.video_label.config(image=photo)`.
How do i fix it?
Jules Franssens is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.