35 lines
1.2 KiB
Python
35 lines
1.2 KiB
Python
import sys
|
|
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextBrowser, QWidget, QLabel, QPushButton, QFileDialog
|
|
from PyQt5.QtGui import QImage, QPixmap
|
|
from PyQt5.QtCore import QTimer, Qt
|
|
import cv2
|
|
|
|
class CameraWindow(QMainWindow):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.cap = cv2.VideoCapture(0) # 默认摄像头
|
|
self.timer = QTimer()
|
|
self.initUI()
|
|
self.timer.timeout.connect(self.update_frame)
|
|
|
|
def initUI(self):
|
|
self.setGeometry(100, 100, 640, 480)
|
|
self.setWindowTitle('Camera Feed')
|
|
self.show()
|
|
self.timer.start(20)
|
|
|
|
def update_frame(self):
|
|
ret, frame = self.cap.read()
|
|
if ret:
|
|
height, width, _ = frame.shape
|
|
bytes_per_line = 3 * width
|
|
cv2_img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
qt_img = QImage(cv2_img.data, width, height, bytes_per_line, QImage.Format_RGB888)
|
|
qt_img = qt_img.scaled(self.width(), self.height(), Qt.KeepAspectRatio)
|
|
self.setCentralWidget(QLabel(self))
|
|
self.centralWidget().setPixmap(QPixmap.fromImage(qt_img))
|
|
|
|
if __name__ == '__main__':
|
|
app = QApplication(sys.argv)
|
|
window = CameraWindow()
|
|
sys.exit(app.exec_()) |