hikvision-client

MissiaLMIT更新放缓

用 Python 封装海康威视设备的接口客户端,方便调用摄像头、NVR 等设备的 API,省去自己处理鉴权和请求细节。

Client for Hikvision devices

0.3.2发布于 2022-01-31 08:38 UTC262star49fork同步于 2026-11-07 07:32 UTC

怎么上手

由 README 提炼,执行前请确认
pip install hikvisionapi
硬件要求按 README 说明

README 未说明,仅需能访问海康设备 HTTP 接口的网络环境

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

海康威视设备的 Python 客户端库,用链式属性调用封装 ISAPI 接口

用 Python 封装海康威视设备的接口客户端,方便调用摄像头、NVR 等设备的 API,省去自己处理鉴权和请求细节。

核心能力

  • 按 cam.System.deviceInfo 链式属性映射 ISAPI 路径
  • 响应可选 dict 解析或原始 XML 文本
  • 支持 PUT 回写 XML 修改设备配置
  • 通过 alertStream 拉取移动侦测等事件流
  • 提供 AsyncClient 异步版本与图片流式下载

容易踩的坑

包名是 hikvisionapi 而仓库名是 hikvision-client,别装错;事件流需调大 timeout 或设为 None,否则容易收不到事件;示例代码为 Python 2 语法,在 Python 3 下需自行调整

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

1 个版本0.3.2
  1. 0.3.2Add async implementation最新正式版2022-01-31 · 2022-01-31 08:38 UTC

    What's Changed

    New Contributors

    Full Changelog: https://github.com/MissiaL/hikvision-client/compare/0.2.1...0.3.2

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    CI

    Python Library for Hikvision Cameras

    Simple and easy to use library for working with video equipment from Hikvision.

    Install

    pip install hikvisionapi
    

    Examples

    There are two formats for receiving a response:

    from hikvisionapi import Client
    
    cam = Client('http://192.168.0.2', 'admin', 'admin')
    
    
    # Dict response (default)
    response = cam.System.deviceInfo(method='get')
    
    response == {
        u'DeviceInfo': {
            u'@version': u'2.0',
            '...':'...'
            }
        }
    
    
    # xml text response
    response = cam.System.deviceInfo(method='get', present='text')
    
    response == '''<?xml version="1.0" encoding="UTF-8" ?>
            <DeviceInfo version="1.0" xmlns="http://www.hikvision.com/ver20/XMLSchema">
            <deviceName>HIKVISION</deviceName>
            </DeviceInfo>'''
    

    Hints:

    # to get the channel info
    motion_detection_info = cam.System.Video.inputs.channels[1].motionDetection(method='get')
    
    
    # to send data to device:
    xml = cam.System.deviceInfo(method='get', present='text')
    cam.System.deviceInfo(method='put', data=xml)
    
    
    # to get events (motion, etc..)
    # Increase timeout if you want to wait for the event to be received
    cam = Client('http://192.168.0.2', 'admin', 'Password', timeout=30)
    cam.count_events = 2 # The number of events we want to retrieve (default = 1)
    response = cam.Event.notification.alertStream(method='get', type='stream')
    
    response == [{
        u'EventNotificationAlert':{
            u'@version': u'2.0',
            u'@xmlns': u'http://www.hikvision.com/ver20/XMLSchema',
            u'activePostCount': u'0',
            u'channelID': u'1',
            u'dateTime': u'2018-03-21T15:49:02+08:00',
            u'eventDescription': u'videoloss alarm',
            u'eventState': u'inactive',
            u'eventType': u'videoloss'
            }
       }]
    
    # Alternative solution to get events
    cam = Client('http://192.168.0.2', 'admin', 'Password', timeout=1)
    while True:
        try:
            response = cam.Event.notification.alertStream(method='get', type='stream')
            if response:
                print response
        except Exception:
            pass
    
    # to get opaque data type and write to file
    response = cam.System.configurationData(method='get', type='opaque_data')
    with open('my_file', 'wb') as f:
        for chunk in response.iter_content(chunk_size=1024): 
            if chunk:
                f.write(chunk)
                
    # Get and save picture from camera            
    response = client.Streaming.channels[102].picture(method='get', type='opaque_data')
    with open('screen.jpg', 'wb') as f:
        for chunk in response.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)                
    

    Examples (Async)

    from hikvisionapi import AsyncClient
    
    cam = AsyncClient('http://192.168.0.2', 'admin', 'admin')
    
    
    # Dict response (default)
    response = await cam.System.deviceInfo(method='get')
    
    response == {
        u'DeviceInfo': {
            u'@version': u'2.0',
            '...':'...'
            }
        }
    
    
    # xml text response
    response = await cam.System.deviceInfo(method='get', present='text')
    
    response == '''<?xml version="1.0" encoding="UTF-8" ?>
            <DeviceInfo version="1.0" xmlns="http://www.hikvision.com/ver20/XMLSchema">
            <deviceName>HIKVISION</deviceName>
            </DeviceInfo>'''
    
    # to send data to device:
    xml = cam.System.deviceInfo(method='get', present='text')
    await cam.System.deviceInfo(method='put', data=xml)
    
    
    # to get events (motion, etc..)
    # Increase timeout if you want to wait for the event to be received (None for infinite)
    async for event in cam.Event.notification.alertStream(method='get', type='stream', timeout=None):
        event == {
            u'EventNotificationAlert':{
                u'@version': u'2.0',
                u'@xmlns': u'http://www.hikvision.com/ver20/XMLSchema',
                u'activePostCount': u'0',
                u'channelID': u'1',
                u'dateTime': u'2018-03-21T15:49:02+08:00',
                u'eventDescription': u'videoloss alarm',
                u'eventState': u'inactive',
                u'eventType': u'videoloss'
                }
        }
    
    # Get and save picture from camera
    with open('screen.jpg', 'wb') as f:
        async for chunk in cam.Streaming.channels[102].picture(method='get', type='opaque_data'):
            if chunk:
                f.write(chunk)
    

    How to run the tests

    python -m pip install -r requirements.txt -r requirements-dev.txt
    pre-commit run --all-files
    pytest --cov-report html --cov hikvisionapi