我想点击一个按钮开始捕捉帧,然后点击另一个按钮释放网络摄像头,让我重做这个过程。
但是当我在下面运行此代码并单击按钮时会出现问题:
import cv2
...
self.StartVideo.clicked.connect(self.startVideo)
...
def startVideo(self):
self.StartVideo.setDisabled(True)
self.cap = cv2.VideoCapture(0)
self.cap.release()
系统崩溃并说Process完成退出代码139(被信号11中断:SIGSEGV)
然后我试着这样:
import cv2
...
self.StartVideo.clicked.connect(self.startVideo)
self.StartVideo.clicked.connect(self.closeVideo)
...
def startVideo(self):
from Video_Analysis.video_threads import video_thread
self.videoThread = video_thread()
self.videoThread.start()
def closeVideo(self):
self.videoThread.stop()
Video_Analysis.video_threads.py是:
# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets, QtGui, QtCore
import threading
import numpy as np
from time import sleep
import cv2
class video_thread(QtCore.QThread):
# finishSignal_frame = QtCore.pyqtSignal(list)
# finishSignal_mark = QtCore.pyqtSignal(np.ndarray)
def __init__(self, parent=None):
super(video_thread, self).__init__(parent)
self.cap = cv2.VideoCapture(0)
def run(self):
self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 320)
self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 240)
self.cap.set(cv2.cv.CV_CAP_PROP_FPS, 30)
while True:
ret, frame = self.cap.read()
rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
convertToQtFormat = QtGui.QImage(rgbImage.data, rgbImage.shape[1], rgbImage.shape[0],
QtGui.QImage.Format_RGB888)
convertToQtFormat = QtGui.QPixmap.fromImage(convertToQtFormat)
pixmap = QtGui.QPixmap(convertToQtFormat)
def stop(self):
self.cap.release()
self.terminate()
但问题是一样的,我怎么能这样呢?
非常感谢!