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
pipenv install --dev
pipenv run pytest
pipenv run pytest --cov-report html --cov hikvisionapi # to get coverage report in ./htmlcov/
# or you can get into the virtual env with:
pipenv shell
pytest
Metadata
Release files for hikvisionapi 0.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| hikvisionapi-0.3.2.tar.gz | 6.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| hikvisionapi-0.3.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.6 kB
Release files / hikvisionapi-0.3.2.tar.gz
| Download URL | hikvisionapi-0.3.2.tar.gz |
|---|---|
| Size | 6.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7bdc14ecdbede027ff269dbf0f0c9e863f056f38759b16b3ec33c02e22904b30
|
|
BLAKE2b-256 checksum How to use checksums |
ba9c4118157078f4ba59e278eb7bd7f6a2bdf354d13f8c5ba453ec0734609624
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
|
Release files / hikvisionapi-0.3.2-py3-none-any.whl
| Download URL | hikvisionapi-0.3.2-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5389a46cbef9a5d23b2eb1784310da9a7a25073f6e9c139bd0397c4c4d04e760
|
|
BLAKE2b-256 checksum How to use checksums |
08a2b84a6b6c2c94a7f3a5ff7a90c5aac175ec04fcf714f44bab22cbbf8798c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
|