Category Archives: Python ML@ RPi
RPi : GoogleHome通知
RPi:SSHでリモート作業
現在のIPアドレスの確認
まず、現在のIPアドレスを調べてください。
$ ifconfig
Wi-Fi接続が完了すると、MacとRaspberryPiは、Wi-Fi経由で接続できます。 まずpingコマンドでネットの接続を確認してください。
$ ping 192.168.0.110
PING 192.168.0.110 (192.168.0.110): 56 data bytes
64 bytes from 192.168.0.110: icmp_seq=0 ttl=64 time=18.073 ms
64 bytes from 192.168.0.110: icmp_seq=1 ttl=64 time=6.089 ms
接続は確認したら、リモートログインへ進んでください。
標準RaspberryPiの SSHを有効化
Raspberry Pi は、デフォルトでSSHはが無効になっています。 有効にするにはraspi-configで設定する必要があるのですが、/bootにsshという名前のファイルを置くだけでも有効にできます。
/bootのパーティションは、Macに接続しただけで, そのままアクセス可能ですので、touchでSSHを有効化できます。
ただし、/boot/sshは、raspi-configで設定されていない場合だけ使用可能です。raspi-configでSSHを無効化した場合は、/boot/sshが存在しても、SSHは有効になりません。
MacOSからリモートログイン
ユーザpiをipのサーバにログインとする。
$ ssh pi@ip
例:
chen@Hong-Mac-mini workspace % ssh pi@192.168.86.140
The authenticity of host '192.168.86.140 (192.168.86.140)' can't be established.
ECDSA key fingerprint is SHA256:9aiGsOD5AIkArHq+ghDIlPUnUi/3zM57WeMmxtCVa+I.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.86.140' (ECDSA) to the list of known hosts.
pi@192.168.86.140's password:
Linux raspberrypi 4.19.118-v7l+ #1311 SMP Mon Apr 27 14:26:42 BST 2020 armv7l
初回ログインする際、fingerprintの確認があります。
一度接続すると、fingerprint記録されるので、次回は確認しなくなる。
同じIPで過去ログインしたPiサーバの中身が入れ替わり、ログインが拒否される。その場合、~.ssh/know_hostに該当する(fingerprintを含めた)項目を削除してください。
Windowsからからリモートログイン
Windowsの場合puttyなどターミナルツールを予めインストールしてください。
putty.exe を起動すると PuTTY 設定 のウィンドウが現れます.
ここで Pi の IP アドレスを入力し, プロトコルに ssh を選択します.
RPi : CNNで深層学習
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
必要なライブラリが足りないと、プログラム中エラーになるので、不足のライブラリを一気に追加する。
$ 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
opencvで動作確認
参考
- https://creepfablic.site/2020/04/10/python-opencv-error/
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 $
公式内蔵カメラの利用
参考
- https://www.raspberrypi.org/documentation/usage/webcams/
- https://uepon.hatenadiary.com/entry/2017/01/22/170349 ーー RaspberryPiでUSB接続のWebカメラを使ってみる
RPi:SDカード作成
Downloads
最新版の「Raspberry Pi OS」をダウンロードして下さい。
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
RPi : Install Python
既存Pythonの確認
最新版の「Raspberry Pi OS」でSDを作成した場合、比較的に新しいバージョンのPythonがすでにインストールしている場合がある。
pi@raspberrypi:~ $ python -V
Python 2.7.16
pi@raspberrypi:~ $ python3 -V
Python 3.7.3
pi@raspberrypi:~ $ pip -V
pip 18.1 from /usr/lib/python2.7/dist-packages/pip (python 2.7)
pi@raspberrypi:~ $ pip3 -V
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
pi@raspberrypi:~ $
インストール
使いのSDに、Pythonがインストールしていない場合、次のコマンドでインストールしてください。
関連記事
- http://ups.edu2web.com/2018/11/13/pi-machine-learning-1-setup/
- http://ups.edu2web.com/2018/10/21/scikit-learn1-install/
- https://digilib.edu2web.com/2017/05/19/raspberry-pi-6-nokia-5110-lcd/
- https://digilib.edu2web.com/2018/09/18/raspberry-pi-9-waveshare-2-13inch-e-paper-hat/