南山区做网站公司,工作室怎么网站备案,如何给自己网站做外链,郑州燚空间网络科技有限公司#1024程序员节 | 征文# 这两天才因为项目需要#xff0c;对网络摄像头的视频采集以及实现人形识别与跟踪技术。对于onvif协议自然起先也没有任何的了解。但是购买的摄像头是SONY网络头是用在其他地方的。因为前期支持探究项目解决方案#xff0c;就直接拿来做demo测试使用。 …#1024程序员节 | 征文# 这两天才因为项目需要对网络摄像头的视频采集以及实现人形识别与跟踪技术。对于onvif协议自然起先也没有任何的了解。但是购买的摄像头是SONY网络头是用在其他地方的。因为前期支持探究项目解决方案就直接拿来做demo测试使用。 先说说onvif协议
Onvif即Open Network Video Interface Forum 可以译为开放型网络视频接口论坛是安迅士、博世、索尼在2008年共同成立的一个国际性、开发型网络视频产品标准网络接口的开发论坛后来由于这个技术开发论坛共同制定的开发型行业标准就用该论坛的大写字母命名即ONVIF 网络视频标准规范习惯简称为ONVIF协议。
Onvif协议的出现解决了不同厂商之间开发的各类失败不能融合使用的难题提供了统一的网络视频开发标准即最终能够通过Onvif这个标准化的平台实现不同产品之间的集成。
测试
onvif Device test tool工具测试 python实现步骤 安装必要的库 使用 pip install onvif 安装 ONVIF 库。根据人形识别所用的库进行安装比如使用 pip install volo假设使用 volo 进行人形识别。 初始化 ONVIF 摄像头 设置摄像头的 IP 地址、端口、用户名和密码等参数。创建 ONVIFCamera 对象并更新其地址。获取 PTZPan/Tilt/Zoom服务用于控制摄像头的云台和变焦。 实现摄像头变焦控制 定义变焦速度等参数。通过调用 PTZ 服务的相关方法实现拉近zoom in、拉远zoom out和停止变焦操作。 人形识别与跟踪 使用选定的人形识别库加载模型并进行图像分析。在视频流中检测人形获取人形的位置信息。根据人形位置调整摄像头的云台和变焦实现跟踪。
完整代码
import cv2
import numpy as np
import serial
from onvif import ONVIFCamera
import time
import asyncio
from zeep import Client
import tkinter as tk
from threading import Thread
import logginglogging.basicConfig(levellogging.DEBUG)# 摄像头配置常量
CAMERA_IP 192.168.0.132
CAMERA_PORT 8080
CAMERA_USERNAME admin
CAMERA_PASSWORD admin123# 串口配置常量
SERIAL_PORT COM4
BAUDRATE 115200# 初始化串口
ser serial.Serial(SERIAL_PORT, BAUDRATE)# 初始化摄像头
camera None
try:camera ONVIFCamera(CAMERA_IP, CAMERA_PORT, CAMERA_USERNAME, CAMERA_PASSWORD)camera.update_xaddrs()except Exception as e:logging.error(f初始化摄像机错误: {e})
# 获取 PTZ 服务
def get_ptz_service(camera):try:if camera:return camera.create_ptz_service()else:print(摄像机未初始化.)return Noneexcept Exception as e:print(fPTZ 服务初始化错误: {e})return None
ptz_service get_ptz_service(camera)
# ONVIF摄像头设置
def get_stream_url():if camera:media_service camera.create_media_service()return media_service.GetStreamUri({StreamSetup: {Stream: RTP-Unicast,Transport: {Protocol: RTSP}},ProfileToken: camera.media.GetProfiles()[0].token}).Urielse:return None
async def process_video_async(cap):while True:ret, frame cap.read()if not ret:print(视频读取失败)breakresized_frame cv2.resize(frame, (899, 600))cv2.imshow(Video, resized_frame)if cv2.waitKey(1) 0xFF ord(q):breakawait asyncio.sleep(0)def start_video_processing():stream_url get_stream_url()if stream_url:cap cv2.VideoCapture(stream_url)cap.set(cv2.CAP_PROP_FPS, 30)asyncio.run(process_video_async(cap))cap.release()else:print(无法获取视频流 URL。)
# 摄像头控制
class CameraControl:def __init__(self, ptz_service, camera):self.ptz_service ptz_serviceself.zoom_speed 0.1self.camera_profile_token P2def zoom_in(self):try:if self.camera_profile_token and P2:self.ptz_service.ContinuousMove({ProfileToken: P2,Velocity: {PanTilt: {x: 0.0, y: 0.0},Zoom: {x: self.zoom_speed}}})print(拉近操作进行中)else:print(摄像头或 PTZ 服务未正确初始化。)except Exception as e:print(f拉近操作失败{e})def zoom_out(self):try:if self.camera_profile_token and self.ptz_service:self.ptz_service.ContinuousMove({ProfileToken: P2,Velocity: {PanTilt: {x: 0.0, y: 0.0},Zoom: {x: -self.zoom_speed} }})print(拉远操作进行中)else:print(摄像头或 PTZ 服务未正确初始化。)except Exception as e:print(f拉远操作失败{e})def stop_zoom(self):try:if self.camera_profile_token and self.ptz_service:# 尝试不同的停止方法self.ptz_service.Stop({ProfileToken: P2, PanTilt: True, Zoom: True})print(停止变焦操作)else:print(摄像头或 PTZ 服务未正确初始化。)except Exception as e:print(f停止变焦失败{e})# 创建 GUI
class CameraApp:def __init__(self, master, camera_control):self.master mastermaster.title(Camera Control)self.camera_control camera_controlself.zoom_in_button tk.Button(master, text拉近, commandself.camera_control.zoom_in)self.zoom_in_button.pack()self.zoom_out_button tk.Button(master, text拉远, commandself.camera_control.zoom_out)self.zoom_out_button.pack()self.stop_button tk.Button(master, text停止变焦, commandself.camera_control.stop_zoom)self.stop_button.pack()self.quit_button tk.Button(master, text退出, commandself.quit)self.quit_button.pack()def quit(self):self.camera_control.stop_zoom()if ser.is_open:ser.close()self.master.quit()
if __name__ __main__:# 启动视频处理线程video_thread Thread(targetstart_video_processing)video_thread.start()# 创建摄像头控制实例if camera and ptz_service:camera_control CameraControl(ptz_service, camera)else:print(无法创建摄像头控制实例摄像头或 PTZ 服务未正确初始化。)# 启动 GUIroot tk.Tk()if camera_control:app CameraApp(root, camera_control)else:print(无法启动 GUI摄像头控制实例未创建。)root.mainloop()