環境直接使用第一篇中安裝好的環境即可
先clone yolov5-face項目
git clone https://github.com/deepcam-cn/yolov5-face.git
并下載預訓練權重文件yolov5n-face.pt
網盤鏈接: https://pan.baidu.com/s/1xsYns6cyB84aPDgXB7sNDQ 提取碼: lw9j
(野火官方提供)
再找一張帶人臉的圖片test.jpg保存在yolov5-face項目文件夾下
使用以下命令開始運行人臉檢測任務
python detect_face.py --weights ./yolov5n-face.pt --img-size 640 --source ./test.jpg --view-img
這個時候,只要報錯:
ModuleNotFoundError: No module named 'xxx'
就使用下面的命令安裝一下相關的庫就好
pip install xxx
再運行上面的人臉檢測任務,我這又出現錯誤:
RuntimeError: Couldn't load custom C++ ops. This can happen if your PyTorch and torchvision versions are incompatible, or if you had errors while compiling torchvision from source. For further information on the compatible versions, check https://github.com/pytorch/vision#installation for the compatibility matrix. Please check your PyTorch version with torch.__version__ and your torchvision version with torchvision.__version__ and verify if they are compatible, and if not please reinstall torchvision so that it matches your PyTorch install.
這個錯誤是當前環境中安裝的?PyTorch 與 torchvision 二者版本不匹配,導致無法加載自定義 C++ 擴展導致的
使用以下代碼查看PyTorch 與 torchvision當前的版本
python - <<EOF
import torch, torchvision
print("PyTorch:", torch.__version__)
print("torchvision:", torchvision.__version__)
EOF
我這顯示
PyTorch: 2.3.0
torchvision: 0.15.2a0
解決辦法:先卸載當前安裝的pytorch和torchvision,再重新安裝指定相匹配版本的pytorch和torchvision
# 卸載
pip uninstall torch torchvision -y# 重新安裝指定版本
pip install torch==2.3.0 torchvision==0.18.0 --extra-index-url https://download.pytorch.org/whl/cu121
再次執行人臉檢測任務,雖然成功檢測出來人臉,但是顯示圖片時又出現錯誤:
libGL error: failed to create dri screen
libGL error: failed to load driver: rockchip
libGL error: failed to create dri screen
libGL error: failed to load driver: rockchip
qt.qpa.xcb: QXcbConnection: XCB error: 148 (Unknown), sequence: 192, resource id: 0, major code: 140 (Unknown), minor code: 20
這個問題是沒能正常加載Rockchip GPU 的驅動程序導致openGL加載失敗,安裝驅動解決問題:
sudo apt-get install mesa-utils libdrm-dev libegl-dev libgles2-mesa-dev
再次執行檢測任務?
python detect_face.py --weights ./yolov5n-face.pt --img-size 640 --source ./test.jpg --view-img
顯示檢測成功,圖片也能正常顯示
loading images ./test.jpg
image 1/1 /home/cat/yolov5/yolov5-face/test.jpg: 10 faces
使用rtsp地址代替圖片,即可進行監控畫面實時人臉檢測?
python detect_face.py --weights ./yolov5n-face.pt --img-size 640 --source rtsp://192.168.31.217:554/ch01.264 --view-img