如何在PysimpleGUI中加速更新图像元素



我正在使用PysimpleGUI的Image元素,其中我在从相机接收帧后更新图像元素,或者有时更新相同的数据,如果没有收到

来自相机的记录在一个单独的线程中处理,而更新元素在主线程中

问题是关于更新单个元素所花费的时间,大约是60毫秒,而对于16个元素,它大约是0.5s,这太高了因此,在可视化中存在滞后效应

这是测试的代码,让我知道我如何加快元素的更新,因为我要使用相同的32图像元素

import PySimpleGUI as sg
import cv2
import time
video_capture1 = cv2.VideoCapture(0)
def layouting(_format, window_name):
if _format == '1x1':
layout = [[sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "11",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True)]]
return layout
elif _format == '16_':
layout = [[
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "11",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True),
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "12",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True),
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "13",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True),
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "14",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True)
],
[
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "21",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True),
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "22",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True),
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "23",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True),
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "24",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True)
],
[
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "31",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True),
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "32",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True),
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "33",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True),
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "34",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True)
],
[
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "41",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True),
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "42",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True),
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "43",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True),
sg.Image(filename='A1_Logo.png', key=f"{window_name}" + "44",
right_click_menu=['R&ight', ['Add Camera', 'Remove']], enable_events=True)
]]
return layout
camera_no = 16
select_layout = "16_"    #'16_' or '1x1' change for 16 and 1 element layout respectively
select_window = 'window-1'
window = sg.Window('AI-VMS',layouting(select_layout, select_window),resizable=True,size=(900,900),location=(500,20),icon='a1-fence-logo.ico',element_justification='center',finalize=True)
window.maximize()
element_keys = list(window.AllKeysDict.keys())
while True:
event,values = window.read(timeout=5)
if event == sg.WIN_CLOSED or event == 'Exit':
break
ret, frameOrig = video_capture1.read()
start = time.time()
for i in element_keys:
if select_layout == "16_":
frame = cv2.resize(frameOrig, (int(window.size[0]/4.2),int(window.size[1]/4.2)))
else:
frame = cv2.resize(frameOrig, (window.size[0], window.size[1]))
imgbytes = cv2.imencode(".png", frame)[1].tobytes()
window[i].update(data=imgbytes)
print(time.time() - start)

video_capture1.release()
window.close()

上面的例子有1 cv2读取并显示在所有16个元素上,这需要大约150毫秒,我希望更新在微秒内完成,或者最大10到20毫秒

请帮助您的想法和如何管理相机和UI在软件有没有行业标准?

更新16张图片的时间可能不到50ms。这需要更多的时间,因为您需要调整图像的大小并对其进行编码以进行更新。

from time import time
from random import randint
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
import PySimpleGUI as sg

def image_to_data(im):
"""
Image object to bytes object.
: Parameters
im - Image object
: Return
bytes object.
"""
with BytesIO() as output:
im.save(output, format="PNG")
data = output.getvalue()
return data
# Prepare images for Image update
images = []
width, height = size = (350, 150)
font = ImageFont.truetype("arial.ttf", 40)
for i in range(16):
im = Image.new("RGBA", size, 'blue')
draw = ImageDraw.Draw(im)
draw.text((width//2, height//2), f"Image {i}", fill='white', anchor="mm", font=font)
images.append(image_to_data(im))
class Image(sg.Image):
# Auto key for Image element
index = 0
def __init__(self, *args, **kwargs):
kwargs.update({
"data":images[randint(0, 15)],
"size":size,
"key":f"Image {Image.index}"
})
Image.index += 1
super().__init__(*args, **kwargs)
layout = [[Image() for i in range(4)] for j in range(4)]
window = sg.Window("Title", layout)

while True:
event, values = window.read(timeout=10)
if event == sg.WIN_CLOSED:
break
elif event == sg.TIMEOUT_EVENT:
old = time()
for i in range(16):
window[f"Image {i}"].update(data=images[randint(0, 15)])
now = time()
print(f"{int((now - old)*1000)}ms")
old = now
window.close()

相关内容

  • 没有找到相关文章

最新更新