我正在尝试显示从 opencv 到 pyqt5 界面的视频流,但我的代码不起作用
I am trying to display video stream from opencv to pyqt5 interface but my code is not working
我正在尝试显示从 opencv 到 pyqt5 接口的视频流。我从一个问题的答案中改编了代码 - %3A。但是当我 运行 我的代码在 pycharm 时,我得到这个错误:
Process finished with exit code -1073740791 (0xC0000409)
这是我的代码:
class Gests_Recognition(QtCore.QObject):
def __init__(self):
super(Gests_Recognition, self).__init__()
self.running = True
change_pixmap = pyqtSignal(QImage)
gest_map = {
0: "up",
1: "down",
2: "right",
3: "left",
4: "forward",
5: "back"
}
columns = ['x11', 'x21', 'x12', 'x22', 'x13', 'x23', 'x14', 'x24', 'x15', 'x25',
'x16', 'x26', 'x17', 'x27', 'x18', 'x28', 'x19', 'x29', 'x110', 'x210', 'x111',
'x211', 'x112', 'x212', 'x113', 'x213', '114', '214', '115', 'x215', 'x116',
'x216', 'x117', 'x217', 'x118', 'x218', 'x119', 'x219', 'x120', 'x220', 'x121',
'x221']
def mapper(self, val):
return Gests_Recognition.gest_map[val]
def run(self):
model = load_model("gestures_model.h5", compile=False)
drawingModule = mediapipe.solutions.drawing_utils
handsModule = mediapipe.solutions.hands
capture = cv2.VideoCapture(0)
with handsModule.Hands(static_image_mode=False, min_detection_confidence=0.7, min_tracking_confidence=0.7,
max_num_hands=1) as hands:
while self.running:
# frame == 480 640
ret, frame = capture.read()
if ret:
processed_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
height_frame, width_frame, channels_frame = frame.shape
bytes_line = channels_frame * width_frame
qt_frame = QImage(processed_frame.data, width_frame, height_frame, bytes_line, QImage.Format_RGB888)
qt_frame = qt_frame.scaled(640, 480, QtCore.Qt.KeepAspectRatio)
self.change_pixmap.emit(qt_frame)
results = hands.process(processed_frame)
cv2.imshow('Hands recognizer', frame)
k = cv2.waitKey(1)
# do something else.
cv2.destroyAllWindows()
capture.release()
class Ui_MainWindow(object):
@pyqtSlot(QImage)
def set_image(self, image):
self.label.setPixmap(QPixmap.fromImage(image))
def setupUi(self, MainWindow):
self.cam_btn = QtWidgets.QPushButton(self.centralwidget)
self.cam_btn.setGeometry(QtCore.QRect(280, 60, 160, 150))
self.cam_btn.setObjectName("cam_btn")
self.cam_btn.setCheckable(True)
self.cam_btn.clicked.connect(self.gest_recognition)
self.cam_btn.setStyleSheet("QPushButton{background-color: #aae053;\n"
"border-radius: 60%;\nbackground-image: url('images/hand.png');\n"
"background-repeat: no-repeat;\nbackground-position: center;}\n"
"QPushButton:hover{background-color: #81eb3b;}")
self.label = QLabel(self.centralwidget)
self.label.move(40, 240)
self.label.resize(640, 480)
self.label.setStyleSheet("border: 5px solid black;")
def gest_recognition(self):
if self.cam_btn.isChecked():
self.cam_btn.setStyleSheet("QPushButton{background-color: red;\n"
"border-radius: 60%;\nbackground-image: url('images/pause.png');\n"
"background-repeat: no-repeat;\nbackground-position: center;}\n")
self.thread_gests = QtCore.QThread()
self.g_recog = Gests_Recognition()
self.g_recog.moveToThread(self.thread_gests)
self.g_recog.change_pixmap.connect(self.set_image)
self.thread_gests.start()
else:
self.cam_btn.setStyleSheet("QPushButton{background-color: #aae053;\n"
"border-radius: 60%;\nbackground-image: url('images/hand.png');\n"
"background-repeat: no-repeat;\nbackground-position: center;}\n"
"QPushButton:hover{background-color: #81eb3b;}")
self.g_recog.running = False
self.thread_gests.terminate()
试试这个概念
创建一个 python 文件 main_window.py
把这个粘贴进去
import sys
# import some PyQt5 modules
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QImage
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QTimer
# import Opencv module
import cv2
from ui_main_window import *
class MainWindow(QWidget):
# class constructor
def __init__(self):
# call QWidget constructor
super().__init__()
self.ui = Ui_Form()
self.ui.setupUi(self)
# create a timer
self.timer = QTimer()
# set timer timeout callback function
self.timer.timeout.connect(self.viewCam)
# set control_bt callback clicked function
self.ui.control_bt.clicked.connect(self.controlTimer)
# view camera
def viewCam(self):
# read image in BGR format
ret, image = self.cap.read()
# convert image to RGB format
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# get image infos
height, width, channel = image.shape
step = channel * width
# create QImage from image
qImg = QImage(image.data, width, height, step, QImage.Format_RGB888)
# show image in img_label
self.ui.image_label.setPixmap(QPixmap.fromImage(qImg))
# start/stop timer
def controlTimer(self):
# if timer is stopped
if not self.timer.isActive():
# create video capture
self.cap = cv2.VideoCapture(0)
# start timer
self.timer.start(20)
# update control_bt text
self.ui.control_bt.setText("Stop")
# if timer is started
else:
# stop timer
self.timer.stop()
# release video capture
self.cap.release()
# update control_bt text
self.ui.control_bt.setText("Start")
if __name__ == '__main__':
app = QApplication(sys.argv)
# create and show mainWindow
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
创建一个新文件并将其命名为 ui_main_window.py 并粘贴下面的代码
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(525, 386)
self.horizontalLayout = QtWidgets.QHBoxLayout(Form)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.image_label = QtWidgets.QLabel(Form)
self.image_label.setObjectName("image_label")
self.verticalLayout.addWidget(self.image_label)
self.control_bt = QtWidgets.QPushButton(Form)
self.control_bt.setObjectName("control_bt")
self.verticalLayout.addWidget(self.control_bt)
self.horizontalLayout.addLayout(self.verticalLayout)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Cam view"))
self.image_label.setText(_translate("Form", "TextLabel"))
self.control_bt.setText(_translate("Form", "Start"))
我正在尝试显示从 opencv 到 pyqt5 接口的视频流。我从一个问题的答案中改编了代码 -
Process finished with exit code -1073740791 (0xC0000409)
这是我的代码:
class Gests_Recognition(QtCore.QObject):
def __init__(self):
super(Gests_Recognition, self).__init__()
self.running = True
change_pixmap = pyqtSignal(QImage)
gest_map = {
0: "up",
1: "down",
2: "right",
3: "left",
4: "forward",
5: "back"
}
columns = ['x11', 'x21', 'x12', 'x22', 'x13', 'x23', 'x14', 'x24', 'x15', 'x25',
'x16', 'x26', 'x17', 'x27', 'x18', 'x28', 'x19', 'x29', 'x110', 'x210', 'x111',
'x211', 'x112', 'x212', 'x113', 'x213', '114', '214', '115', 'x215', 'x116',
'x216', 'x117', 'x217', 'x118', 'x218', 'x119', 'x219', 'x120', 'x220', 'x121',
'x221']
def mapper(self, val):
return Gests_Recognition.gest_map[val]
def run(self):
model = load_model("gestures_model.h5", compile=False)
drawingModule = mediapipe.solutions.drawing_utils
handsModule = mediapipe.solutions.hands
capture = cv2.VideoCapture(0)
with handsModule.Hands(static_image_mode=False, min_detection_confidence=0.7, min_tracking_confidence=0.7,
max_num_hands=1) as hands:
while self.running:
# frame == 480 640
ret, frame = capture.read()
if ret:
processed_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
height_frame, width_frame, channels_frame = frame.shape
bytes_line = channels_frame * width_frame
qt_frame = QImage(processed_frame.data, width_frame, height_frame, bytes_line, QImage.Format_RGB888)
qt_frame = qt_frame.scaled(640, 480, QtCore.Qt.KeepAspectRatio)
self.change_pixmap.emit(qt_frame)
results = hands.process(processed_frame)
cv2.imshow('Hands recognizer', frame)
k = cv2.waitKey(1)
# do something else.
cv2.destroyAllWindows()
capture.release()
class Ui_MainWindow(object):
@pyqtSlot(QImage)
def set_image(self, image):
self.label.setPixmap(QPixmap.fromImage(image))
def setupUi(self, MainWindow):
self.cam_btn = QtWidgets.QPushButton(self.centralwidget)
self.cam_btn.setGeometry(QtCore.QRect(280, 60, 160, 150))
self.cam_btn.setObjectName("cam_btn")
self.cam_btn.setCheckable(True)
self.cam_btn.clicked.connect(self.gest_recognition)
self.cam_btn.setStyleSheet("QPushButton{background-color: #aae053;\n"
"border-radius: 60%;\nbackground-image: url('images/hand.png');\n"
"background-repeat: no-repeat;\nbackground-position: center;}\n"
"QPushButton:hover{background-color: #81eb3b;}")
self.label = QLabel(self.centralwidget)
self.label.move(40, 240)
self.label.resize(640, 480)
self.label.setStyleSheet("border: 5px solid black;")
def gest_recognition(self):
if self.cam_btn.isChecked():
self.cam_btn.setStyleSheet("QPushButton{background-color: red;\n"
"border-radius: 60%;\nbackground-image: url('images/pause.png');\n"
"background-repeat: no-repeat;\nbackground-position: center;}\n")
self.thread_gests = QtCore.QThread()
self.g_recog = Gests_Recognition()
self.g_recog.moveToThread(self.thread_gests)
self.g_recog.change_pixmap.connect(self.set_image)
self.thread_gests.start()
else:
self.cam_btn.setStyleSheet("QPushButton{background-color: #aae053;\n"
"border-radius: 60%;\nbackground-image: url('images/hand.png');\n"
"background-repeat: no-repeat;\nbackground-position: center;}\n"
"QPushButton:hover{background-color: #81eb3b;}")
self.g_recog.running = False
self.thread_gests.terminate()
试试这个概念 创建一个 python 文件 main_window.py 把这个粘贴进去
import sys
# import some PyQt5 modules
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QImage
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QTimer
# import Opencv module
import cv2
from ui_main_window import *
class MainWindow(QWidget):
# class constructor
def __init__(self):
# call QWidget constructor
super().__init__()
self.ui = Ui_Form()
self.ui.setupUi(self)
# create a timer
self.timer = QTimer()
# set timer timeout callback function
self.timer.timeout.connect(self.viewCam)
# set control_bt callback clicked function
self.ui.control_bt.clicked.connect(self.controlTimer)
# view camera
def viewCam(self):
# read image in BGR format
ret, image = self.cap.read()
# convert image to RGB format
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# get image infos
height, width, channel = image.shape
step = channel * width
# create QImage from image
qImg = QImage(image.data, width, height, step, QImage.Format_RGB888)
# show image in img_label
self.ui.image_label.setPixmap(QPixmap.fromImage(qImg))
# start/stop timer
def controlTimer(self):
# if timer is stopped
if not self.timer.isActive():
# create video capture
self.cap = cv2.VideoCapture(0)
# start timer
self.timer.start(20)
# update control_bt text
self.ui.control_bt.setText("Stop")
# if timer is started
else:
# stop timer
self.timer.stop()
# release video capture
self.cap.release()
# update control_bt text
self.ui.control_bt.setText("Start")
if __name__ == '__main__':
app = QApplication(sys.argv)
# create and show mainWindow
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
创建一个新文件并将其命名为 ui_main_window.py 并粘贴下面的代码
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(525, 386)
self.horizontalLayout = QtWidgets.QHBoxLayout(Form)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.image_label = QtWidgets.QLabel(Form)
self.image_label.setObjectName("image_label")
self.verticalLayout.addWidget(self.image_label)
self.control_bt = QtWidgets.QPushButton(Form)
self.control_bt.setObjectName("control_bt")
self.verticalLayout.addWidget(self.control_bt)
self.horizontalLayout.addLayout(self.verticalLayout)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Cam view"))
self.image_label.setText(_translate("Form", "TextLabel"))
self.control_bt.setText(_translate("Form", "Start"))