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 = [] #['192.168.1.2:65011', '192.168.1.2:65012']
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.communicate(str.encode(dayCommand))
        p = subprocess.Popen('at ' + sunset.strftime('%H:%M'), stdin=subprocess.PIPE, shell=True)
        p.communicate(str.encode(nightCommand))

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'])

args=parser.parse_args()

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));

Gardening Update – July

I learned how fast cucumbers can grow in one week:

I planted two different varieties, one of which is a pickling cucumber type.

And here they are pickled:

The first harvest of bell peppers went well. The first batch will be pickled or made into pepper jam.

I picked up some ball jars and canned everything. We need a new stove, this current electric one can barely get this amount of water up to a boil.

The tomatoes are all 5-6 ft tall now and are starting to turn red.

After a few weeks tomatoes were coming off the trellis faster than I could can them. I’ve harvested about ~60 pounds of tomatoes so far:

I haven’t finished the beds completely yet though 🙁

To Infinity, Recursively!