我开发了一个包含按钮的应用程序。单击按钮,它开始运行网络摄像头,以无限循环方式使用OpenCV捕获帧。当用户按“Q”时,程序将释放网络摄像头实例。当我关闭应用程序时,应用程序会成功关闭,但网络摄像头实例不会被释放并继续运行。如何确保在关闭应用程序时释放网络摄像头资源。你知道吗
你知道吗网络摄像头.py:-
import cv2
def hello():
camera = cv2.VideoCapture(0)
top, right, bottom, left = 10, 350, 225, 590
num_frames = 0
while True:
_, frame = camera.read()
clone = frame.copy()
roi = frame[top:bottom, right:left]
cv2.rectangle(clone, (left, top), (right, bottom), (0, 255, 0), 2)
num_frames += 1
cv2.imshow("Video Feed", clone)
keypress = cv2.waitKey(1) & 0xFF
if keypress == ord("q"):
break
camera.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
hello()
你知道吗应用程序.py:-
from PyQt5 import QtCore, QtGui, QtWidgets
from Webcam import hello
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.window = MainWindow
self.window.setObjectName("self.window")
self.window.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(self.window)
self.centralwidget.setObjectName("centralwidget")
self.startButton = QtWidgets.QPushButton(self.centralwidget)
self.startButton.setText("&Start")
self.startButton.clicked.connect(self.open_cam)
self.window.setCentralWidget(self.centralwidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def open_cam(self):
hello()
class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
def closeEvent(self, event):
result = QtWidgets.QMessageBox.question(self,
"Confirm Exit...",
"Are you sure you want to exit ?",
QtWidgets.QMessageBox.Yes| QtWidgets.QMessageBox.No)
event.ignore()
if result == QtWidgets.QMessageBox.Yes:
event.accept()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
可能是摄像头的问题。我将尝试根据这个示例https://stackoverflow.com/a/11449901/8150685更改您实现相机的方式。我还建议你把相机改成一个类,这样你就可以控制它的设置和消亡。你知道吗
注意:我没有使用Qt的计算机,因此未对其进行测试。我可能需要一些调试或它可能不工作。你知道吗
现在在Qt中,您可以创建一个摄影机对象。你知道吗
相关问题 更多 >
编程相关推荐