我是pyqt的新手,我正在尝试创建一个程序来通过网络摄像头捕获某人的脸部图像。该应用程序中有一个线程正在显示视频源,如果您按下按钮,它将显示您的脸部图像或默认的“找不到脸部”图像 data / NoFace.png 。
问题是它在大约2/3的时间内运行良好,有时在我第一次按下“获取图像”按钮时崩溃。它也不打印stacktrace。它给了我这个: 进程结束,退出代码为-1073741819(0xC0000005)
所以我不确定如何解决它。有人可以帮我解释一下吗?
main.py
class FaceApp(QMainWindow):
def __init__(self):
super().__init__()
self.haar_cascade = cv2.CascadeClassifier(
'data/haarcascade_frontalface_alt.xml')
self.videoRunning = None
self.photoData = SharedData()
self.initUI()
def setImage(self, image):
self.videoFeed.setPixmap(QPixmap.fromImage(image))
def setFaceImg(self):
if self.photoData.hasphoto:
img = self.photoData.get_photo()
img = cv2.flip(img, 1)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
faces = self.haar_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
faceimg = None
if len(faces) == 1:
x, y, w, h = faces[0]
faceimg = gray[y:y + h, x:x + w]
img = img[y:y + h, x:x + w]
self.photoData.FaceImg = np.copy(cv2.resize(faceimg,(128,128)))
else:
img = self.photoData.NoFaceImg
img = cv2.resize(img, (200, 200))
img = np.copy(img)
convert_to_qt_format = QImage(img.data, img.shape[1], img.shape[0], QImage.Format_RGB888)
p = convert_to_qt_format
self.__setFaceImg__(p)
def __setFaceImg__(self, image):
self.faceImg.setPixmap(QPixmap.fromImage(image))
def CreateMenu(self):
self.mainMenu = self.menuBar()
self.fileMenu = self.mainMenu.addMenu('File')
def initUI(self):
self.resize(900, 600)
qtRectangle = self.frameGeometry()
self.CreateMenu()
centerpoint = QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerpoint)
QToolTip.setFont(QFont('SansSerif', 10))
self.videoFeed = QLabel(self)
self.videoFeed.move(0, 40)
self.videoFeed.resize(640, 480)
self.faceImg = QLabel(self)
self.faceImg.move(650, 40)
self.faceImg.resize(200, 200)
facebt = QPushButton("get image", self)
facebt.move(700, 500)
facebt.clicked.connect(self.setFaceImg)
th = VideoThread(self.photoData)
th.changePixmap.connect(self.setImage)
th.start()
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FaceApp()
sys.exit(app.exec_())
video.py
class SharedData:
def __init__(self):
self.hasphoto = False
self.photo = None
self.FaceImg = None
self.NoFaceImg = cv2.imread("data/NoFace.png")
def set_photo(self, photo):
self.hasphoto = True
self.photo = photo
def get_photo(self):
return self.photo
class VideoThread(QThread):
changePixmap = pyqtSignal(QImage)
def __init__(self, PhotoData, parent=None, camera=0):
QThread.__init__(self, parent=parent)
self.isRunning = True
self.PhotoData = PhotoData
self.cap = None
if self.cap is not None:
self.cap.release()
self.cap = cv2.VideoCapture(camera)
def run(self):
while self.isRunning:
ret, frame = self.cap.read()
if ret:
rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.PhotoData.set_photo(rgb_image)
p = QImage(rgb_image.data, rgb_image.shape[1], rgb_image.shape[0], QImage.Format_RGB888)
#p = convert_to_qt_format.scaled(640, 480, Qt.KeepAspectRatio)
self.changePixmap.emit(p)
def __del__(self):
self.cap.release()