Hikvision Separate Day / Night Settings with Raspberry Pi

The Hikvision cameras I use for LPR don’t have the ability to manually control the gain of the sensor when the camera is set in “Auto” mode. “Auto” mode pushes the camera from night to day mode automagically by detecting the ambient light in the scene. This is normally fine, but for LPR the gain needs to be set very low to compensate for the bright reflections that come from license plates. Manually pushing the camera into night or day mode enables the control of gain, but making the camera switch automagically becomes impossible.

To get around this issue I use a Raspberry Pi to push configuration changes to the camera manually. It finds the local sunrise/sunset times based on latitude/longitude and uses those times to schedule the change. If you are using a NVR you need to enable remote camera management in the settings so you can reach each individual camera.

Place this first block of code into a file named ALPR.py

import requests
import json
import time
import subprocess
from dateutil import tz
from datetime import datetime

longitude = ''
latitude = ''
timezone = ''#America/New_York
ips = [] #['', '']
user = ''
password = ''
nightOrDayPath = '/home/pi/setNightDay.py'

r = requests.get('http://api.sunrise-sunset.org/json?lat=' + latitude + '&lng=' + longitude + '&date=today')
json = json.loads(r.content.decode('utf-8'))

from_zone = tz.gettz('UTC')
to_zone = tz.gettz(timezone)

utcSunset = datetime.strptime(time.strftime("%Y-%m-%d") + " " + json['results']['sunset'], '%Y-%m-%d %I:%M:%S %p')
utcSunset = utcSunset.replace(tzinfo=from_zone)
utcSunrise = datetime.strptime(time.strftime("%Y-%m-%d") + " " + json['results']['sunrise'], '%Y-%m-%d %I:%M:%S %p')
utcSunrise = utcSunrise.replace(tzinfo=from_zone)
sunset = utcSunset.astimezone(to_zone)
sunrise = utcSunrise.astimezone(to_zone)

print("sunrise is " + sunrise.strftime('%H:%M %p'))
print("sunset is " +  sunset.strftime('%H:%M %p'))

for ip in ips:
        dayCommand = 'python3 ' + nightOrDayPath + ' --ip ' + ip + ' --user ' + user + ' --password ' + password + ' --dayornight day'
        nightCommand = 'python3 ' + nightOrDayPath + ' --ip ' + ip + ' --user ' + user + ' --password ' + password + ' --dayornight night'

        p = subprocess.Popen('at ' + sunrise.strftime('%H:%M') , stdin=subprocess.PIPE, shell=True)
        p = subprocess.Popen('at ' + sunset.strftime('%H:%M'), stdin=subprocess.PIPE, shell=True)

print('tasks are scheduled')

Create a cron job that will execute scheduler.py after midnight each day.

1 0 * * * /usr/bin/python3 /home/pi/alpr.py

Put the second block in setNightDay.py

#!/usr/bin/env python
import requests
from requests.auth import HTTPBasicAuth
import sys
import argparse

parser = argparse.ArgumentParser(description='Set day and night status on Hikvision cameras.')
parser.add_argument('--ip', help='IP address and port to send command to')
parser.add_argument('--user', help='Hikvision username')
parser.add_argument('--password', help='Hikvision password')
parser.add_argument('--dayornight', help='night or day', choices=['night', 'day'])


xml = '<ImageChannel version="2.0" xmlns="http://www.hikvision.com/ver20/XMLSchema"><IrcutFilter version="2.0" xmlns="http://www.hikvision.com/ver20/XMLSchema"><IrcutFilterType>' + args.dayornight + '</IrcutFilterType></IrcutFilter></ImageChannel>'
requests.put('http://' + args.ip + '/ISAPI/Image/channels/1', data=xml, auth=HTTPBasicAuth(args.user, args.password));

2 thoughts on “Hikvision Separate Day / Night Settings with Raspberry Pi”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.