ラズパイで物体認識シリーズ YOLO v5 使った物体検出
YOLO標準の物体検出モデルを使って画像から物体検出はなんとなく試せたので、もう少し踏み込んでいきます
【ラズパイで物体認識シリーズ】
・OpenCV の準備
・HaarCascades を使った物体検出
・YOLO v5のセットアップ
・YOLO v5 使った物体検出 ←イマココ
・Xi IoTへの組み込み ちょっと延期
==ラズパイ環境====
raspberry Pi 4 model-B RAM 4GB
$ cat /proc/version
Linux version 5.4.51-v7l+ (dom@buildbot) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611)) #1327 SMP Thu Jul 23 11:04:39 BST 2020
$ cat /etc/debian_version
10.4
========
YOLO v5おさらい
標準の物体検出モデルでおさらします。物体検出行いたい画像・映像ファイルを "inference/images/" 配下に設置します
そしてgithubから入手した detect.py を実行します
$ python3 detect.py --weigths yolov5s.pt
これでデフォルトで "inference/images/" 配下の全ファイルに対して、物体検出を実施します
<代表的な"detect.py"のオプション>
--source | インプットイメージの場所、ビデオデバイスのパス ・[デフォルト] iference/images(ディレクトリパス) ・0(webカメラ) ・rtsp://xxx.xxx.xxx/ |
--weights | 学習済みモデル |
--conf | confidence threshold(信頼度) |
リソースたっぷり検証環境では、画像ファイル一枚当たり0.02秒くらいで推論しているようです
$ python .\detect.py --weights .\yolov5s.pt Namespace(agnostic_nms=False, augment=False, classes=None, conf_thres=0.4, device='', img_size=640, iou_thres=0.5, output='inference/output', save_txt=False, source ='inference/images', update=False, view_img=False, weights=['.\\yolov5s.pt']) Using CUDA device0 _CudaDeviceProperties(name='GeForce GTX 1070', total_memory=8192MB) Fusing layers... Model Summary: 140 layers, 7.45958e+06 parameters, 6.61683e+06 gradients image 1/6 D:\Dsatoru\work\develop\python\opencv\yolo\yolov5-master\inference\images\108C6DD1-DFD7-4C4B-BB73-83BBE63925EA.jpg: 640x512 1 persons, 4 bottles, 1 cups, 1 broccolis, 1 ovens, Done. (0.022s) image 2/6 D:\Dsatoru\work\develop\python\opencv\yolo\yolov5-master\inference\images\4WKaeZ9D0hqas8H5LejtxXV-9Y.jpg: 512x640 3 persons, Done. (0.020s) image 3/6 D:\Dsatoru\work\develop\python\opencv\yolo\yolov5-master\inference\images\802690-wwe.com.jpg: 384x640 2 persons, Done. (0.025s) image 4/6 D:\Dsatoru\work\develop\python\opencv\yolo\yolov5-master\inference\images\ESgjGkzVAAIYb9S.jpg: 384x640 5 persons, Done. (0.024s) image 5/6 D:\Dsatoru\work\develop\python\opencv\yolo\yolov5-master\inference\images\EdSnJmXXgAE_Boj.png: 384x640 2 persons, Done. (0.022s) image 6/6 D:\Dsatoru\work\develop\python\opencv\yolo\yolov5-master\inference\images\zidane.jpg: 384x640 2 persons, 2 ties, Done. (0.021s) Results saved to D:\Dsatoru\work\develop\python\opencv\yolo\yolov5-master\inference/output Done. (1.392s)
画像サイズなど気にせずおいてもうまいことやってくれます
動画ファイルもなんのその(36秒の動画で、推論処理は30秒くらいで完了)
とりあえずのYOLOv5を動かしてみただけ#xiiot pic.twitter.com/auEPdM8U7r
— konchangakita (@konchangakita) 2020年8月8日
ラズパイ上でも試してみます、と
ラズパイだと画像一枚あたりの推論に3-4秒かかってしまいます。。。
$ python3 detect.py --weights yolov5s.pt Unable to init server: Could not connect: Connection refused Unable to init server: Could not connect: Connection refused (detect.py:1381): Gdk-CRITICAL **: 18:18:36.465: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed (detect.py:1381): Gdk-CRITICAL **: 18:18:36.471: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed Namespace(agnostic_nms=False, augment=False, classes=None, conf_thres=0.4, device='', img_size=640, iou_thres=0.5, output='inference/output', save_txt=False, source='inference/images', update=False, view_img=False, weights=['yolov5s.pt']) Using CPU Fusing layers... Model Summary: 140 layers, 7.45958e+06 parameters, 6.61683e+06 gradients image 1/6 /home/pi/yolo/yolov5/inference/images/108C6DD1-DFD7-4C4B-BB73-83BBE63925EA.jpg: 640x512 1 persons, 4 bottles, 1 cups, 1 broccolis, 1 ovens, Done. (4.175s) image 2/6 /home/pi/yolo/yolov5/inference/images/4WKaeZ9D0hqas8H5LejtxXV-9Y.jpg: 512x640 3 persons, Done. (4.159s) image 3/6 /home/pi/yolo/yolov5/inference/images/802690-wwe.com.jpg: 384x640 2 persons, Done. (3.272s) image 4/6 /home/pi/yolo/yolov5/inference/images/ESgjGkzVAAIYb9S.jpg: 384x640 5 persons, Done. (3.032s) image 5/6 /home/pi/yolo/yolov5/inference/images/EdSnJmXXgAE_Boj.png: 384x640 2 persons, Done. (3.072s) image 6/6 /home/pi/yolo/yolov5/inference/images/zidane.jpg: 384x640 2 persons, 2 ties, Done. (2.639s) Results saved to /home/pi/yolo/yolov5/inference/output
これでは、ラズパイ上でYOLOでリアルタイムってのは結構厳しそうです
OpenCV 単品だと動画もいけたので期待していたのですが。。。
とりあえずラズパイでYOLO with Webカメラを動かしてみる
github からダウンロードした、yolov5ディレクトリで実行するだけです
5秒くらいしたら自動的にプレイヤーが起動します、簡単
$ python3 detect.py --source 0 --weights yolov5s.pt Namespace(agnostic_nms=False, augment=False, classes=None, conf_thres=0.4, device='', img_size=640, iou_thres=0.5, output='inference/output', save_txt=False, source='0', update=False, view_img=False, weights=['yolov5s.pt']) Using CPU Fusing layers... Model Summary: 140 layers, 7.45958e+06 parameters, 6.61683e+06 gradients 1/1: 0... success (640x480 at 30.00 FPS). 0: 512x640 Done. (3.569s) 0: 512x640 Done. (3.849s) 0: 512x640 Done. (3.581s) 0: 512x640 Done. (3.618s)
こんな感じです
YOLOラズパイやっぱきついやんね? pic.twitter.com/opMteSFpyA
— konchangakita (@konchangakita) 2020年8月8日
ラグがおきてるわけではなく実際カクカクなのです
推論にかかっている時間分だけ、カクカクしちゃっているようです
やはりYOLO on ラズパイでリアルタイムを求めるのはちょっとコクなようですね
Xi IoTとのコラボ方法はちょっと方法を考えてみます