【发布时间】:2021-10-20 03:27:33
【问题描述】:
有没有办法在重绘新图像之前清除 PySimpleGUI 中的图形?我注意到函数 window["-GRAPH-"].draw_image() 在程序运行一段时间时会导致严重的内存泄漏,因为它试图将图片堆叠在所有绘制的图像之上.
背景:
我的应用正在尝试显示来自网络摄像头的实时提要,同时还会在摄像头提要上进行一些绘图(取决于鼠标点击)。为了从相机源中检测鼠标点击事件,我使用 sg.Graph 来捕获鼠标位置。
sample_app_display:user label the box of object in a live camera feed
代码片段:
sg.Graph(853, (0, 480), (853, 0), key="-GRAPH-", change_submits=True, drag_submits=False)
...
camera = my_opencv_library(device=0)
while True:
event, values = window.read(timeout=20)
if event == "-GRAPH-":
camera.update_coordinate(values["-GRAPH-"])
# obtain live feed with runtime drawing (based on mouse click)
frame = camera.get_frame()
imgbytes = cv2.imencode(".png", frame)[1].tobytes()
window["-GRAPH-"].draw_image(data=imgbytes, location=(0,0))
【问题讨论】:
-
我想答案是可用的here。我希望这会有所帮助。
-
感谢@A_Tiny_Speck_In_Programming,它确实解决了我的问题。
标签: python python-3.x user-interface pysimplegui