0

我正在开发一个图像采集应用程序。我在 MacOS sierra 上使用 PyQt4、pydc1394(用于火线相机的 libdc1394 Python 包装器),相机是 AVT Marlin F131B。我设法编写了一个可以从相机获取图像的程序,但是,该程序在 10 分钟后崩溃(这个时间可能因不同的计算机而异)返回Segmentation Fault

最初,我怀疑 pydc1394 中存在内存泄漏。但是,当我在停用 QThread.emit 的情况下运行代码时,代码运行得非常好。当我尝试将主窗口连接到 Qthread 对象时,问题就出现了。

附上我的代码。你知道发生了什么吗?

谢谢

from PyQt4 import QtGui, QtCore
from pydc1394 import DC1394Library, Camera
from threading import Thread
from datetime import datetime as dt
import time, sys, os, cv2
import numpy as np

class AcquisitionThread(QtCore.QThread):
    sig = QtCore.pyqtSignal(object)

    def __init__(self,cam):
        super(AcquisitionThread,self).__init__()

        self._cam = cam
        self._running = True
        self.start()
        print 'acquisition thread initiated'


    def run(self):
        while self._cam.running and self._running:
            frame = self._cam.current_image
            timestamp = frame.timestamp
            image = np.asarray(frame[:,:])
            self.sig.emit(frame)
            now_time = str(dt.now())
        return 

    def terminate(self):
        self._running = False
        print 'acquisition thread terminated'
        self.wait()
        print "Exited with success"

class MainWindow(QtGui.QWidget):
    def __init__(self):
        super(MainWindow,self).__init__()
        self.resize(700,540)
        self.move(300,300)
        self.setWindowTitle("Camera Acquisition")
        self.scene = QtGui.QGraphicsScene(self)
        self.view = QtGui.QGraphicsView(self.scene)
        self.view.setSizeIncrement(2,2)
        self.vbox = QtGui.QVBoxLayout()
        self.vbox.addWidget(self.view)
        self.setLayout(self.vbox)

        #initiate camera
        l = DC1394Library()
        try:
            list_cameras = l.enumerate_cameras()
        except:
            list_cameras = []

        if len(list_cameras) > 0:
            print len(list_cameras), "camera(s) found:"
            for i,c in enumerate(list_cameras):
                print i,"\t",c['guid']
        else:
            print "No camera was found. Program exiting ..."
            sys.exit()
        self._cam = Camera(l,guid = l.enumerate_cameras()[0]['guid'])

        #start camera 
        self._cam.start(interactive=True)
        print "camera started"
        time.sleep(1)

        self.acquisitionThread = AcquisitionThread(self._cam)
        self.acquisitionThread.sig.connect(self.displayImage)

        self.show()

    def displayImage(self,frame):
        timestamp = frame.timestamp
        #print timestamp
        now_time = str(dt.now())

        print now_time,"\t",timestamp
        img = np.asarray(frame[:,:])
        h,w = img.shape
        pix = QtGui.QPixmap(QtGui.QImage(img.tostring(),w,h,QtGui.QImage.Format_RGB888).rgbSwapped())
        self.scene.clear()
        self.scene.addPixmap(pix)
        #print "done displaying"

    def closeEvent(self,event):
        self._cam.stop()
        print "camera stopped"
        self._cam.close()
        print "camera closed"
        self.acquisitionThread.terminate()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_())
4

0 回答 0