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 疫情中的社交距離監控需求。