pyqt5 获取网络摄像头
时间: 2024-01-25 22:07:14 浏览: 113
在使用pyqt5获取网络摄像头时,可以使用OpenCV库来读取摄像头的帧,并将其绘制在pyqt控件上。以下是一个示例代码:
```python
import cv2
from PyQt5 import QtGui, QtCore
class MainWindow(QtWidgets.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.label_show_camera = QtWidgets.QLabel(self)
self.label_show_camera.setFixedSize(640, 480)
self.timer_camera = QtCore.QTimer()
self.timer_camera.timeout.connect(self.show_camera)
self.cap = cv2.VideoCapture("your_camera_url") # 用网络摄像头的URL替换"your_camera_url"
self.timer_camera.start(30)
def show_camera(self):
flag, self.image = self.cap.read()
show = cv2.resize(self.image, (640, 480))
show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)
self.label_show_camera.setPixmap(QtGui.QPixmap.fromImage(showImage))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
```
请注意,你需要将"your_camera_url"替换为实际的网络摄像头URL。此代码将创建一个界面,并将网络摄像头的视频流显示在名为`label_show_camera`的控件上。
阅读全文