Daily Archives: July 1, 2020
RPi : 顔画像の自動収集
ソースコードの取得
Cloning into ‘hello_ghome’…
remote: Enumerating objects: 23, done.
remote: Total 23 (delta 0), reused 0 (delta 0), pack-reused 23
Unpacking objects: 100% (23/23), done.
pi@raspberrypi:~ $
pi@raspberrypi:~/hello_ghome $
顔画像の収集
OpenCVには顔認識用のライブラリがあらかじめ用意されており,そちらを利用するだけで簡単に顔画像を収集できる.
それを利用してWebカメラから撮影した画像から,顔領域で切り抜きを行い,保存をするプログラムを作成し画像を収集した.
コード実行してみる
* 原因
* 解決策
haarcascadeファイルの場所を確認。
pi@raspberrypi:~/hello_ghome $ locate haarcas
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_eye.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_eye_tree_eyeglasses.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_frontalcatface.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_frontalcatface_extended.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_frontalface_alt.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_frontalface_alt2.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_frontalface_alt_tree.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_frontalface_default.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_fullbody.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_lefteye_2splits.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_licence_plate_rus_16stages.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_lowerbody.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_profileface.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_righteye_2splits.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_russian_plate_number.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_smile.xml
/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_upperbody.xml
/usr/share/gst-plugins-bad/1.0/opencv_haarcascades
/usr/share/gst-plugins-bad/1.0/opencv_haarcascades/fist.xml
/usr/share/gst-plugins-bad/1.0/opencv_haarcascades/palm.xml
pi@raspberrypi:~/hello_ghome $
ファイルリンクしてみる
pi@raspberrypi:~/hello_ghome $ sudo mkdir /usr/local/share/OpenCV
pi@raspberrypi:~/hello_ghome $ cd /usr/local/share/OpenCV
pi@raspberrypi:/usr/local/share/OpenCV $ sudo ln -s /usr/local/lib/python3.7/dist-packages/cv2/data haarcascades
pi@raspberrypi:/usr/local/share/OpenCV $ ll
total 0
lrwxrwxrwx 1 root root 47 Jul 3 00:26 haarcascades -> /usr/local/lib/python3.7/dist-packages/cv2/data
再度, 同じコマンドでファイル確認できた。
pi@raspberrypi:/usr/local/share/OpenCV $ ll /usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml
-rw-r–r– 1 root staff 676709 Jul 2 15:37 /usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml
img//2020-07-03-003242_0.jpgis clip and saved!
img//2020-07-03-003243_0.jpgis clip and saved!
img//2020-07-03-003243_0.jpgis clip and saved!
img//2020-07-03-003243_0.jpgis clip and saved!
img//2020-07-03-003243_0.jpgis clip and saved!
img//2020-07-03-003243_0.jpgis clip and saved!
img//2020-07-03-003243_0.jpgis clip and saved!
img//2020-07-03-003243_0.jpgis clip and saved!
img//2020-07-03-003244_0.jpgis clip and saved!
img//2020-07-03-003244_0.jpgis clip and saved!
img//2020-07-03-003244_0.jpgis clip and saved!
img//2020-07-03-003244_0.jpgis clip and saved!
img//2020-07-03-003244_0.jpgis clip and saved!
img//2020-07-03-003244_0.jpgis clip and saved!
img//2020-07-03-003244_0.jpgis clip and saved!
img//2020-07-03-003245_0.jpgis clip and saved!
img//2020-07-03-003245_0.jpgis clip and saved!
img//2020-07-03-003248_0.jpgis clip and saved!
参考
RPi:VNCでリモート作業
MacのFinderからVNC接続
- 起動方法は簡単です。 Finderを起動して、メニューバーの「移動」ー「サーバへ接続」を選択します。 …
- サーバアドレスには「vpn://220.99.0.xx:ポート番号」の書式で入力します。 …
例:vnc://192.168.86.140:5901 - 認証画面が表示されますので、VNCサーバで指定しているパスワードを入力します。
もし、接続できない場合、VNCの最新プロトコルに対応してない可能性があるので、次のRealVNCを利用した接続を試しましょう。
RealVNCを利用した接続
Raspberry Pi に毎回 HDMI・キーボ・マウスをつなぐのが面倒、SSHはまた慣れてない、MacとかからGUIで触れるようにしたい。その場合RealVNCを利用という選択肢があります。
Raspbian OSにはVNCサーバ(RealVNC)が組み込まれていますが、既定では無効になっています。
VNCビューアのインストール
RealVNCと互換性のあるVNCクライアントアプリを用意してください。
(macOSの場合、VNCクライアントなくでも、Finderから利用できます。)
Download VNC Viewer to the device
使用しているPCがMacの場合は、上記サイトを開いてから [macOS] アイコンをクリックしてください。
その後、画面中央の [DOWNLOAD VNC VIEWER]をクリックしてインストールイメージ(dmgファイル)をダウンロードします。
VNCビューアの起動
インストールされた「VNC Viewer」を起動します。
初期起動画面が表示されるので、そのまま[GOT IT]ボタンをクリックします。
ビューア画面が表示されたら、アドレス入力欄に Raspberry Pi のIPアドレスを入力して [enter]キーを押下します。
ログインユーザ名とパスワードを入力して [OK]ボタンをクリックします。
Piのサーバ設定
VNCを有効にする
最初からデスクトップ版のイメージでSDカードを作成した場合、VNCもインストールされ、これを有効化すればVNCクライアントから接続できるようになります。
Raspberry Pi を起動してデスクトップ画面が表示されたら、メニューバー左端のラズベリーアイコンをクリックします。
表示されたプルダウンメニューから [設定]-[Raspberry Piの設定]をクリックします。
「Raspberry Piの設定」画面で「インタフェース」タブをクリックし、[VNC]の「有効」を選択してから[OK]ボタンをクリックします。
VNCをインストール
Ref:
RPi:Install Open CV
システムを最新版に更新
Get:1 http://archive.raspberrypi.org/debian buster InRelease [32.6 kB]
Get:2 http://raspbian.raspberrypi.org/raspbian buster InRelease [15.0 kB]
Get:3 http://raspbian.raspberrypi.org/raspbian buster/main armhf Packages [13.0 MB]
Get:4 http://archive.raspberrypi.org/debian buster/main armhf Packages [330 kB]
Get:5 http://raspbian.raspberrypi.org/raspbian buster/non-free armhf Packages [104 kB]
Fetched 13.5 MB in 11s (1,246 kB/s)
Reading package lists… Done
Reading package lists… Done
Building dependency tree
Reading state information… Done
Calculating upgrade… Done
The following package was automatically installed and is no longer required:
rpi-eeprom-images
Use ‘sudo apt autoremove’ to remove it.
The following packages will be upgraded:
arandr ca-certificates firmware-atheros firmware-brcm80211 firmware-libertas firmware-misc-nonfree firmware-realtek libfm-data libfm-extra4
libfm-gtk-data libfm-gtk4 libfm-modules libfm4 libgnutls30 libnode-dev libnode64 libraspberrypi-bin libraspberrypi-dev libraspberrypi-doc
libraspberrypi0 libvlc-bin libvlc5 libvlccore9 lxpanel lxpanel-data nodejs nodejs-doc pcmanfm pi-greeter pi-package pi-package-data
pi-package-session piclone pipanel piwiz raspberrypi-bootloader raspberrypi-kernel raspberrypi-ui-mods raspi-config rp-bookshelf rp-prefapps
rpi-chromium-mods rpi-eeprom rpi-eeprom-images scratch2 vlc vlc-bin vlc-data vlc-l10n vlc-plugin-base vlc-plugin-notify vlc-plugin-qt
vlc-plugin-samba vlc-plugin-skins2 vlc-plugin-video-output vlc-plugin-video-splitter vlc-plugin-visualization
57 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 225 MB of archives.
After this operation, 7,697 kB disk space will be freed.
Do you want to continue? [Y/n] y
Processing triggers for ca-certificates (20200601~deb10u1) …
Updating certificates in /etc/ssl/certs…
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d…
done.
done.
Processing triggers for libvlc-bin:armhf (3.0.11-0+deb10u1+rpt1) …
pi@raspberrypi:~ $
opencvインストール
Downloading https://www.piwheels.org/simple/opencv-python/opencv_python-4.1.1.26-cp37-cp37m-linux_armv7l.whl (10.0MB)
100% |████████████████████████████████| 10.0MB 45kB/s
Requirement already satisfied: numpy>=1.16.2 in /usr/lib/python3/dist-packages (from opencv-python) (1.16.2)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.1.1.26
(2023/1/18 update)最近上記のコマンドでbuildが延々と終わらない、最後にエラーになりました。数日悩んだ解決策は:
pipのバージョンが古いため起きる問題ではないかと、試す。
pi@chlab-a71b3eca:~ $ pip install –upgrade pip
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: pip in /usr/lib/python3/dist-packages (20.3.4)
Collecting pip
Using cached pip-22.3.1-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
WARNING: The scripts pip, pip3, pip3.10 and pip3.9 are installed in ‘/home/pi/.local/bin’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
Successfully installed pip-22.3.1
それでも効果がない!
https://teratail.com/ でARMアーキテクチャでは cmake の問題で OpenCV のビルドに失敗するようです。ビルド済みのパッケージのインストールではいいらしい。
pi@chlab-a71b3eca:~ $ sudo apt-get update
Get:1 http://raspbian.raspberrypi.org/raspbian bullseye InRelease [15.0 kB]
Get:2 http://archive.raspberrypi.org/debian bullseye InRelease [23.6 kB]
Get:3 http://archive.raspberrypi.org/debian bullseye/main armhf Packages [311 kB]
Fetched 349 kB in 3s (124 kB/s)
Reading package lists… Done
pi@chlab-a71b3eca:~ $ sudo apt-get install python-opencv
Reading package lists… Done
Building dependency tree… Done
Reading state information… Done
E: Unable to locate package python-opencv
ビルド済みのパッケージがない?
pi@chlab-a71b3eca:~ $ sudo apt-get install python3-opencv
これてうまくいく。
opencvで動作確認
追加パッケージ
必要なライブラリが足りないと、プログラム中エラーになるので、さらに不足のライブラリを一気に追加する。
$ sudo apt-get install libjasper-dev
$ sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig libqt4-test
$ sudo apt-get install libatlas-base-dev
環境変数にLD_PRELOAD追加
ついにopencvの動作確認でき
参考
- https://creepfablic.site/2020/04/10/python-opencv-error/
- https://qiita.com/muramasa2/items/80a2cae31bafb9de0ea2
- https://teratail.com/questions/301644
RPi:カメラの利用
USBカメラの利用
USB デバイス一覧からも確認
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 008: ID 056e:7009 Elecom Co., Ltd
Bus 001 Device 003: ID 046d:c534 Logitech, Inc. Unifying Receiver
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
pi@raspberrypi:~ $
デバイスのリストからも確認
ソフトでカメラの確認
guvcview
guvcview
を使用してみました。pi@raspberrypi:~ $ sudo apt-get install guvcview
guvcview
を探して、起動してみてください。fswebcam
— Opening /dev/video0…
Trying source module v4l2…
/dev/video0 opened.
No input was specified, using the first.
Adjusting resolution from 384×288 to 352×288.
— Capturing frame…
Captured frame in 0.00 seconds.
— Processing captured image…
Writing JPEG image to ‘image.jpg’.
pi@raspberrypi:~/Pictures $
公式内蔵カメラの利用
カメラを有効にする
「sudo raspi-config」を起動して、「Legacy Camera Enable/disble legacy camera support」を選択して、Enableしてください。
カメラの回転
カメラ回転の場合、次のコマンドでファイルに v4l2-ctl –set-ctrl=rotate=90 を/etc/rc.local に追加。
pi@raspberrypi:~ $ sudo vi /etc/rc.local
カメラの認識
コマンドの次の行に「supported=1 detected=1」と表示されたら、カメラが認識されていることになります。
pi@raspberrypi:~ $ vcgencmd get_camera
supported=1 detected=1
pi@raspberrypi:~ $
写真を撮影
カメラモジュールが正常に認識できていることを確認できたら、実際に写真を撮影してみましょう。
写真撮影の操作もコマンドを入力して行います。
LXTerminalで「sudo raspistill -o image.jpg」と入力して実行します。
コマンドを実行すると、撮影画像が画面上に5秒ほど表示されます。
参考
- https://www.raspberrypi.org/documentation/usage/webcams/
- https://uepon.hatenadiary.com/entry/2017/01/22/170349 ーー RaspberryPiでUSB接続のWebカメラを使ってみる
- https://www.pc-koubou.jp/magazine/17276 ーー Raspberry Piとカメラモジュールを接続する
RPi:SDカード作成
Downloads
最新版の「Raspberry Pi OS」をダウンロードして下さい。
https://www.raspberrypi.org/downloads/
Raspberry Piユーザーは、Windows、macOS、Ubuntuで利用可能な新しいRaspberry Pi Imagerをダウンロードして使用できるようになります。
このユーティリティは、メカニクスに導入したいくつかのショートカットのおかげで、使いやすく、非常に高速です。
(今まではEtcherを使い焼き込みます。 https://www.balena.io/etcher/ )
初めて起動する
初めて起動する場合、 RaspberryPi にできたSDカードを差し込み、下記のものを接続し、電源は最後に接続する。
- USBキーボード
- HDMIディスプレイ
- 電源(5V、2.5A)
ディスプレイで RaspberryPi用イメージ の起動を確認する。
Raspberry Pi基本のセットアップ
Raspberry Pi基本のセットアップについて、Linux & Raspberry Piを参照してください。
次は機械学習に必要なセットアップ手順;
- 「Hostname」に好きなホスト名
- Change Passwordからパスワード変更
- 「SSH」の「Enable」
- 「Camera」の「Enable」
- 「Set Locale」の「Japan」
- 「Set Timezone」は「Japan」
- 「Keyboard」は「Japanese」
OKをクリックすると「再起動」
次は、システムを最新状態に更新
1 sudo apt update
2 sudo apt upgrade
固定IPに
リモート作業がメインになるので、IPアドレスしょっちゅう変えじゃうと困りますね。
IPアドレスを固定することができる。まず現在のIPを調べる。
$ ifconfig
そのIP範囲に固定のIPアドレスを設定。
$ sudo vi /etc/dhcpcd.conf
interface wlan0
static ip_address=192.168.0.70/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1