Skip to content

Instantly share code, notes, and snippets.

@stephenhouser
Last active August 10, 2023 18:04
Show Gist options
  • Save stephenhouser/c0ecaacedc14cbe62502ec123a812e12 to your computer and use it in GitHub Desktop.
Save stephenhouser/c0ecaacedc14cbe62502ec123a812e12 to your computer and use it in GitHub Desktop.
Controlling AXIS Pan/Tilt/Zoom Web Camera

AXIS-Camera Control

This small bit of Python code is to control an AXIS Pan/Tilt/Zoom web camera. The code here does not display the image from the camera, or have any feedback about the control. Those parts are left to the user. This is simply an example of how to send commands and get responses from the AXIS camera.

NOTE: Requres Python v3 and the requests module.

Pyhton sample code to control an AXIS Pan/Tilt/Zoom web camera.
#!/usr/bin/env python
import requests
import time
#function ptz_slider_onChange(group)
#{
# if ((group == "pan" && "abs" == "rel") ||
# (group == "tilt" && "abs" == "rel") ||
# (group == "zoom" && "abs" == "rel") ||
# (group == "focus" && "no" == "rel") ||
# (group == "brightness" && "" == "rel") ||
# (group == "iris" && "abs" == "rel")) {
# group = "r" + group;
# }
# if (theCameraNumber == "") theCameraNumber = "1";
# var url = "/axis-cgi/com/ptz.cgi?camera="+theCameraNumber+"&"+group+"="+parseFloat(Math.round(theNewSliderValue * 10)/10);
#}
#
# Looks like 'pan' is an absolute pan where 'rpan' is a relative pan.
#
#curl 'http://<<camera address>>/axis-cgi/com/ptz.cgi?camera=1&continuouspantiltmove=0,0&imagerotation=0&timestamp=1491243098477'
camera_n = 1
camera_url = 'http://<<camera address>>/axis-cgi/com/ptz.cgi'
camera_ir = 0
presets = {
'Home':
{
'pan' : -120.7629,
'tilt' : -4.8568,
'zoom' : 696.0,
'brightness' : 3333.0,
#'autofocus' : 'on',
#'autoiris' : 'on',
#'focus' : 6424.0, # generates error
#'iris' : 2739.0,
},
'Mt. Washington':
#value="tilt=154749:focus=32766.000000:pan=267468:iris=32766.000000:zoom=11111.000000"
{
'pan' : 156.7195,
'tilt' : -0.6732,
'zoom' : 11111.0,
#'autofocus' : 'on',
#'autoiris' : 'on',
#'focus' : 7964.0,
#'iris' : 2583.0,
}
}
def merge_dicts(*dict_args):
"""
Given any number of dicts, shallow copy and merge into a new dict,
precedence goes to key value pairs in latter dicts.
"""
result = {}
for dictionary in dict_args:
result.update(dictionary)
return result
def cameraCmd(q_cmd):
print("cameraCmd({})".format(q_cmd))
resp_data = {}
base_q_args = {
'camera': camera_n, 'imagerotation': camera_ir,
'html': 'no', 'timestamp': int(time.time())
}
q_args = merge_dicts(q_cmd, base_q_args)
resp = requests.get(camera_url, params=q_args)
if resp.text.startswith('Error'):
print(resp.text)
else:
for line in resp.text.splitlines():
(name, var) = line.split("=", 2)
try:
resp_data[name.strip()] = float(var)
except ValueError:
resp_data[name.strip()] = var
return resp_data
def cameraGet(query):
#print("cameraGet(" + query + ")")
return cameraCmd({ 'query': query })
# resp_data = {}
# q_args = { 'query': query,
# 'camera': camera_n, 'imagerotation': camera_ir,
# 'html': 'no', 'timestamp': int(time.time())
# }
# resp = requests.get(camera_url, params=q_args)
# for line in resp.text.splitlines():
# (name, var) = line.split("=", 2)
# try:
# resp_data[name.strip()] = float(var)
# except ValueError:
# resp_data[name.strip()] = var
#
# return resp_data
def cameraSet(group, val):
#print("cameraSet(" + group + ", " + str(val) + ")")
return cameraCmd({ group: val })
# resp_data = {}
# q_args = { group: val,
# 'camera': camera_n, 'imagerotation': camera_ir,
# 'html': 'no', 'timestamp': int(time.time())
# }
#
# resp = requests.get(camera_url, params=q_args)
# for line in resp.text.splitlines():
# (name, var) = line.split("=", 2)
# try:
# resp_data[name.strip()] = float(var)
# except ValueError:
# resp_data[name.strip()] = var
#
# return resp_data
def cameraGoToPreset(preset_name):
preset = presets[preset_name]
if preset != None:
for key, value in preset.items():
cameraSet(key, value)
def cameraHome():
return cameraCmd({'move': 'home'})
def cameraGetPTZ():
return cameraGet('position')
def cameraGetLimits():
return cameraGet('limits')
def cameraPan(value):
return cameraSet('pan', value)
def cameraTilt(value):
return cameraSet('tilt', value)
def cameraZoom(value):
return cameraSet('zoom', value)
def cameraPanRelative(value):
return cameraSet('rpan', value)
def cameraTiltRelative(value):
return cameraSet('rtilt', value)
def cameraZoomRelative(value):
return cameraSet('rzoom', value)
print("Move to home...")
print(cameraHome())
#time.sleep(1)
print("Get PTZ and Limits...")
print(cameraGetPTZ())
print(cameraGetLimits())
for i in range(5):
time.sleep(1)
print("Move left...")
print(cameraPanRelative(-1))
for i in range(5):
time.sleep(1)
print("Move right...")
print(cameraPanRelative(1))
#print(cameraTilt(-1))
#time.sleep(5)
#print("Show Mt. Washington...")
#print(cameraGoToPreset('Mt. Washington'))
#time.sleep(2)
#print("Move back home...")
#print(cameraHome())
#print(cameraGoToPreset('Home'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment