我将使用“ibaiGorodo/pyKinectAzure”创建一个流媒体程序。需要以帧为单位从设备接收图像,但在接收图像时会发生内存泄漏。
之后,在pyside中使用'fromImage()'时再次发生内存泄漏。
似乎 get_images 中的图像没有返回,而是堆叠在内存中的某个地方。
我怎么解决这个问题?
import threading
import sys
import cv2 as cv
import numpy as np
import pyAzKinct # using this library: https://github.com/ibaiGorordo/pyKinectAzure
from PySide6.QtGui import *
from PySide6.QtCore import *
from PySide6.QtWidgets import *
import psutil
class GUI_interface(QMainWindow):
def __init__(self):
super(GUI_interface,self).__init__()
self.running = False
self.setWindowTitle('test')
self.setFixedSize(700, 700)
self.component_init()
self.component_method()
self.show()
def component_init(self):
# quit process button
self.quit_btn = QPushButton('Q u i t', self)
self.quit_btn.setGeometry(1250, 150, 120, 50)
self.quit_btn.move(20, 620)
# start screen1 with black image
self.img1 = cv.imread('img1.png')
self.img1 = cv.cvtColor(self.img1, cv.COLOR_BGR2RGB)
qImg1 = QImage(self.img1, self.img1.shape[1], self.img1.shape[0], QImage.Format_RGB888)
self.pixmap1 = QPixmap.fromImage(qImg1)
self.label1 = QLabel(self)
self.label1.setPixmap(self.pixmap1)
self.label1.resize(600, 600)
self.label1.move(20, 20)
self.camera = pyAzKinct.transfer_img(0, "master")
def component_method(self):
memory_usage("pre-start")
self.start()
self.quit_btn.clicked.connect(self.onExit)
def run(self):
while True:
memory_usage("screen Threading")
img1 = pyAzKinct.get_combine_images(self.camera) # Main problem
memory_usage("check1-1")
crop_img1 = img1[236:844,656:1264]
memory_usage("check1-2")
crop_img1 = cv.cvtColor(crop_img1, cv.COLOR_BGR2RGB)
memory_usage("check1-3")
data1 = QByteArray(crop_img1.tobytes())
memory_usage("check1-4")
qImg1 = QImage(data1, crop_img1.shape[1], crop_img1.shape[0], QImage.Format_RGB888)
memory_usage("check1-5")
pImg1 = QPixmap.fromImage(qImg1) #second problem
memory_usage("check1-6")
self.label1.setPixmap(pImg1) # apply image
memory_usage("check1-7")
self.label1.update() # refreshing image
memory_usage("check1-8")
def stop(self):
self.camera.capture_release()
print("stoped..")
def start(self):
th = threading.Thread(target=self.run)
th.daemon = True
th.start()
print("started..")
def onExit(self):
print("exit")
self.stop()
QCoreApplication.instance().quit()
def memory_usage(message: str = 'debug'):
# current process RAM usage
p = psutil.Process()
rss = p.memory_info().rss / 2 ** 20 # Bytes to MB
print(f"[{message}] memory usage: {rss: 10.5f} MB")
if __name__ == '__main__':
cv.imwrite('img1.png', np.zeros((600, 600), dtype=np.uint8))
app = QApplication(sys.argv)
test=GUI_interface()
sys.exit(app.exec())
这是内存使用日志:
[pre-start] memory usage: 128.82422 MB
[screen Threading] memory usage: 128.87891 MBstarted..
[get_images 1] memory usage: 140.22656 MB
[get_images handle] memory usage: 162.60156 MB
[get_images color] memory usage: 162.66406 MB
[get_images depth] memory usage: 217.75391 MB
[get_images trans] memory usage: 241.04688 MB
[get_images combine] memory usage: 247.01562 MB
[check1-1] memory usage: 239.10156 MB
[check1-2] memory usage: 239.10156 MB
[check1-3] memory usage: 240.16016 MB
[check1-4] memory usage: 241.22656 MB
[check1-5] memory usage: 241.23047 MB
[check1-6] memory usage: 242.66016 MB
[check1-7] memory usage: 242.66016 MB
[check1-8] memory usage: 242.66406 MB
[screen Threading] memory usage: 242.66406 MB
[get_images 1] memory usage: 242.66406 MB
[get_images handle] memory usage: 242.66406 MB
[get_images color] memory usage: 242.66406 MB
[get_images depth] memory usage: 288.49609 MB
[get_images trans] memory usage: 295.50000 MB
[get_images combine] memory usage: 301.43750 MB
[check1-1] memory usage: 287.58984 MB
[check1-2] memory usage: 286.53125 MB
[check1-3] memory usage: 287.58984 MB
[check1-4] memory usage: 288.64844 MB
[check1-5] memory usage: 287.58984 MB
[check1-6] memory usage: 289.00391 MB
[check1-7] memory usage: 287.58984 MB
[check1-8] memory usage: 287.58984 MB
[screen Threading] memory usage: 287.58984 MB
[get_images 1] memory usage: 287.58984 MB
[get_images handle] memory usage: 287.58984 MB
[get_images color] memory usage: 287.58984 MB
[get_images depth] memory usage: 306.61719 MB
[get_images trans] memory usage: 311.58594 MB
[get_images combine] memory usage: 317.51953 MB
[check1-1] memory usage: 303.67188 MB
[check1-2] memory usage: 302.61328 MB
[check1-3] memory usage: 303.67188 MB
[check1-4] memory usage: 304.73047 MB
[check1-5] memory usage: 303.67188 MB
[check1-6] memory usage: 305.08594 MB
[check1-7] memory usage: 303.67188 MB
[check1-8] memory usage: 303.67188 MB
[screen Threading] memory usage: 303.67188 MB
[get_images 1] memory usage: 303.67188 MB
[get_images handle] memory usage: 303.67188 MB
[get_images color] memory usage: 303.67188 MB
[get_images depth] memory usage: 324.22656 MB
[get_images trans] memory usage: 329.18359 MB
[get_images combine] memory usage: 335.11719 MB
[check1-1] memory usage: 321.26953 MB
[check1-2] memory usage: 320.21094 MB
[check1-3] memory usage: 321.26953 MB
[check1-4] memory usage: 322.32812 MB
[check1-5] memory usage: 321.26953 MB
[check1-6] memory usage: 322.68359 MB
[check1-7] memory usage: 321.26953 MB
[check1-8] memory usage: 321.26953 MB
[screen Threading] memory usage: 321.26953 MB
[get_images 1] memory usage: 321.26953 MB
[get_images handle] memory usage: 321.26953 MB
[get_images color] memory usage: 321.26953 MB
[get_images depth] memory usage: 340.57812 MB
[get_images trans] memory usage: 346.57031 MB
[get_images combine] memory usage: 353.50781 MB
[check1-1] memory usage: 339.66016 MB
[check1-2] memory usage: 338.60156 MB
[check1-3] memory usage: 339.66016 MB
[check1-4] memory usage: 340.71875 MB
[check1-5] memory usage: 339.66016 MB
[check1-6] memory usage: 341.07422 MB
[check1-7] memory usage: 339.66016 MB
[check1-8] memory usage: 339.66016 MB
[screen Threading] memory usage: 339.66016 MB
[get_images 1] memory usage: 339.66016 MB
[get_images handle] memory usage: 339.66016 MB
[get_images color] memory usage: 339.66016 MB
[get_images depth] memory usage: 358.10938 MB
[get_images trans] memory usage: 361.62500 MB
[get_images combine] memory usage: 367.55859 MB
[check1-1] memory usage: 352.70703 MB
[check1-2] memory usage: 351.64844 MB
[check1-3] memory usage: 352.70703 MB
[check1-4] memory usage: 353.76562 MB
[check1-5] memory usage: 352.70703 MB
[check1-6] memory usage: 354.12109 MB
[check1-7] memory usage: 352.70703 MB
[check1-8] memory usage: 352.70703 MB
[screen Threading] memory usage: 352.70703 MB
[get_images 1] memory usage: 352.70703 MB
[get_images handle] memory usage: 352.70703 MB
[get_images color] memory usage: 352.70703 MB
[get_images depth] memory usage: 369.07031 MB
[get_images trans] memory usage: 374.06250 MB
[get_images combine] memory usage: 379.99609 MB
[check1-1] memory usage: 366.14844 MB
[check1-2] memory usage: 365.08984 MB
[check1-3] memory usage: 366.14844 MB
[check1-4] memory usage: 367.20703 MB
[check1-5] memory usage: 366.14844 MB
[check1-6] memory usage: 367.56250 MB
[check1-7] memory usage: 366.14844 MB
[check1-8] memory usage: 366.14844 MB
[screen Threading] memory usage: 366.14844 MB
[get_images 1] memory usage: 366.14844 MB
[get_images handle] memory usage: 366.14844 MB
[get_images color] memory usage: 366.14844 MB
[get_images depth] memory usage: 381.38281 MB
[get_images trans] memory usage: 387.37500 MB
[get_images combine] memory usage: 394.31250 MB
[check1-1] memory usage: 380.46484 MB
[check1-2] memory usage: 379.40625 MB
[check1-3] memory usage: 380.46484 MB
[check1-4] memory usage: 381.52344 MB
[check1-5] memory usage: 380.46484 MB
[check1-6] memory usage: 381.87891 MB
[check1-7] memory usage: 380.46484 MB
[check1-8] memory usage: 380.46484 MB
[screen Threading] memory usage: 380.46484 MB
[get_images 1] memory usage: 380.46484 MB
[get_images handle] memory usage: 380.46484 MB
[get_images color] memory usage: 380.46484 MB
[get_images depth] memory usage: 394.29297 MB
[get_images trans] memory usage: 401.29297 MB
[get_images combine] memory usage: 407.22656 MB
[check1-1] memory usage: 393.37891 MB
[check1-2] memory usage: 392.32031 MB
[check1-3] memory usage: 393.37891 MB
[check1-4] memory usage: 394.43750 MB
[check1-5] memory usage: 393.37891 MB
[check1-6] memory usage: 394.79297 MB
[check1-7] memory usage: 393.37891 MB
[check1-8] memory usage: 393.37891 MB
[screen Threading] memory usage: 393.37891 MB
[get_images 1] memory usage: 393.37891 MB
[get_images handle] memory usage: 393.37891 MB
[get_images color] memory usage: 393.37891 MB
[get_images depth] memory usage: 408.21484 MB
[get_images trans] memory usage: 414.20703 MB
[get_images combine] memory usage: 419.13672 MB
[check1-1] memory usage: 405.28906 MB
[check1-2] memory usage: 404.23047 MB
[check1-3] memory usage: 405.28906 MB
[check1-4] memory usage: 406.34766 MB
[check1-5] memory usage: 405.28906 MB
[check1-6] memory usage: 406.70312 MB
[check1-7] memory usage: 405.28906 MB
[check1-8] memory usage: 405.28906 MB
[screen Threading] memory usage: 405.28906 MB
[get_images 1] memory usage: 405.28906 MB
[get_images handle] memory usage: 405.28906 MB
[get_images color] memory usage: 405.28906 MB
[get_images depth] memory usage: 421.28516 MB
[get_images trans] memory usage: 426.21484 MB
[get_images combine] memory usage: 433.15234 MB
[check1-1] memory usage: 419.30469 MB
[check1-2] memory usage: 418.24609 MB
[check1-3] memory usage: 419.30469 MB
[check1-4] memory usage: 420.36328 MB
[check1-5] memory usage: 419.30469 MB
[check1-6] memory usage: 420.71875 MB
[check1-7] memory usage: 419.30469 MB
[check1-8] memory usage: 419.30469 MB
[screen Threading] memory usage: 419.30469 MB
[get_images 1] memory usage: 419.30469 MB
[get_images handle] memory usage: 419.30469 MB
[get_images color] memory usage: 419.30469 MB
[get_images depth] memory usage: 433.71484 MB
[get_images trans] memory usage: 440.71484 MB
[get_images combine] memory usage: 446.64844 MB
[check1-1] memory usage: 432.80078 MB
[check1-2] memory usage: 431.74219 MB
[check1-3] memory usage: 432.80078 MB
[check1-4] memory usage: 433.85938 MB
[check1-5] memory usage: 432.80078 MB
[check1-6] memory usage: 434.21484 MB
[check1-7] memory usage: 432.80078 MB
[check1-8] memory usage: 432.80078 MB
[screen Threading] memory usage: 432.80078 MB
[get_images 1] memory usage: 432.80078 MB
[get_images handle] memory usage: 432.80078 MB
[get_images color] memory usage: 432.80078 MB
[get_images depth] memory usage: 449.04688 MB
[get_images trans] memory usage: 455.03906 MB
[get_images combine] memory usage: 459.96875 MB
[check1-1] memory usage: 446.12109 MB
[check1-2] memory usage: 445.06250 MB
[check1-3] memory usage: 446.12109 MB
[check1-4] memory usage: 447.17969 MB
[check1-5] memory usage: 446.12109 MB
[check1-6] memory usage: 447.53516 MB
[check1-7] memory usage: 446.12109 MB
[check1-8] memory usage: 446.12109 MB
[screen Threading] memory usage: 446.12109 MB
[get_images 1] memory usage: 446.12109 MB
[get_images handle] memory usage: 446.12109 MB
[get_images color] memory usage: 446.12109 MB
[get_images depth] memory usage: 461.46875 MB
[get_images trans] memory usage: 466.45703 MB
[get_images combine] memory usage: 472.39062 MB
[check1-1] memory usage: 458.54297 MB
[check1-2] memory usage: 457.48438 MB
[check1-3] memory usage: 458.54297 MB
[check1-4] memory usage: 459.60156 MB
[check1-5] memory usage: 458.54297 MB
[check1-6] memory usage: 459.95703 MB
[check1-7] memory usage: 458.54297 MB
[check1-8] memory usage: 458.54297 MB
[screen Threading] memory usage: 458.54297 MB
[get_images 1] memory usage: 458.54297 MB
[get_images handle] memory usage: 458.54297 MB
[get_images color] memory usage: 458.54297 MB
[get_images depth] memory usage: 473.82422 MB
[get_images trans] memory usage: 479.81641 MB
[get_images combine] memory usage: 486.75391 MB
[check1-1] memory usage: 472.90625 MB
[check1-2] memory usage: 471.84766 MB
[check1-3] memory usage: 472.90625 MB
[check1-4] memory usage: 473.96484 MB
[check1-5] memory usage: 472.90625 MB
[check1-6] memory usage: 474.32031 MB
[check1-7] memory usage: 472.90625 MB
[check1-8] memory usage: 472.90625 MB
[screen Threading] memory usage: 472.90625 MB
[get_images 1] memory usage: 472.90625 MB
[get_images handle] memory usage: 472.90625 MB
[get_images color] memory usage: 472.90625 MB
[get_images depth] memory usage: 487.07031 MB
[get_images trans] memory usage: 494.06641 MB
[get_images combine] memory usage: 498.99609 MB
[check1-1] memory usage: 485.14844 MB
[check1-2] memory usage: 484.08984 MB
[check1-3] memory usage: 485.14844 MB
[check1-4] memory usage: 486.20703 MB
[check1-5] memory usage: 485.14844 MB
[check1-6] memory usage: 486.56250 MB
[check1-7] memory usage: 485.14844 MB
[check1-8] memory usage: 485.14844 MB
[screen Threading] memory usage: 485.14844 MB
[get_images 1] memory usage: 485.14844 MB
[get_images handle] memory usage: 485.14844 MB
[get_images color] memory usage: 485.14844 MB
[get_images depth] memory usage: 501.31250 MB
[get_images trans] memory usage: 506.30078 MB
[get_images combine] memory usage: 512.23438 MB
[check1-1] memory usage: 498.38672 MB
[check1-2] memory usage: 497.32812 MB
[check1-3] memory usage: 498.38672 MB
[check1-4] memory usage: 499.44531 MB
[check1-5] memory usage: 498.38672 MB
[check1-6] memory usage: 499.80078 MB
[check1-7] memory usage: 498.38672 MB
[check1-8] memory usage: 498.38672 MB
[screen Threading] memory usage: 498.38672 MB
[get_images 1] memory usage: 498.38672 MB
[get_images handle] memory usage: 498.38672 MB
[get_images color] memory usage: 498.38672 MB
[get_images depth] memory usage: 514.39453 MB
[get_images trans] memory usage: 521.33203 MB
[get_images combine] memory usage: 526.26172 MB
[check1-1] memory usage: 512.41406 MB
[check1-2] memory usage: 511.35547 MB
[check1-3] memory usage: 512.41406 MB
[check1-4] memory usage: 513.47266 MB
[check1-5] memory usage: 512.41406 MB
[check1-6] memory usage: 513.82812 MB
[check1-7] memory usage: 512.41406 MB
[check1-8] memory usage: 512.41406 MB
[screen Threading] memory usage: 512.41406 MB
[get_images 1] memory usage: 512.41406 MB
[get_images handle] memory usage: 512.41406 MB
[get_images color] memory usage: 512.41406 MB
[get_images depth] memory usage: 528.38281 MB
[get_images trans] memory usage: 534.31641 MB
[get_images combine] memory usage: 540.25000 MB
[check1-1] memory usage: 526.40234 MB
[check1-2] memory usage: 525.34375 MB
[check1-3] memory usage: 526.40234 MB
[check1-4] memory usage: 527.46094 MB
[check1-5] memory usage: 526.40234 MB
[check1-6] memory usage: 527.81641 MB
[check1-7] memory usage: 526.40234 MB
[check1-8] memory usage: 526.40234 MB
[screen Threading] memory usage: 526.40234 MB
[get_images 1] memory usage: 526.40234 MB
[get_images handle] memory usage: 526.40234 MB
[get_images color] memory usage: 526.40234 MB
[get_images depth] memory usage: 540.32422 MB
[get_images trans] memory usage: 546.25781 MB
[get_images combine] memory usage: 553.19531 MB
[check1-1] memory usage: 539.34766 MB
[check1-2] memory usage: 538.28906 MB
[check1-3] memory usage: 539.34766 MB
[check1-4] memory usage: 540.40625 MB
[check1-5] memory usage: 539.34766 MB
[check1-6] memory usage: 540.76172 MB
[check1-7] memory usage: 539.34766 MB
[check1-8] memory usage: 539.34766 MB
[screen Threading] memory usage: 539.34766 MB
[get_images 1] memory usage: 539.34766 MB
[get_images handle] memory usage: 539.34766 MB
[get_images color] memory usage: 539.34766 MB
[get_images depth] memory usage: 554.52734 MB
[get_images trans] memory usage: 559.48438 MB
[get_images combine] memory usage: 565.41797 MB
[check1-1] memory usage: 555.17969 MB
[check1-2] memory usage: 554.12109 MB
[check1-3] memory usage: 555.48438 MB
[check1-4] memory usage: 553.63281 MB
[check1-5] memory usage: 552.57422 MB
[check1-6] memory usage: 553.98828 MB
[check1-7] memory usage: 552.57422 MB
[check1-8] memory usage: 552.57422 MB
[screen Threading] memory usage: 552.57422 MB
[get_images 1] memory usage: 552.57422 MB
[get_images handle] memory usage: 552.57422 MB
[get_images color] memory usage: 552.57422 MB
[get_images depth] memory usage: 566.28906 MB
[get_images trans] memory usage: 572.22266 MB
[get_images combine] memory usage: 579.16016 MB
[check1-1] memory usage: 565.31250 MB
[check1-2] memory usage: 564.25391 MB
[check1-3] memory usage: 565.31250 MB
[check1-4] memory usage: 566.37109 MB
[check1-5] memory usage: 565.31250 MB
[check1-6] memory usage: 566.72656 MB
[check1-7] memory usage: 565.31250 MB
[check1-8] memory usage: 565.31250 MB
[screen Threading] memory usage: 565.31250 MB
[get_images 1] memory usage: 565.31250 MB
[get_images handle] memory usage: 565.31250 MB
[get_images color] memory usage: 565.31250 MB
[get_images depth] memory usage: 579.51562 MB
[get_images trans] memory usage: 586.51172 MB
[get_images combine] memory usage: 592.44531 MB
[check1-1] memory usage: 578.59766 MB
[check1-2] memory usage: 577.53906 MB
[check1-3] memory usage: 578.59766 MB
[check1-4] memory usage: 579.65625 MB
[check1-5] memory usage: 578.59766 MB
[check1-6] memory usage: 580.01172 MB
[check1-7] memory usage: 578.59766 MB
[check1-8] memory usage: 578.59766 MB
[screen Threading] memory usage: 578.59766 MB
[get_images 1] memory usage: 578.59766 MB
[get_images handle] memory usage: 578.59766 MB
[get_images color] memory usage: 578.59766 MB
[get_images depth] memory usage: 592.60156 MB
[get_images trans] memory usage: 598.03906 MB
[get_images combine] memory usage: 603.97266 MB
[check1-1] memory usage: 590.12500 MB
[check1-2] memory usage: 589.06641 MB
[check1-3] memory usage: 590.12500 MB
[check1-4] memory usage: 591.18359 MB
[check1-5] memory usage: 590.12500 MB
[check1-6] memory usage: 591.53906 MB
[check1-7] memory usage: 590.12500 MB
[check1-8] memory usage: 590.12500 MB