-
Notifications
You must be signed in to change notification settings - Fork 11
/
ptz_control.py
148 lines (126 loc) · 6.28 KB
/
ptz_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
# @Time : 2021/4/20 14:57
# @Author : RichardoMu
# @File : ptz_control.py
# @Software: PyCharm
import sys
from onvif import ONVIFCamera
from time import sleep
IP = "192.168.66.64" # Camera IP address
PORT = 80 # Port
USER = "admin" # Username
PASS = "wst123456" # Password
class ptzControl(object):
def __init__(self):
super(ptzControl, self).__init__()
self.mycam = ONVIFCamera(IP,PORT,USER,PASS)
# create media service object
self.media = self.mycam.create_media_service()
# Get target profile
self.media_profile = self.media.GetProfiles()[0]
# Use the first profile and Profiles have at least one
token = self.media_profile.token
# PTZ controls -------------------------------------------------------------
self.ptz = self.mycam.create_ptz_service()
# Get available PTZ services
request = self.ptz.create_type('GetServiceCapabilities')
Service_Capabilities = self.ptz.GetServiceCapabilities(request)
# Get PTZ status
status = self.ptz.GetStatus({'ProfileToken': token})
# Get PTZ configuration options for getting option ranges
request = self.ptz.create_type('GetConfigurationOptions')
request.ConfigurationToken = self.media_profile.PTZConfiguration.token
ptz_configuration_options = self.ptz.GetConfigurationOptions(request)
# get continuousMove request -- requestc
self.requestc = self.ptz.create_type('ContinuousMove')
self.requestc.ProfileToken = self.media_profile.token
if self.requestc.Velocity is None:
self.requestc.Velocity = self.ptz.GetStatus({'ProfileToken': self.media_profile.token}).Position
self.requestc.Velocity.PanTilt.space = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].URI
self.requestc.Velocity.Zoom.space = ptz_configuration_options.Spaces.ContinuousZoomVelocitySpace[0].URI
# get absoluteMove request -- requesta
self.requesta = self.ptz.create_type('AbsoluteMove')
self.requesta.ProfileToken = self.media_profile.token
if self.requesta.Position is None:
self.requesta.Position = self.ptz.GetStatus(
{'ProfileToken': self.media_profile.token}).Position
if self.requesta.Speed is None:
self.requesta.Speed = self.ptz.GetStatus(
{'ProfileToken': self.media_profile.token}).Position
# get relativeMove request -- requestr
self.requestr = self.ptz.create_type('RelativeMove')
self.requestr.ProfileToken = self.media_profile.token
if self.requestr.Translation is None:
self.requestr.Translation = self.ptz.GetStatus(
{'ProfileToken': self.media_profile.token}).Position
self.requestr.Translation.PanTilt.space = ptz_configuration_options.Spaces.RelativePanTiltTranslationSpace[0].URI
self.requestr.Translation.Zoom.space = ptz_configuration_options.Spaces.RelativeZoomTranslationSpace[0].URI
if self.requestr.Speed is None:
self.requestr.Speed = self.ptz.GetStatus(
{'ProfileToken': self.media_profile.token}).Position
self.requests = self.ptz.create_type('Stop')
self.requests.ProfileToken = self.media_profile.token
self.requestp = self.ptz.create_type('SetPreset')
self.requestp.ProfileToken = self.media_profile.token
self.requestg = self.ptz.create_type('GotoPreset')
self.requestg.ProfileToken = self.media_profile.token
self.stop()
# Stop pan, tilt and zoom
def stop(self):
self.requests.PanTilt = True
self.requests.Zoom = True
print(f"self.request:{self.requests}")
self.ptz.Stop(self.requests)
# Continuous move functions
def perform_move(self, requestc):
# Start continuous move
ret = self.ptz.ContinuousMove(requestc)
def move_tilt(self, velocity):
self.requestc.Velocity.PanTilt.x = 0.0
self.requestc.Velocity.PanTilt.y = velocity
self.perform_move(self.requestc)
def move_pan(self, velocity):
self.requestc.Velocity.PanTilt.x = velocity
self.requestc.Velocity.PanTilt.y = 0.0
self.perform_move(self.requestc)
def move_continuous(self, pan, tilt):
self.requestc.Velocity.PanTilt.x = pan
self.requestc.Velocity.PanTilt.y = tilt
self.perform_move(self.requestc)
def zoom(self, velocity):
self.requestc.Velocity.Zoom.x = velocity
self.perform_move(self.requestc)
# Absolute move functions --NO ERRORS BUT CAMERA DOES NOT MOVE
def move_abspantilt(self, pan, tilt, velocity):
self.requesta.Position.PanTilt.x = pan
self.requesta.Position.PanTilt.y = tilt
self.requesta.Speed.PanTilt.x = velocity
self.requesta.Speed.PanTilt.y = velocity
ret = self.ptz.AbsoluteMove(self.requesta)
# Relative move functions --NO ERRORS BUT CAMERA DOES NOT MOVE
def move_relative(self, pan, tilt, velocity):
self.requestr.Translation.PanTilt.x = pan
self.requestr.Translation.PanTilt.y = tilt
self.requestr.Speed.PanTilt = [velocity,velocity]
# self.requestr.Speed.PanTilt.x = velocity
# self.requestr.Speed.PanTilt.y = velocity
self.requestr.Speed.Zoom = 0
ret = self.ptz.RelativeMove(self.requestr)
def zoom_relative(self, zoom, velocity):
self.requestr.Translation.PanTilt.x = 0
self.requestr.Translation.PanTilt.y = 0
self.requestr.Translation.Zoom.x = zoom
self.requestr.Speed.PanTilt.x = 0
self.requestr.Speed.PanTilt.y = 0
self.requestr.Speed.Zoom.x = velocity
ret = self.ptz.RelativeMove(self.requestr)
# Sets preset set, query and and go to
def set_preset(self, name):
self.requestp.PresetName = name
self.requestp.PresetToken = '1'
self.preset = self.ptz.SetPreset(self.requestp) # returns the PresetToken
def get_preset(self):
self.ptzPresetsList = self.ptz.GetPresets(self.requestc)
def goto_preset(self):
self.requestg.PresetToken = '1'
self.ptz.GotoPreset(self.requestg)