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')
response = cam.System.deviceInfo(method='get')
response == {
u'DeviceInfo': {
u'@version': u'2.0',
'...':'...'
}
}
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:
motion_detection_info = cam.System.Video.inputs.channels[1].motionDetection(method='get')
xml = cam.System.deviceInfo(method='get', present='text')
cam.System.deviceInfo(method='put', data=xml)
cam = Client('http://192.168.0.2', 'admin', 'Password', timeout=30)
cam.count_events = 2
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'
}
}]
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
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)
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')
response = await cam.System.deviceInfo(method='get')
response == {
u'DeviceInfo': {
u'@version': u'2.0',
'...':'...'
}
}
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>'''
xml = cam.System.deviceInfo(method='get', present='text')
await cam.System.deviceInfo(method='put', data=xml)
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'
}
}
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
pipenv install --dev
pipenv run pytest
pipenv run pytest --cov-report html --cov hikvisionapi
pipenv shell
pytest