教育培训网站抄袭,个人网页网站制作模板,05网电子书,上海短视频推广公司文章目录 1、功能描述2、代码实现3、效果展示4、完整代码5、涉及到的库函数dlib.correlation_tracker() 6、参考 1、功能描述
基于 dlib 库#xff0c;实现指定类别的目标检测和单目标跟踪
2、代码实现
caffe 模型
https://github.com/MediosZ/MobileNet-SSD/tree/master/… 文章目录 1、功能描述2、代码实现3、效果展示4、完整代码5、涉及到的库函数dlib.correlation_tracker() 6、参考 1、功能描述
基于 dlib 库实现指定类别的目标检测和单目标跟踪
2、代码实现
caffe 模型
https://github.com/MediosZ/MobileNet-SSD/tree/master/mobilenet
或者
链接: https://pan.baidu.com/s/1fiBz6tEQmcXdw_dtaUuAVw?pwdpw5n 提取码: pw5n 输入 1x3x300x300
输出的类别数为 21 导入必要的包
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import dlib
import cv2注意 dlib 的安装
conda 或者 pip 安装如果 build 失败的话可以试试下载 whl 安装
https://github.com/Silufer/dlib-python/tree/main
python -V 查看 python 版本然后找到对应版本的 whl pip install xxx.whl 构造参数解析并解析参数
ap argparse.ArgumentParser()
ap.add_argument(-p, --prototxt, requiredTrue,helppath to Caffe deploy prototxt file)
ap.add_argument(-m, --model, requiredTrue,helppath to Caffe pre-trained model)
ap.add_argument(-v, --video, requiredTrue,helppath to input video file)
ap.add_argument(-l, --label, requiredTrue,helpclass label we are interested in detecting tracking)
ap.add_argument(-o, --output, typestr,helppath to optional output video file)
ap.add_argument(-c, --confidence, typefloat, default0.2,helpminimum probability to filter weak detections)
args vars(ap.parse_args())涉及到 caffe 模型的 prototxtcaffemodel输入视频类别标签输出视频检测框的置信度配置
moblienet 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])# 初始化视频流、dlib 相关跟踪器、输出视频写入器和预测的类标签
print([INFO] starting video stream...)
vs cv2.VideoCapture(args[video])
tracker None
writer None
label
# 启动每秒帧数估计器
fps FPS().start()循环读取视频帧
# 循环播放视频文件流中的帧
while True:# 从视频文件中获取下一帧(grabbed, frame) vs.read()# 检查我们是否已经到达视频文件的末尾if frame is None:break# 调整帧大小以加快处理速度然后将帧从 BGR 转换为 RGB 排序dlib 需要 RGB 排序frame imutils.resize(frame, width600)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)resize 图片至宽为 600转化为 RGB 输入模式设置输出视频相关配置 # 如果我们的相关对象跟踪器是None我们首先需要应用一个对象检测器来为跟踪器提供实际跟踪的东西if tracker is None:# 获得帧尺寸并将帧转换为 blob(h, w) frame.shape[:2]blob cv2.dnn.blobFromImage(frame, 0.007843, (w, h), 127.5)# blob传入网络并获得检测结果net.setInput(blob)detections net.forward()# 确保至少有一个检测结果if len(detections) 0:# 找到概率最大的检测索引——为方便起见我们只跟踪我们以最大概率找到的第一个对象# 未来的示例将演示如何检测和提取*特定*对象i np.argmax(detections[0, 0, :, 2])# 获取与对象关联的概率及其类标签conf detections[0, 0, i, 2]label CLASSES[int(detections[0, 0, i, 1])]# filter out weak detections by requiring a minimum# confidenceif conf args[confidence] and label args[label]:# compute the (x, y)-coordinates of the bounding box# for the objectbox detections[0, 0, i, 3:7] * np.array([w, h, w, h])(startX, startY, endX, endY) box.astype(int)# construct a dlib rectangle object from the bounding# box coordinates and then start the dlib correlation# trackertracker dlib.correlation_tracker()rect dlib.rectangle(startX, startY, endX, endY)tracker.start_track(rgb, rect)# draw the bounding box and text for the objectcv2.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)第一帧的时候调用目标检测模型获取检测结果 detections
如果检测到了目标预测的分数大于配置的阈值且预测的类别和配置的类别一致
初始化跟踪器 tracker可视化检测结果 否则我们已经执行了检测所以让我们跟踪对象 else:# 更新跟踪器并抓取被跟踪对象的位置tracker.update(rgb)pos tracker.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, label, (startX, startY - 15),cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)后续帧采用跟踪算法update 更新目标坐标后通过 get_position 获取新的坐标并可视化 # 检查我们是否应该将帧写入磁盘if writer is not None:writer.write(frame)# 显示输出帧cv2.imshow(Frame, frame)key cv2.waitKey(1) 0xFF# 如果按下了“q”键则退出循环if key ord(q):break# 更新FPS计数器fps.update()保存和可视化结果按 q 键退出视频流 # 我们的 fps 计数器停止并且 FPS 信息显示在终端中
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()
# 最后我们关闭所有 OpenCV 窗口并释放视频流
cv2.destroyAllWindows()
vs.release()完成信息统计释放资源
3、效果展示 train_result cat_result 4、完整代码
# 导入必要的包
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import dlib
import cv2# 构造参数解析并解析参数
ap argparse.ArgumentParser()
ap.add_argument(-p, --prototxt, requiredTrue,helppath to Caffe deploy prototxt file)
ap.add_argument(-m, --model, requiredTrue,helppath to Caffe pre-trained model)
ap.add_argument(-v, --video, requiredTrue,helppath to input video file)
ap.add_argument(-l, --label, requiredTrue,helpclass label we are interested in detecting tracking)
ap.add_argument(-o, --output, typestr,helppath to optional output video file)
ap.add_argument(-c, --confidence, typefloat, default0.2,helpminimum probability to filter weak detections)
args vars(ap.parse_args())# 初始化MobileNet 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])# 初始化视频流、dlib 相关跟踪器、输出视频写入器和预测的类标签
print([INFO] starting video stream...)
vs cv2.VideoCapture(args[video])
tracker None
writer None
label
# 启动每秒帧数估计器
fps FPS().start()# 循环播放视频文件流中的帧
while True:# 从视频文件中获取下一帧(grabbed, frame) vs.read()# 检查我们是否已经到达视频文件的末尾if frame is None:break# 调整帧大小以加快处理速度然后将帧从 BGR 转换为 RGB 排序dlib 需要 RGB 排序frame imutils.resize(frame, width600)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)# 如果我们的相关对象跟踪器是None我们首先需要应用一个对象检测器来为跟踪器提供实际跟踪的东西if tracker is None:# 获得帧尺寸并将帧转换为 blob(h, w) frame.shape[:2]blob cv2.dnn.blobFromImage(frame, 0.007843, (w, h), 127.5)# blob传入网络并获得检测结果net.setInput(blob)detections net.forward()# 确保至少有一个检测结果if len(detections) 0:# 找到概率最大的检测索引——为方便起见我们只跟踪我们以最大概率找到的第一个对象# 未来的示例将演示如何检测和提取*特定*对象i np.argmax(detections[0, 0, :, 2])# 获取与对象关联的概率及其类标签conf detections[0, 0, i, 2]label CLASSES[int(detections[0, 0, i, 1])]# filter out weak detections by requiring a minimum# confidenceif conf args[confidence] and label args[label]:# compute the (x, y)-coordinates of the bounding box# for the objectbox detections[0, 0, i, 3:7] * np.array([w, h, w, h])(startX, startY, endX, endY) box.astype(int)# construct a dlib rectangle object from the bounding# box coordinates and then start the dlib correlation# trackertracker dlib.correlation_tracker()rect dlib.rectangle(startX, startY, endX, endY)tracker.start_track(rgb, rect)# draw the bounding box and text for the objectcv2.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:# 更新跟踪器并抓取被跟踪对象的位置tracker.update(rgb)pos tracker.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, label, (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# 如果按下了“q”键则退出循环if key ord(q):break# 更新FPS计数器fps.update()# 我们的 fps 计数器停止并且 FPS 信息显示在终端中
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()
# 最后我们关闭所有 OpenCV 窗口并释放视频流
cv2.destroyAllWindows()
vs.release()测试脚本1
python .\track.py -p .\mobilenet_ssd\MobileNetSSD_deploy.prototxt -m .\mobilenet_ssd\MobileNetSSD_deploy.caffemodel -v .\cat.mp4 -l cat -o cat_result.mp4测试脚本2
python .\track.py -p .\mobilenet_ssd\MobileNetSSD_deploy.prototxt -m .\mobilenet_ssd\MobileNetSSD_deploy.caffemodel -v .\train.mp4 -l aeroplane -o train_result.mp45、涉及到的库函数
dlib.correlation_tracker()
dlib.correlation_tracker 是 Dlib 库中的一个功能用于实现目标跟踪Object Tracking。
dlib.correlation_tracker 基于判别式相关滤波器Discriminative Correlation Filter, DCF的方法这种方法通过训练一个滤波器来区分目标对象和背景从而实现高效的跟踪。
使用 dlib.correlation_tracker 跟踪目标通常涉及以下几个步骤
初始化跟踪器首先你需要创建一个 correlation_tracker 对象。这通常是在你已知目标对象在第一帧中的位置时进行的。设置目标区域你需要指定一个矩形区域通常通过左上角和右下角的坐标或者通过中心点和尺寸来标识目标对象在第一帧中的位置。更新跟踪器对于后续的视频帧你需要将新的帧传递给跟踪器并让它更新目标的位置。这个过程会不断重复直到视频结束或者跟踪失败。获取跟踪结果每次更新后你可以从跟踪器中获取当前帧中目标对象的位置。
以下是一个简单的示例展示了如何使用 dlib.correlation_tracker 进行目标跟踪
import dlib
import cv2# 加载视频
cap cv2.VideoCapture(video.mp4)# 读取第一帧
ret, frame cap.read()# 选择目标区域这里需要手动选择或者通过某种方法自动选择
rect dlib.rectangle(50, 50, 200, 200) # 示例矩形需要替换为实际的目标位置# 创建跟踪器
tracker dlib.correlation_tracker()
tracker.start_track(frame, rect)while cap.isOpened():ret, frame cap.read()if not ret:break# 更新跟踪器tracker.update(frame)# 获取跟踪结果rect tracker.get_position()# 在帧上绘制跟踪结果cv2.rectangle(frame, (rect.left(), rect.top()), (rect.right(), rect.bottom()), (0, 255, 0), 2)# 显示结果cv2.imshow(Tracking, frame)# 按下 q 键退出if cv2.waitKey(1) 0xFF ord(q):break# 释放资源
cap.release()
cv2.destroyAllWindows()注意事项
目标初始化目标在第一帧中的位置对于跟踪器的性能至关重要。如果初始化不准确跟踪可能会失败。视频质量视频的质量如分辨率、帧率、光照条件等也会影响跟踪器的性能。遮挡和快速移动当目标被遮挡或者快速移动时跟踪器可能会遇到困难。虽然 dlib.correlation_tracker 已经在很多场景下表现良好但在这些情况下可能需要更复杂的策略。
通过 dlib.correlation_tracker你可以实现高效且相对准确的目标跟踪适用于各种计算机视觉应用如视频监控、人机交互等。
6、参考
目标跟踪4使用dlib进行对象跟踪dlib–win系统所有版本文件下载地址whl文件