停止相机捕获线程,但无法释放

2024-10-02 12:22:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用qthread显示从usb摄像头捕获的图像

问题是在使用以下命令终止线程后出现的:

th.terminate()

摄像头指示灯仍亮,表示仍在使用(跳过以释放捕获)

尝试做:

cap = cv2.VideoCapture(0)
cap.release()

终止后,问题仍然存在

if(self.monitoringToggle == False):
    self.monitoringToggle = True
    self.monitoringButton.setText("Stop Monitoring")
    self.th.changePixmap.connect(self.setImage)
    self.th.start()

else:
    self.monitoringToggle = False
    self.monitoringButton.setText("Monitoring")
    self.th.terminate()
    cap = cv2.VideoCapture(0)
    cap.release()
    qImg = QImage("white_background.png")
    self.videoLabel.setPixmap(QtGui.QPixmap(qImg))

class MonitoringThread(QThread):
    def run(self):
        cap = cv2.VideoCapture(0)
        while(True):
            ret, frame = cap.read()
            key = cv2.waitKey(1) & 0xFF
            if key == ord("q"):
                break
        cap.release()

Tags: selffalsetruereleaseifcv2monitoring摄像头

热门问题