I am developing an application for image acquisition. I am using PyQt4, pydc1394 (libdc1394 Python wrapper for firewire camera) on a MacOS sierra and the camera is AVT Marlin F131B. I managed to write a program that can acquire images from the camera, however, this program crushes after 10min (this time may vary on different computers) returning a Segmentation Fault.
Initially, I was suspecting a memory leak in the pydc1394. However, when I run the code with QThread.emit deactivated, the code runs very well. The problem raises up when I try to connect the main window to the Qthread object.
My code is attached. Do you have any idea about what is going on please?
Thank you
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_())