Package: hikvisionapi
Python Library for Hikvision CamerasSimple and easy to use library for working with video equipment from Hikvision.InstallpipinstallhikvisionapiExamplesThere are two formats for receiving a response:fromhikvisionapiimportClientcam=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 responseresponse=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 infomotion_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 receivedcam=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 eventscam=Client('http://192.168.0.2','admin','Password',timeout=1)whileTrue:try:response=cam.Event.notification.alertStream(method='get',type='stream')ifresponse:printresponseexceptException:pass# to get opaque data type and write to fileresponse=cam.System.configurationData(method='get',type='opaque_data')withopen('my_file','wb')asf:forchunkinresponse.iter_content(chunk_size=1024):ifchunk:f.write(chunk)# Get and save picture from cameraresponse=client.Streaming.channels[102].picture(method='get',type='opaque_data')withopen('screen.jpg','wb')asf:forchunkinresponse.iter_content(chunk_size=1024):ifchunk:f.write(chunk)Examples (Async)fromhikvisionapiimportAsyncClientcam=AsyncClient('http://192.168.0.2','admin','admin')# Dict response (default)response=awaitcam.System.deviceInfo(method='get')response=={u'DeviceInfo':{u'@version':u'2.0','...':'...'}}# xml text responseresponse=awaitcam.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')awaitcam.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)asyncforeventincam.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 camerawithopen('screen.jpg','wb')asf:asyncforchunkincam.Streaming.channels[102].picture(method='get',type='opaque_data'):ifchunk:f.write(chunk)How to run the testspipenvinstall--dev
pipenvrunpytest
pipenvrunpytest--cov-reporthtml--covhikvisionapi# to get coverage report in ./htmlcov/# or you can get into the virtual env with:pipenvshell
pytest