OpenCV从入门到精通实战(六)——多目标追踪

news/2024/5/2 20:55:28

基于原生的追踪

使用OpenCV库实现基于视频的对象追踪。通过以下步骤和Python代码,您将能够选择不同的追踪器,并对视频中的对象进行实时追踪。

步骤 1: 导入必要的库

首先,我们需要导入一些必要的Python库,包括argparsetimecv2 (OpenCV) 和 numpy

import argparse
import time
import cv2
import numpy as np

步骤 2: 设置参数解析

使用argparse库来解析命令行参数。我们将指定输入视频文件的路径以及选择的追踪器类型。

ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", type=str, help="path to input video file")
ap.add_argument("-t", "--tracker", type=str, default="kcf", help="OpenCV object tracker type")
args = vars(ap.parse_args())

步骤 3: 定义支持的追踪器

在OpenCV中,有多种对象追踪器可用。我们将它们存储在一个字典中,便于后续使用。

OPENCV_OBJECT_TRACKERS = {"csrt": cv2.TrackerCSRT_create,"kcf": cv2.TrackerKCF_create,"boosting": cv2.TrackerBoosting_create,"mil": cv2.TrackerMIL_create,"tld": cv2.TrackerTLD_create,"medianflow": cv2.TrackerMedianFlow_create,"mosse": cv2.TrackerMOSSE_create
}

步骤 4: 初始化追踪器和视频流

我们初始化一个多对象追踪器并打开视频文件。

trackers = cv2.MultiTracker_create()
vs = cv2.VideoCapture(args["video"])

步骤 5: 处理视频帧

接下来,我们读取视频中的每一帧,并对其进行缩放处理,然后使用追踪器更新追踪状态,并绘制追踪的边框。

while True:frame = vs.read()frame = frame[1]if frame is None:break(h, w) = frame.shape[:2]width = 600r = width / float(w)dim = (width, int(h * r))frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)(success, boxes) = trackers.update(frame)for box in boxes:(x, y, w, h) = [int(v) for v in box]cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)cv2.imshow("Frame", frame)key = cv2.waitKey(100) & 0xFFif key == ord("s"):box = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True)tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()trackers.add(tracker, frame, box)elif key == 27:break
vs.release()
cv2.destroyAllWindows()

总结

import argparse
import time
import cv2
import numpy as np# 配置参数
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", type=str,help="path to input video file")
ap.add_argument("-t", "--tracker", type=str, default="kcf",help="OpenCV object tracker type")
args = vars(ap.parse_args())# opencv已经实现了的追踪算法OPENCV_OBJECT_TRACKERS = {"csrt": cv2.TrackerCSRT_create,"kcf": cv2.TrackerKCF_create,"boosting": cv2.TrackerBoosting_create,"mil": cv2.TrackerMIL_create,"tld": cv2.TrackerTLD_create,"medianflow": cv2.TrackerMedianFlow_create,"mosse": cv2.TrackerMOSSE_create
}# 实例化OpenCV's multi-object tracker
trackers = cv2.MultiTracker_create()
vs = cv2.VideoCapture(args["video"])# 视频流
while True:# 取当前帧frame = vs.read()# (true, data)frame = frame[1]# 到头了就结束if frame is None:break# resize每一帧(h, w) = frame.shape[:2]width=600r = width / float(w)dim = (width, int(h * r))frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)# 追踪结果(success, boxes) = trackers.update(frame)# 绘制区域for box in boxes:(x, y, w, h) = [int(v) for v in box]cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)# 显示cv2.imshow("Frame", frame)key = cv2.waitKey(100) & 0xFFif key == ord("s"):# 选择一个区域,按sbox = cv2.selectROI("Frame", frame, fromCenter=False,showCrosshair=True)# 创建一个新的追踪器tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()trackers.add(tracker, frame, box)# 退出elif key == 27:break
vs.release()
cv2.destroyAllWindows()

通过上述步骤和代码,可以实现一个简单的视频对象追踪应用,该应用支持多种追踪算法,并允许用户实时选择和追踪视频中的对象。这种技术在许多领域都有广泛的应用,包括安全监控、人机交互和自动驾驶车辆等。

检测模型的跟踪

检测模型 使用Python、OpenCV、dlib和多进程处理视频中的实时对象跟踪。以下是具体步骤及相关代码片段:

1. 设置和参数解析

  • 导入必要的库,并设置参数解析,处理输入如视频文件路径和模型配置。
from utils import FPS
import multiprocessing
import numpy as np
import argparse
import dlib
import cv2

2. 初始化深度学习模型

  • 加载预训练的Caffe模型进行对象检测。
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])

3. 视频流处理

  • 从指定的文件开始视频捕捉,并准备处理帧。
vs = cv2.VideoCapture(args["video"])

4. 帧处理

  • 调整帧大小并转换为RGB格式进行处理。
  • 如果检测到的对象置信度高于阈值,则初始化对象跟踪。
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

5. 对象检测和跟踪

  • 对初次检测到的对象创建跟踪器,并使用多进程处理。
p = multiprocessing.Process(target=start_tracker, args=(bb, label, rgb, iq, oq))
p.daemon = True
p.start()

6. 追踪器更新和结果输出

  • 每个跟踪器获取新的帧,更新位置并输出跟踪结果。
outputQueue.put((label, (startX, startY, endX, endY)))

7. 视频输出和显示

  • 如果指定了输出文件,将处理后的帧写入视频文件。
  • 显示处理后的帧并在用户按下ESC键时停止。
writer.write(frame)
key = cv2.waitKey(1) & 0xFF
if key == 27:break

8. 清理和资源释放

  • 停止FPS计时,释放视频文件和窗口资源。
fps.stop()
writer.release()
cv2.destroyAllWindows()
vs.release()

完整代码:
utils.py

import datetimeclass FPS:def __init__(self):# store the start time, end time, and total number of frames# that were examined between the start and end intervalsself._start = Noneself._end = Noneself._numFrames = 0def start(self):# start the timerself._start = datetime.datetime.now()return selfdef stop(self):# stop the timerself._end = datetime.datetime.now()def update(self):# increment the total number of frames examined during the# start and end intervalsself._numFrames += 1def elapsed(self):# return the total number of seconds between the start and# end intervalreturn (self._end - self._start).total_seconds()def fps(self):# compute the (approximate) frames per secondreturn self._numFrames / self.elapsed()

multi_object_tracking_fast.py

import datetimeclass FPS:def __init__(self):# store the start time, end time, and total number of frames# that were examined between the start and end intervalsself._start = Noneself._end = Noneself._numFrames = 0def start(self):# start the timerself._start = datetime.datetime.now()return selfdef stop(self):# stop the timerself._end = datetime.datetime.now()def update(self):# increment the total number of frames examined during the# start and end intervalsself._numFrames += 1def elapsed(self):# return the total number of seconds between the start and# end intervalreturn (self._end - self._start).total_seconds()def fps(self):# compute the (approximate) frames per secondreturn self._numFrames / self.elapsed()

multi_object_tracking_slow.py

#导入工具包
from utils import FPS
import numpy as np
import argparse
import dlib
import cv2
"""
--prototxt mobilenet_ssd/MobileNetSSD_deploy.prototxt 
--model mobilenet_ssd/MobileNetSSD_deploy.caffemodel 
--video race.mp4
"""
# 参数
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--prototxt", default="mobilenet_ssd/MobileNetSSD_deploy.prototxt",help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "--model", default="mobilenet_ssd/MobileNetSSD_deploy.caffemodel",help="path to Caffe pre-trained model")
ap.add_argument("-v", "--video",default="race.mp4",help="path to input video file")
ap.add_argument("-o", "--output", type=str,help="path to optional output video file")
ap.add_argument("-c", "--confidence", type=float, default=0.2,help="minimum probability to filter weak detections")
args = vars(ap.parse_args())# SSD标签
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat","bottle", "bus", "car", "cat", "chair", "cow", "diningtable","dog", "horse", "motorbike", "person", "pottedplant", "sheep","sofa", "train", "tvmonitor"]# 读取网络模型
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])# 初始化
print("[INFO] starting video stream...")
vs = cv2.VideoCapture(args["video"])
writer = None# 一会要追踪多个目标
trackers = []
labels = []# 计算FPS
fps = FPS().start()while True:# 读取一帧(grabbed, frame) = vs.read()# 是否是最后了if frame is None:break# 预处理操作(h, w) = frame.shape[:2]width=600r = width / float(w)dim = (width, int(h * r))frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)# 如果要将结果保存的话if args["output"] is not None and writer is None:fourcc = cv2.VideoWriter_fourcc(*"MJPG")writer = cv2.VideoWriter(args["output"], fourcc, 30,(frame.shape[1], frame.shape[0]), True)# 先检测 再追踪if len(trackers) == 0:# 获取blob数据(h, w) = frame.shape[:2]blob = cv2.dnn.blobFromImage(frame, 0.007843, (w, h), 127.5)# 得到检测结果net.setInput(blob)detections = net.forward()# 遍历得到的检测结果for i in np.arange(0, detections.shape[2]):# 能检测到多个结果,只保留概率高的confidence = detections[0, 0, i, 2]# 过滤if confidence > args["confidence"]:# extract the index of the class label from the# detections listidx = int(detections[0, 0, i, 1])label = CLASSES[idx]# 只保留人的if CLASSES[idx] != "person":continue# 得到BBOX#print (detections[0, 0, i, 3:7])box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])(startX, startY, endX, endY) = box.astype("int")# 使用dlib来进行目标追踪#http://dlib.net/python/index.html#dlib.correlation_trackert = dlib.correlation_tracker()rect = dlib.rectangle(int(startX), int(startY), int(endX), int(endY))t.start_track(rgb, rect)# 保存结果labels.append(label)trackers.append(t)# 绘图cv2.rectangle(frame, (startX, startY), (endX, endY),(0, 255, 0), 2)cv2.putText(frame, label, (startX, startY - 15),cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)# 如果已经有了框,就可以直接追踪了else:# 每一个追踪器都要进行更新for (t, l) in zip(trackers, labels):t.update(rgb)pos = t.get_position()# 得到位置startX = int(pos.left())startY = int(pos.top())endX = int(pos.right())endY = int(pos.bottom())# 画出来cv2.rectangle(frame, (startX, startY), (endX, endY),(0, 255, 0), 2)cv2.putText(frame, l, (startX, startY - 15),cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)# 也可以把结果保存下来if writer is not None:writer.write(frame)# 显示cv2.imshow("Frame", frame)key = cv2.waitKey(1) & 0xFF# 退出if key == 27:break# 计算FPSfps.update()fps.stop()
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))if writer is not None:writer.release()cv2.destroyAllWindows()
vs.release()

在这里插入图片描述
在这里插入图片描述

代码地址:多目标追踪


http://www.mrgr.cn/p/26702717

相关文章

redis常见的应用问题以及解决方案

redis中三种缓存问题以及解决方案缓存穿透 问题描述: key对应的数据在数据源并不存在,每次针对此key的请求从缓存获取不到,请求都会压到数据源(数据库),从而可能压垮数据源。 用一个不存在的用户id获取用户信息,不论缓存还是数据库都没有,若黑客利用此漏洞进行攻击可能…

产废端实时音视频监控系统在运输车辆驾驶室中的应用

实时音视频监控系统可通过在运输车辆驾驶室安装音视频摄录设备,实现将运输车辆内部及周围环境音视频数据通过移动网络实时回传指挥中心的功能。 前端摄录设备主要负责采集车内外的视音频信息,为了保障车辆及运输人员 的安全,应合理选择摄录设…

Linux程序调试优化(1)——内存占用详解及优化思路

文章目录 1.free查看总体的内存占用2./proc/$PID/status 查看某进程状态 linux开发最重要的两个参数,分别是内存以及CPU使用率,若内存出现严重不足,则在需要使用内存时,可能出现申请不到的情况,导致 OOM,L…

BOSHIDA DC电源模块的未来发展方向和创新应用领域

BOSHIDA DC电源模块的未来发展方向和创新应用领域 随着科技的快速发展,直流(DC)电源模块的应用领域也在不断扩大。从传统的电子产品到新兴的清洁能源领域,DC电源模块正发挥着越来越重要的作用。未来,DC电源模块将继续发展,并在更多领域创造创新应用。 一,DC电源模块在电…

React 简单登录平台Demo(1):Next.js配置

目录前言体验Next.js中文文档路径问题创建项目启动报错测试TailWindCSS 热重载如何添加路由第三方库引入tailwindcss配置相关链接布局新建布局布局也是嵌套影响的根节点layout配置路由跳转Link跳转Hook跳转简单的登录页面 前言 我之前的那个项目写到后面,发现还不如直接用Next…

torchEEG工具箱

文章信息: 题目:TorchEEGEMO:基于脑电图的情绪识别深度学习工具箱 期刊:Expert Systems with Applications 环境:pytorch 1.11.0 CUDA 11.3 摘要: ​ 一个python工具箱TorchEEG,将工作流程分为五个模块…

构建镜像优化经验

构建镜像已经简单到docker build 或 buildkit build了,但优化镜像成了下一个问题。Layer层数据是叠加的 容器镜像由多个Layer层数据组装而成,每一条命令结果都会以一个层存储并逐渐叠加。 以Python镜像安装诸多包为例。 # py-pip1.dockerfile FROM python # 安装每个包都以RU…

全氟烷氧基树脂(PFA)洗瓶的性能优势

洗瓶是化学实验室中用于装清洗溶液的一种容器,并配有发射细液流的装置。 #实验室#PFA#洗瓶 PFA洗瓶由特氟龙塑料制成,带有螺旋盖。特氟龙塑料被设计成柔韧的,因此可以用手挤压瓶子以产生压力,迫使瓶子中的液体流过塑料管并以单滴或窄流的形式流出到被清洁的表面上。 PFA洗瓶…

个人 Scrum Day 3

一、项目总结 昨天已完成的工作:首页的前端内容 今日计划完成工作:在点击具体校区界面后会进入该校区的所有食堂列表界面,以及列表界面调整。点击某一食堂窗口后进入该窗口评论界面 工作中遇到的困难:navigator的用法以及排布窗口评论界面 二、代码签入 https://github.com…

37. UE5 RPG创建自定义的Ability Task

在前面的文章中,我们实现了一个火球术的一些基本功能,火球术技能的释放,在技能释放后,播放释放动画,在动画播放到需要释放火球术的位置时,将触发动画通知,在动画通知中触发标签事件,…

RT-Thread时钟管理

操作系统需要通过时间来规范其任务,主要介绍时钟节拍和基于时钟节拍的定时器。 时钟节拍 任何操作系统都需要提供一个时钟节拍,以供系统处理所有和时间有关的事件,如线程的延时、线程的时间片轮转调度以及定时器超时等。 RT-Thread 中,时钟节拍的长度可以根据 RT_TICK_P…

搭建HBase2.x完全分布式集群(CentOS 9 + Hadoop3.x)

Apache HBase™是一个分布式、可扩展、大数据存储的Hadoop数据库。 当我们需要对大数据进行随机、实时的读/写访问时,可以使用HBase。这个项目的目标是在通用硬件集群上托管非常大的表——数十亿行X数百万列。Apache HBase是一个开源、分布式、版本化的非关系数据库…

虚拟机磁盘剩余空间不足

VMware 弹出提示: 对文件“E:\Virtual Machine\CentOS 7 1810 的克隆 (2)\CentOS 7 1810-cl1.vmdk”的操作失败。 如果该文件位于远程文件系统上,请确保网络连接以及该磁盘所在的服务器正常工作。如果该文件位于可移动介质中,请重新连接该介…

HarmonyOS NEXT应用开发之多层嵌套类对象监听

介绍 本示例介绍使用@Observed装饰器和@ObjectLink装饰器来实现多层嵌套类对象属性变化的监听。 效果图预览使用说明加载完成后显示商品列表,点击刷新按钮可以刷新商品图片和价格。实现思路创建FistGoodsModel类,类对象是用@Observed修饰的类SecondGoodsItemList,SecondGood…

基于信息安全的软测工具链解决方案

伴随着汽车与外界的交互手段不断丰富,车联网相关设备、系统间的数据交互更加频繁,万物互联下的网络攻击也逐渐渗透延伸到车联网的领域。汽车行业面临着重大的信息安全挑战。此外,UNECE WP.29 R155和ISO/SAE 21434标准也对汽车的信息安全提出了规范化的要求,旨在产品全生命周…

Linux 网络编程项目--简易ftp

主要代码 config.h #define LS 0 #define GET 1 #define PWD 2#define IFGO 3#define LCD 4 #define LLS 5 #define CD 6 #define PUT 7#define QUIT 8 #define DOFILE 9struct Msg {int type;char data[1024];char secondBuf[128]; }; 服务器: #i…

GIS融合之路(六)-Cesium的雨雪风雷电效果

终于来到系列第六篇了,也来到大家最喜闻乐见天气效果 系列传送门: 山海鲸可视化:GIS融合之路(一)技术选型CesiumJS/loaders.gl/iTowns? 山海鲸可视化:GIS融合之路(二)CesiumJS和ThreeJS深度缓冲区整合 山海鲸可视化:GIS融合之路(三)CesiumJS和ThreeJS相机同步 山海…

WPF项目使用日志

提问 WPF项目如何使用日志 回答引入nuget log4net加入配置特性[assembly: ThemeInfo( ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located //(used if a resource is not found in the page, // or application resource dictionarie…

编程入门(四)【计算机网络基础(由一根网线连接两个电脑开始)】

读者大大们好呀!!!☀️☀️☀️ 🔥 欢迎来到我的博客 👀期待大大的关注哦❗️❗️❗️ 🚀欢迎收看我的主页文章➡️寻至善的主页 文章目录 前言两个电脑如何互连呢?集线器、交换机与路由器总结 前言 当你有…