PyQT5 - récupérer le flux de la webcam dans un widget
Bonjour,
Je voudrais créer une interface PyQt, avec entre autre une fenêtre pour récupérer le flux de la webcam.
J'ai trouvé ce code qui fonctionne :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi'.format(timestamp),fourcc, 25.0, (640, 480))
while( cap.isOpened() ):
ret, frame = cap.read()
if ret == True:
frame = cv2.flip(frame,1)
out.write(frame)
cv2.imshow('frame' , frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows() |
Mais quand je cherche à l'intégrer dans mon interface :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| class Camera(QWidget):
def __init__(self):
timestamp = time.strftime("%Y%m%d%H%M%S")
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output_{}.avi'.format(timestamp),fourcc, 25.0, (640, 480))
while( cap.isOpened() ):
ret, frame = cap.read()
if ret == True:
frame = cv2.flip(frame,1)
out.write(frame)
cv2.imshow('frame' , frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
class MainWindow(QMainWindow):
# constructor
def __init__(self):
super().__init__()
# setting geometry
self.setGeometry(100, 100,
800, 600)
# setting style sheet
self.setStyleSheet("background : lightgrey;")
toolbar = QToolBar("Camera Tool Bar")
self.addToolBar(toolbar)
click_action = QAction("Click photo", self)
click_action.setStatusTip("This will capture picture")
click_action.setToolTip("Capture picture")
click_action.triggered.connect(self.click_photo)
toolbar.addAction(click_action)
self.main_widget = QWidget()
self.setCentralWidget(self.main_widget)
self.layout = QVBoxLayout(self.main_widget)
self.layout.addWidget(Camera())
self.layout.addItem(QSpacerItem(20, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding))
self.status = QStatusBar()
self.status.setStyleSheet("background : white;")
self.setStatusBar(self.status)
self.setWindowTitle("PyQt5 Cam")
self.show() |
L'image de la webcam prend toute la place de la fenêtre.
Ce que je voudrais c'est pouvoir 'isoler' l'image de la webcam dans un widget, pour que dans mon interface, je puisse mettre d'autres widgets autour (boutons, ...)
Merci,
Nico