-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcv_qt.py
47 lines (42 loc) · 1.52 KB
/
cv_qt.py
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
import sys
import cv2
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QTimer
class VideoWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.label = QLabel(self)
layout = QVBoxLayout()
layout.addWidget(self.label)
self.setLayout(layout)
self.cap = cv2.VideoCapture(0) # Open the default camera
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_frame)
self.timer.start(1000 // 30) # 30 FPS
def update_frame(self):
ret, frame = self.cap.read()
if ret:
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = frame_rgb.shape
bytes_per_line = ch * w
q_img = QImage(frame_rgb.data, w, h, bytes_per_line, QImage.Format_RGB888)
pixmap = QPixmap.fromImage(q_img)
self.label.setPixmap(pixmap)
def closeEvent(self, event):
self.cap.release()
event.accept()
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.video_widget = VideoWidget()
layout = QVBoxLayout()
layout.addWidget(self.video_widget)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.setWindowTitle('OpenCV and Qt Integration')
main_window.setGeometry(100, 100, 640, 480)
main_window.show()
sys.exit(app.exec_())