使用 MSS 截取大量屏幕截图时,内存快速填满并崩溃 python
When taking many screenshots with MSS, memory fills quickly and crashes python
这是我的代码:
import time
import cv2
import mss
import numpy as np
Frame = [0, 0, 1920, 1080]
def GetFrame():
monitor = {"top": Frame[0], "left": Frame[1], "width": Frame[2], "height": Frame[3]}
sct_img = mss.mss().grab(monitor)
return np.asarray(sct_img)
while (True):
inimg = GetFrame()
cv2.imshow("WHY IS MEMORY SO HIGH???????", inimg)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
运行时,它不会抛出任何错误,但查看任务管理器时,我的内存很快就会被填满(大约 200 次迭代后),最终导致我的桌面崩溃,然后 python。我调查了 garbage collection,没有运气。
Python version 3.7.0
MSS version 4.0.1
嗯,我自己修好了,我不知道为什么它有效,但它确实。这是我的高智商解决方案:
import time
import cv2
import mss
import numpy as np
Frame = [0, 0, 1920, 1080]
def GetFrame():
#########solution here
with mss.mss() as sct: #<-- thats the solution.... yep
monitor = {"top": Frame[0], "left": Frame[1], "width": Frame[2], "height": Frame[3]}
sct_img = sct.grab(monitor)
return np.asarray(sct_img)
while (True):
inimg = GetFrame()
cv2.imshow("Normal memory!!", inimg)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
如果有人想发表评论并解释为什么这对我有用,那么我将不胜感激:)
这是我的代码:
import time
import cv2
import mss
import numpy as np
Frame = [0, 0, 1920, 1080]
def GetFrame():
monitor = {"top": Frame[0], "left": Frame[1], "width": Frame[2], "height": Frame[3]}
sct_img = mss.mss().grab(monitor)
return np.asarray(sct_img)
while (True):
inimg = GetFrame()
cv2.imshow("WHY IS MEMORY SO HIGH???????", inimg)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
运行时,它不会抛出任何错误,但查看任务管理器时,我的内存很快就会被填满(大约 200 次迭代后),最终导致我的桌面崩溃,然后 python。我调查了 garbage collection,没有运气。
Python version 3.7.0
MSS version 4.0.1
嗯,我自己修好了,我不知道为什么它有效,但它确实。这是我的高智商解决方案:
import time
import cv2
import mss
import numpy as np
Frame = [0, 0, 1920, 1080]
def GetFrame():
#########solution here
with mss.mss() as sct: #<-- thats the solution.... yep
monitor = {"top": Frame[0], "left": Frame[1], "width": Frame[2], "height": Frame[3]}
sct_img = sct.grab(monitor)
return np.asarray(sct_img)
while (True):
inimg = GetFrame()
cv2.imshow("Normal memory!!", inimg)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
如果有人想发表评论并解释为什么这对我有用,那么我将不胜感激:)