《ONVIF with python》

安装

onvif用python安装有源码安装和pip安装两种方式

  1. pip 安装
  • python 2.x
pip install onvif
  • python 3
pip install --upgrade onvif_zeep
  1. Github源码安装
  • python2.x

https://github.com/quatanium/python-onvif.git

cd python-onvif && python setup.py install
  • python 3

https://github.com/FalkTannhaeuser/python-onvif-zeep.git

cd python-onvif-zeep && python setup.py install

ONVIF控制

使用规则

  1. 创建对象:ptz, imaging, media
  2. 调用对象方法,详见wsdl文档
  3. 设置参数:
    • 字典形式,无需初始化
    • 属性形式,部分需要初始化
  4. 使用函数

示例

from onvif import ONVIFCamera

class myONVIF():
    def __init__(self, ip, usr, pwd):
        self.usr = usr
        self.pwd = pwd
        self.cam = ONVIFCamera(ip, 80, usr, pwd)
        self.ptz = self.cam.create_ptz_service()
        self.media = self.cam.create_media_service()
        self.imaging = self.cam.create_imaging_service()
        self.profile = self.cam.media.GetProfiles()[0]

    def get_ptz_status(self):
        # 获取PTZ状态
        request = self.cam.ptz.create_type('GetStatus')
        request.ProfileToken = self.profile.token
        status = self.cam.ptz.GetStatus(request)
        return {'pan': status.Position.PanTilt.x, 'tilt': status.Position.PanTilt.y, 'zoom': status.Position.Zoom.x}

    def set_home(self):
        # 设置归位点
        request = self.cam.ptz.create_type('SetHomePosition')  # 设置当前位置为原点位置
        request.ProfileToken = self.profile.token
        self.cam.ptz.SetHomePosition(request)

    def move_home(self):
        # 归位
        request = self.cam.ptz.create_type('GotoHomePosition')  # 回到原点位置
        request.ProfileToken = self.profile.token
        if request.Speed is None:  # 设置归位的速度
            request.Speed = self.cam.ptz.GetStatus({'ProfileToken': self.profile.token}).Position
        self.cam.ptz.GotoHomePosition(request)

    def abs_move(self, pan, tilt, zoom):
        # 绝对移动
        request = self.ptz.create_type('AbsoluteMove')
        request.ProfileToken = self.profile.token
        request.Position = {'PanTilt':{'x':pan, 'y':tilt}, 'Zoom':zoom}
        request.Speed = {'PanTilt':{'x':1, 'y':1}, 'Zoom':1}    # default speed
        self.ptz.AbsoluteMove(request)

    def continue_move(self, speed_pan, speed_tilt, speed_zoom):
        # 持续移动
        request = self.ptz.create_type("ContinuousMove")
        request.ProfileToken =  self.profile.token
        request.Velocity = {'PanTilt':{'x':speed_pan, 'y':speed_tilt}, 'Zoom':speed_zoom}
        self.ptz.ContinuousMove(request)

    def relative_move(self, pan, tilt, zoom):
        # 相对移动
        request = self.cam.ptz.create_type('RelativeMove')
        request.ProfileToken = self.profile.token
        request.Translation = {'PanTilt':{'x':pan, 'y':tilt}, 'Zoom':zoom}
        request.Speed = {'PanTilt':{'x':1, 'y':1}, 'Zoom':1}
        self.ptz.RelativeMove(request)

    def stop_move(self):
        # 停止
        self.ptz.Stop({'ProfileToken': self.profile.token})

    def snap_image(self, path):
        # 抓拍图像
        import requests, os
        res = self.media.GetSnapshotUri({'ProfileToken': self.profile.token})
        auth = requests.auth.HTTPDigestAuth(self.usr, self.pwd)
        response = requests.get(url=res.Uri, auth=auth)
        path_out = os.path.join(path, 'tmp.jpg')
        with open(path_out, 'wb') as fp:
            fp.write(response.content)

    def get_rtsp(self):
        # 获取rtsp
        obj = self.media.create_type('GetStreamUri')
        obj.StreamSetup = {'Stream': 'RTP-Unicast', 'Transport': {'Protocol': 'RTSP'}}
        obj.ProfileToken = self.profile.token
        res_uri = self.media.GetStreamUri(obj)['Uri']
        return res_uri

    def get_image_status(self):
        # 获取焦距不起作用???
        request = self.imaging.create_type('GetStatus')
        request.VideoSourceToken = self.profile.VideoSourceConfiguration.SourceToken
        return self.imaging.GetStatus(request)

    def abs_move_image(self, pos, speed=1):
        # 焦距绝对移动
        request = self.imaging.create_type('Move')
        request.VideoSourceToken = self.profile.VideoSourceConfiguration.SourceToken
        request.Focus = {'Absolute': {'Position': pos, 'Speed': speed}}
        self.imaging.Move(request)

    def continue_move_image(self, speed=1):
        # 焦距持续移动
        request = self.imaging.create_type('Move')
        request.VideoSourceToken = self.profile.VideoSourceConfiguration.SourceToken
        request.Focus = {'Continuous':{'Speed':0.5}}
        self.imaging.Move(request)

    def relative_move_image(self, dist, speed=1):
        # 焦距相对移动
        request = self.imaging.create_type('Move')
        request.VideoSourceToken = self.profile.VideoSourceConfiguration.SourceToken
        request.Focus = {'Relative': {'Distance':dist, 'Speed':speed}}
        self.imaging.Move(request)

    def stop_move_image(self):
        # 焦距停止移动
        self.imaging.Stop({'VideoSourceToken': self.profile.VideoSourceConfiguration.token})

if __name__ == "__main__":
    pass
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
ONVIF是一种广泛应用于监控领域的网络视频接口标准,它提高了视频监控的便利性和效率。使用Python来控制ONVIF的步骤可以总结为以下几个方面: 1. 首先,需要搭建Python的开发环境,确保安装了相关的Python库和依赖。 2. 接下来,可以使用PythonONVIF库来与IPC进行通信,通过调用ONVIF的Get和Set方法来获取和设置IPC的参数。这些方法可以用来获取视频流、控制摄像头的云台、调整图像设置等。 3. 在使用Python控制ONVIF的过程中,有一些技术优势。首先,Python是一种简单易学的编程语言,具有很好的可读性和易于调试的特点。其次,Python拥有丰富的第三方库和工具,可以提供更多的功能和便利的开发环境。最后,Python具有跨平台的特性,可以在不同的操作系统上运行。 综上所述,使用Python来控制ONVIF可以提供便利的开发环境和丰富的功能,使得对IPC的控制更加灵活和高效。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python 通过ONVIF控制IPC](https://blog.csdn.net/baidu_39053164/article/details/129008638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值