要调用海康摄像机视频,需要使用海康SDK提供的API。以下是一个简单的示例代码,可以连接到海康摄像机并获取视频流:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QTimer
from HCNetSDK import *
class Camera(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Camera')
self.setGeometry(100, 100, 640, 480)
self.label = QLabel(self)
self.label.setGeometry(0, 0, 640, 480)
self.timer = QTimer(self)
self.timer.timeout.connect(self.show_video)
self.timer.start(30)
self.login()
def login(self):
# 登录摄像机
init_res = NET_DVR_Init()
if init_res:
print('SDK初始化成功')
else:
print('SDK初始化失败')
sys.exit()
user_id = NET_DVR_Login_V30('192.168.1.64', 8000, 'admin', '12345', NET_DVR_DEVICEINFO_V30())
if user_id < 0:
print('登录失败')
sys.exit()
else:
print('登录成功')
# 开始预览
lpClientInfo = NET_DVR_CLIENTINFO()
lpClientInfo.lChannel = 1
lpClientInfo.hPlayWnd = 0
lpClientInfo.lLinkMode = 0
lpClientInfo.sMultiCastIP = ''
self.lRealPlayHandle = NET_DVR_RealPlay_V30(user_id, lpClientInfo, None, None, True)
def show_video(self):
# 获取视频流
ret, frame = NET_DVR_GetRealPlayerIndex(self.lRealPlayHandle)
if ret:
data = NET_DVR_GetRealPlayerIndex(self.lRealPlayHandle, frame)
pixmap = QPixmap.fromImage(QImage(data, 640, 480, QImage.Format_RGB888))
self.label.setPixmap(pixmap)
if __name__ == '__main__':
app = QApplication(sys.argv)
camera = Camera()
camera.show()
sys.exit(app.exec_())
```
需要注意的是,上述代码中的`HCNetSDK`是海康SDK的Python封装,需要先安装。可以通过以下命令安装:
```bash
pip install hcnetsdk
```
此外,还需要在海康摄像机的管理页面中开启SDK接入功能,并配置好IP地址、端口号、用户名和密码等信息。
来源地址:https://blog.csdn.net/ducanwang/article/details/131402299