I need to display live camera video using opencv in pyqt5. I receive this video in a separate stream, which also does something else. I want that when the button is clicked, a function is launched that should do something and at that time a video from it is displayed. The second time you press the button, the video should disappear. How can I implement this? Here is my code:
class Gests_Recognition(QtCore.QObject):
def __init__(self):
super(Gests_Recognition, self).__init__()
self.running = True
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
results = hands.process(processed_frame)
cv2.imshow('Hands recognizer', frame)
k = cv2.waitKey(1)
# do something
cv2.destroyAllWindows()
capture.release()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
# 363x346px
MainWindow.setObjectName("Drone control")
MainWindow.setFixedSize(720, 500)
MainWindow.setWindowIcon(QIcon('images/drone.png'))
MainWindow.setStyleSheet("background-color: #212329; ")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
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)
def gest_recognition(self):
if self.cam_btn.isChecked():
self.thread_gests = QtCore.QThread()
self.g_recog = Gests_Recognition()
self.g_recog.moveToThread(self.thread_gests)
self.thread_gests.started.connect(self.g_recog.run)
self.thread_gests.start()
else:
self.g_recog.running = False
self.thread_gests.terminate()