COVID 社交距离检测(covid-social-distancing-detection)
- 一、项目概述
- 二、项目架构
- 三、环境搭建
- 四、运行项目
- 五、输出结果
- 六、常见问题及解决方法
- 报错1. cv2.error: OpenCV(4.11.0) :-1: error: (-5:Bad argument) in function 'circle'
- 报错2 cv2.circle(bird_view_img, (pair[0][0],pair[0][1]), BIG_CIRCLE, COLOR_RED, 2)
- 问题 3:缺少 `requirements.txt` 文件
- 问题 4:TensorFlow 模型下载失败
- 问题 5:校准过程中点选择错误
- 问题 6:运行时出现 `ModuleNotFoundError`
- 七、实战效果
- 八、改进建议
一、项目概述
covid-social-distancing-detection 是一个基于深度学习的实时社交距离检测系统,旨在通过视频流检测人员之间的距离,以帮助减少 COVID-19 的传播。该项目使用 OpenCV 和 TensorFlow 实现,能够实时检测视频中人员的位置,并通过鸟瞰图(bird’s-eye view)计算人员之间的距离,从而判断是否存在社交距离违规。
二、项目架构
- 对象检测:使用预训练的深度学习模型(如 YOLOv3 或 Faster R-CNN)检测视频帧中的人物。
- 透视变换:将视频帧转换为鸟瞰图,以便在 2D 平面上测量距离。
- 距离计算:计算人员之间的距离,并标记违反社交距离规则的人员对。
- 实时监控:对实时视频流进行处理,输出带有标记的视频帧。
三、环境搭建
- 安装 Python 库:
- 安装 OpenCV、TensorFlow 和其他依赖项:
pip install -r requirements.txt
- 安装 OpenCV、TensorFlow 和其他依赖项:
- 下载预训练模型:
- 从 TensorFlow 模型库下载 Faster R-CNN Inception V2 COCO 模型:
wget http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_v2_coco_2018_01_28.tar.gz tar -xvzf faster_rcnn_inception_v2_coco_2018_01_28.tar.gz
- 将解压后的模型文件放在
models/
目录下。
- 从 TensorFlow 模型库下载 Faster R-CNN Inception V2 COCO 模型:
四、运行项目
- 校准:
- 运行校准脚本以确定视频帧的透视变换矩阵:
python calibrate_with_mouse.py
- 输入视频文件名和帧大小(默认为
PETS2009.avi
和 800 像素)。 - 按照提示在视频帧中选择四个点(右上、右下、左下、左上)。
- 运行校准脚本以确定视频帧的透视变换矩阵:
- 启动社交距离检测:
- 运行检测脚本:
python social_distanciation_video_detection.py
- 输入 TensorFlow 模型名称(默认为
faster_rcnn_inception_v2_coco_2018_01_28
)、视频文件名(默认为PETS2009.avi
)和人员之间的最小距离(以像素为单位)。
- 运行检测脚本:
五、输出结果
- 处理后的视频帧将存储在
outputs/
目录中。 - 视频输出包括正常帧和鸟瞰图,违规的人员对将用红色框和红线标记。
六、常见问题及解决方法
报错1. cv2.error: OpenCV(4.11.0) 👎 error: (-5:Bad argument) in function ‘circle’
Traceback (most recent call last):File "X:\000Project\CVprojects\CVprojects\covid-social-distancing-detection\src\social_distanciation_video_detection.py", line 204, in <module>cv2.circle(bird_view_img, (x, y), BIG_CIRCLE, COLOR_GREEN, 2)
cv2.error: OpenCV(4.11.0) :-1: error: (-5:Bad argument) in function 'circle'
> Overload resolution failed:
> - Can't parse 'center'. Sequence item with index 0 has a wrong type
> - Can't parse 'center'. Sequence item with index 0 has a wrong type
解决方法
# 确保 x 和 y 是整数
x = int(x)
y = int(y)
# 确保 bird_view_img 是有效的图像对象
if bird_view_img is not None:# 确保 BIG_CIRCLE 和 COLOR_GREEN 是有效的值BIG_CIRCLE = 20COLOR_GREEN = (0, 255, 0)cv2.circle(bird_view_img, (x, y), BIG_CIRCLE, COLOR_GREEN, 2)
else:print("Error: bird_view_img is not initialized properly.")
报错2 cv2.circle(bird_view_img, (pair[0][0],pair[0][1]), BIG_CIRCLE, COLOR_RED, 2)
raceback (most recent call last):File "X:\000Project\CVprojects\CVprojects\covid-social-distancing-detection\src\social_distanciation_video_detection.py", line 226, in <module>change_color_on_topview(pair)File "X:\000Project\CVprojects\CVprojects\covid-social-distancing-detection\src\social_distanciation_video_detection.py", line 77, in change_color_on_topviewcv2.circle(bird_view_img, (pair[0][0],pair[0][1]), BIG_CIRCLE, COLOR_RED, 2)
cv2.error: OpenCV(4.11.0) :-1: error: (-5:Bad argument) in function 'circle'
> Overload resolution failed:
> - Can't parse 'center'. Sequence item with index 0 has a wrong type
> - Can't parse 'center'. Sequence item with index 0 has a wrong type
解决方法
def change_color_on_topview(pair):global bird_view_img, BIG_CIRCLE, SMALL_CIRCLE, COLOR_RED# 确保 x 和 y 是整数x1 = int(pair[0][0])y1 = int(pair[0][1])x2 = int(pair[1][0])y2 = int(pair[1][1])# 确保坐标值在有效范围内x1 = max(0, min(width - 1, x1))y1 = max(0, min(height - 1, y1))x2 = max(0, min(width - 1, x2))y2 = max(0, min(height - 1, y2))# 确保 bird_view_img 是有效的图像对象if bird_view_img is None:print("Error: bird_view_img is not initialized properly.")return# 确保 BIG_CIRCLE 和 SMALL_CIRCLE 是有效的值BIG_CIRCLE = 20SMALL_CIRCLE = 5COLOR_RED = (0, 0, 255)# 绘制四个圆cv2.circle(bird_view_img, (x1, y1), BIG_CIRCLE, COLOR_RED, 2)cv2.circle(bird_view_img, (x1, y1), SMALL_CIRCLE, COLOR_RED, -1)cv2.circle(bird_view_img, (x2, y2), BIG_CIRCLE, COLOR_RED, 2)cv2.circle(bird_view_img, (x2, y2), SMALL_CIRCLE, COLOR_RED, -1)
问题 3:缺少 requirements.txt
文件
- 解决方法:确保从 GitHub 仓库克隆了完整的项目代码。
问题 4:TensorFlow 模型下载失败
- 解决方法:检查网络连接,确保可以从 TensorFlow 模型库下载文件。
问题 5:校准过程中点选择错误
- 解决方法:确保按照右上、右下、左下、左上的顺序选择点。
问题 6:运行时出现 ModuleNotFoundError
- 解决方法:确保已安装所有必要的 Python 库,包括 OpenCV 和 TensorFlow。
七、实战效果
该项目能够实时检测视频流中人员之间的距离,并标记违反社交距离规则的人员对。这有助于在公共场所(如火车站、购物中心等)实时监控社交距离规则的遵守情况。
八、改进建议
- 实时性能优化:进一步优化算法以提高实时处理速度。
- 多摄像头支持:扩展系统以支持多个摄像头的输入。
- 隐私保护:确保系统不存储敏感图像数据,仅保留统计信息。
通过上述步骤和建议,你可以成功运行并优化 covid-social-distancing-detection 项目,以应对 COVID-19 疫情中的社交距离监控需求。