Get Hikvision camera video with python

To call Hikvision camera video, you need to use the API provided by Hikvision SDK. The following is a simple sample code that can connect to Hikvision cameras and get video streams:

```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):
        # login camera
        init_res = NET_DVR_Init()
        if init_res:
            print('SDK initialization succeeded')
        else:
            print('SDK initialization failed')
            sys.exit()

        user_id = NET_DVR_Login_V30('192.168.1.64', 8000, 'admin', '12345', NET_DVR_DEVICEINFO_V30())
        if user_id < 0:
            print('login failed')
            sys.exit()
        else:
            print('login successful')

        # 开始预览
        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_())
```

It should be noted that `HCNetSDK` in the above code is the Python package of Hikvision SDK, which needs to be installed first. It can be installed with the following command:

```bash
pip install hcnetsdk
```

In addition, it is also necessary to enable the SDK access function in the management page of the Hikvision camera, and configure the IP address, port number, user name and password and other information.

Guess you like

Origin blog.csdn.net/ducanwang/article/details/131402299