m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

jetson nanoでusb接続したカメラより物体検知

USBカメラ接続

ラズパイのカメラもあるけど接続がUSBの方が簡単そうだったのでUSBにした

USB接続し、認識されていること確認

$ lsusb
Bus 002 Device 002: ID xxxx:xxxx Realtek Semiconductor Corp.
Bus 002 Device 001: ID xxxx:xxxx Linux Foundation 3.0 root hub
Bus 001 Device 005: ID xxxx:xxxx Elecom Co., Ltd
Bus 001 Device 004: ID xxxx:xxxx Logitech, Inc. Unifying Receiver
Bus 001 Device 003: ID xxxx:xxxx
Bus 001 Device 002: ID xxxx:xxxx Realtek Semiconductor Corp.
Bus 001 Device 001: ID xxxx:xxxx Linux Foundation 2.0 root hub
$
$ ls /dev/video*
/dev/video0
$

実装

import jetson.inference
import jetson.utils

net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5)
camera = jetson.utils.gstCamera(1280, 720, "/dev/video0")
display = jetson.utils.glDisplay()

while display.IsOpen():
    img, width, height = camera.CaptureRGBA()
    detections = net.Detect(img, width, height)
    
    print detections
    if len(detections) > 0:
        print("detect count= {0}".format(len(detections)))
        for detection in detections:
            print ("ClassID:{0}, Confidence:{1}" \
                  + ", Left:{2}, Top:{3}, Right:{4}, Bottom:{5}" \
                  + ", Width:{6}, Height:{7}, Area:{8}, Center:{9}" \
                  ).format(detection.ClassID
                          , detection.Confidence
                          , detection.Left
                          , detection.Top
                          , detection.Right
                          , detection.Bottom
                          , detection.Width
                          , detection.Height
                          , detection.Area
                          , detection.Center
                          )

    display.RenderOnce(img, width, height)
    display.SetTitle("Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS()))

実験(汚部屋御免)


jetson nanoでカメラ画像より物体検知 うーん、動画うまく取る方法がわからん( ;∀;)

参考

www.youtube.com

jetson nanoで物体検知を行う

jetson nanoで物体検知を試した

日本語の情報がどのあたりが最新か不明のためでいろいろ手間取った ssh経由でやVNCでやろうとしたけど結局直接端末でやらないと面倒そうなので端末で制御

パッケージインストール

sudo apt-get update
sudo apt-get install git cmake libpython3-dev python3-numpy

jetson-interfaceを利用して画像認識を行うため、git clone

git clone --recursive https://github.com/dusty-nv/jetson-inference
cd jetson-inference
mkdir build
cd build
cmake ../

↓ モデルダウンロード f:id:m_shige1979:20200621100545j:plain

# modelを選択する(デフォルトのまま)
[ ] 1  Image Recognition - all models          (2.2 GB)
[ ] 2     > AlexNet                            (244 MB)
[*] 3     > GoogleNet                          (54 MB)
[ ] 4     > GoogleNet-12                       (42 MB)
[*] 5     > ResNet-18                          (47 MB)
[ ] 6     > ResNet-50                          (102 MB)
[ ] 7     > ResNet-101                         (179 MB)
[ ] 8     > ResNet-152                         (242 MB)
[ ] 9     > VGG-16                             (554 MB)
[ ] 10    > VGG-19                             (575 MB)
[ ] 11    > Inception-V4                       (172 MB)
[ ] 12 Object Detection - all models           (395 MB) 
[ ] 13    > SSD-Mobilenet-v1                   (27 MB)
[*] 14    > SSD-Mobilenet-v2                   (68 MB)
[ ] 15    > SSD-Inception-v2                   (100 MB)
[*] 16    > PedNet                             (30 MB)
[ ] 17    > NultiPed                           (30 MB)
[*] 18    > FaceNet                            (24 MB)
[*] 19    > DetectNet-COCO-Dog                 (29 MB)
[ ] 20    > DetectNet-COCO-Bottle              (29 MB)
[ ] 21    > DetectNet-COCO-Chair               (29 MB)
[ ] 22    > DetectNet-COCO-Airplane            (29 MB)
[ ] 23 Semantic Segmentation - all             (518 MB)
[*] 24    > FCN-ResNet18-Cityscapes-512x256    (47 MB)
[*] 25    > FCN-ResNet18-Cityscapes-1024x512   (47 MB)
[ ] 26    > FCN-ResNet18-Cityscapes-2048x1024  (47 MB)
[*] 27    > FCN-ResNet18-DeepScene-576x320     (47 MB)
[ ] 28    > FCN-ResNet18-DeepScene-864x480     (47 MB)
[*] 29    > FCN-ResNet18-MHP-512x320           (47 MB)
[ ] 30    > FCN-ResNet18-MHP-512x320           (47 MB)
[*] 31    > FCN-ResNet18-Pascal-VOC-320x320    (47 MB)
[ ] 32    > FCN-ResNet18-Pascal-VOC-512x320    (47 MB)
[*] 33    > FCN-ResNet18-SUN-RGBD-512x400      (47 MB)
[ ] 34    > FCN-ResNet18-SUN-RGBD-640x512      (47 MB)
[ ] 35 Semantic SEgmentation -legacy           (1.4 GB)
[ ] 36    > FCB-Alexnet-Cityscapes-SD          (235 MB)
[ ] 37    > FCB-Alexnet-Cityscapes-HD          (235 MB)
[ ] 38    > FCB-Alexnet-Aerial-FPV             (7 MB)
[ ] 39    > FCB-Alexnet-Pascal-VOC             (235 MB)
[ ] 40    > FCB-Alexnet-Synthia-CVPR           (235 MB)
[ ] 41    > FCB-Alexnet-Synthia-Summer-SD      (235 MB)
[ ] 42    > FCB-Alexnet-Synthia-Summer-HD      (235 MB)
[ ] 43 Image Processing - all models           (138 MB)
[ ] 44    > Deep-Homography-COCO               (137 MB)
[ ] 45    > Super-Resolution-BSD500            (1MB MB)
→  了解

↓ PyTorchはスキップしても良いかも? f:id:m_shige1979:20200621101418j:plain

 [ ] 1 PyTorch v1.1.0 for Python 2.7
 [ ] 2 PyTorch v1.1.0 for Pythin 3.6
→  了解

↓ ビルド&インストール&ライブラリ登録

make
sudo make install
sudo ldconfig

動作確認

1回目(初回はいろいろ準備するものがあるようで時間がかかる)

cd $HOME/jetson-inference/build/aarch64/bin/
./detectnet-console.py images/peds_0.jpg output_0.jpg

結果 f:id:m_shige1979:20091008113933j:plainf:id:m_shige1979:20200621110117j:plain

2回目

./detectnet-console.py images/peds_1.jpg output_1.jpg

結果 f:id:m_shige1979:20130305114518j:plainf:id:m_shige1979:20200621110203j:plain

参考

www.youtube.com jetson-inference/building-repo-2.md at master · dusty-nv/jetson-inference · GitHub

補足

# ダウンロードツール起動コマンド
cd jetson-inference/tools
./download-models.sh

# PyTorchのインストーラー起動コマンド
cd jetson-inference/build
./install-pytorch.sh

# 自前マシンへコピー
scp jetson@jetson-desktop.local:/home/jetson/jetson-inference/build/aarch64/bin/images/peds_0.jpg $HOME/work/peds_0.jpg
scp jetson@jetson-desktop.local:/home/jetson/jetson-inference/build/aarch64/bin/output_0.jpg $HOME/work/output_0.jpg
scp jetson@jetson-desktop.local:/home/jetson/jetson-inference/build/aarch64/bin/images/peds_1.jpg $HOME/work/peds_1.jpg
scp jetson@jetson-desktop.local:/home/jetson/jetson-inference/build/aarch64/bin/output_1.jpg $HOME/work/output_1.jpg

jetson nanoにusbのwifiを接続1

今回使用したもの

接続しても動かない

なんかドライバが認識されてないっぽい

ドライバとかインストール方法を調査

かんたんにいうと以下で対応できた。

sudo apt-get install dkms
git clone -b v5.6.4.2 https://github.com/aircrack-ng/rtl8812au.git
cd rtl8812au/
sudo ./dkms-install.sh

※ただ、usbを接続していた場合は一度抜き差ししないと認識してくれないので注意

f:id:m_shige1979:20200619001841j:plain OK

参考情報

GitHub - aircrack-ng/rtl8812au: RTL8812AU/21AU and RTL8814AU driver with monitor mode and frame injection tp-link Archer T2U Nano AC600をLinuxで使う方法 - Shizuka's Style (Tre)

jetson nanoにvncインストール

vncサーバインストール

$ sudo apt install tigervnc-common tigervnc-standalone-server tigervnc-scraping-server

vncサーバのパスワード設定

$ vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
$

環境変数DISPLAY確認

$ env | grep DISPLAY
DIPLAY=:1

※実際にjetsonの端末より実行、SSH接続の場合はうまく動作しない可能性あり

起動確認

$ x0vncserver -display :0 -passwordfile ~/.vnc/passwd
Sun Jun 14 09:53:11 2020
 Geometry:    Desktop geometry is set to 1920x1080+0+0
 Main:        XTest extension present - version 2.2
 Main:        Listening on port 5900

接続

・finderより接続
f:id:m_shige1979:20200614104849p:plain

vncで設定したパスワードでログイン
f:id:m_shige1979:20200614104859p:plain

・かくにん
f:id:m_shige1979:20200614104910p:plain

自動起動

$ vi /etc/systemd/system/x0vncserver.service
---
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target
After=network.target remote-fs.target nss-lookup.target
After=x11-common.service

[Service]
Type=forking
User=jetson
Group=jetson
WorkingDirectory=/home/jetson
ExecStart=/bin/sh -c 'sleep 10 && /usr/bin/x0vncserver -display :1  -rfbport 5900 -passwordfile /home/jetson/.vnc/passwd &'

[Install]
WantedBy=multi-user.target
---

systemctlで起動

sudo systemctl enable x0vncserver.service
sudo systemctl start x0vncserver.service

再起動

sudo reboot

所感

GUI画面でログインしておかないと
VNCサーバが起動しないよー( ;∀;)
あまり時間を当てられないのでここまでとする

2020/06/19追記 自動ログインを設定することで対応できました。(^o^)

jetson nanoセットアップ

jetson nanoセットアップ

・ jetson nano www.amazon.co.jp ・SDCard www.amazon.co.jp

ダウンロード&SDカード書き込み

ダウンロード

https://developer.nvidia.com/jetson-nano-sd-card-image

書き込みツールを使用してSDカード(micro)へ書き込み

balenaEtcher - Flash OS images to SD cards & USB drives

起動

配線

f:id:m_shige1979:20200613135448j:plain

起動

f:id:m_shige1979:20200613135512j:plain ※「A start job is running for End-user configuration after initial OEM installation・・・・・」と表示された場合は一度、電源を切って再起動する

セットアップ

利用規約

f:id:m_shige1979:20200613175524j:plain

言語

f:id:m_shige1979:20200613175543j:plain

キーボード設定

f:id:m_shige1979:20200613175658j:plain

ロケール設定

f:id:m_shige1979:20200613175731j:plain

ユーザー設定

f:id:m_shige1979:20200613175837j:plain

パーティション

f:id:m_shige1979:20200613175852j:plain

なんかいろいろあって完了

f:id:m_shige1979:20200613180152j:plain

apt update

$ sudo apt-get update
$ sudo apt-get upgrade

swap領域確認

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           3.9G        1.6G        798M         46M        1.5G        2.1G
Swap:          1.9G          0B        1.9G
$

swap領域作成

$ git clone https://github.com/JetsonHacksNano/installSwapfile
$ cd installSwapfile

$ vi ./installSwapfile.sh
----
SWAPSIZE=4
----
$ ./installSwapfile.sh

参考 * https://qiita.com/karaage0703/items/b14c249aa33112669ee4

メモリ確認

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           3.9G        1.6G        399M         46M        1.8G        2.0G
Swap:          7.9G        1.7M        7.9G
$ 

日本語入力対応

sudo apt-get install fonts-ipafont fonts-ipaexfon
sudo apt-get install fcitx-mozc
sudo reboot

参考 * https://toyo-interest.com/jetson-nano/jetson-nano%e6%97%a5%e6%9c%ac%e8%aa%9e%e5%85%a5%e5%8a%9b%e3%81%a7%e3%81%8d%e3%82%8b%e3%82%88%e3%81%86%e3%81%ab%e3%81%99%e3%82%8b/

再起動

$ sudo reboot

日本語入力がうまく動かない( ;∀;) とりあえずここまで 次はデモやってみる

Docker for Windowsをインストール

Macではなく

Windowsで行う、
ただ、Windowsでの場合は「Hyper-V」で動作するため、VirtualBox使えない
いや、まあ古いやつならいけるけど、最新の使ってみたくなった

ダウンロード

hub.docker.com
ここからダウンロード

インストール

Hyper-Vを有効にして、再起動

f:id:m_shige1979:20200224100101p:plain

インストーラーを実行

f:id:m_shige1979:20200224100143p:plain

インストールを行う

f:id:m_shige1979:20200224100225p:plain
f:id:m_shige1979:20200224100240p:plain
f:id:m_shige1979:20200224100251p:plain

インストールが完了したら再起動を行う

f:id:m_shige1979:20200224100302p:plain

dokcerサービスを起動

f:id:m_shige1979:20200224100312p:plain

タスクバーのdockerアイコンを確認

f:id:m_shige1979:20200224100405p:plain

hello-world

>docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/


>

nginx

> docker run --name nginx1 -d -p 8080:80 nginx


f:id:m_shige1979:20200224101004p:plain

こんな感じかな

所感

Docker ToolBoxと異なるのは別IPでDockerを制御することはないのでlocalhost:xxxxといった感じで扱えることや
設定回りがやりやすくなった点
問題点としてはVirtualBoxと併用できなかったり、Hyper-Vがないと動かせない点とかですかね…
Homeで動かせたらよかったのですが、できなかったのでアップグレードするはめになりました…

自己CA認証局を作成して、SSLページを表示する

chromeとか

もう、オレオレ証明書だけでは見せてくれなくなった(´・ω・`)

CAとかわからないけど

ネットで検索していくつか出たものを参考に作成してみた

準備

opensslの設定ファイルをコピー
mkdir -p /etc/pki/caCrt
cp -p /etc/pki/tls/openssl.cnf /etc/pki/caCrt/.
cp -p /etc/pki/tls/openssl.cnf /etc/pki/caCrt/openssl-ca.cnf
cp -p /etc/pki/tls/openssl.cnf /etc/pki/caCrt/openssl-server.cnf
cp -p /etc/pki/tls/openssl.cnf /etc/pki/caCrt/openssl-client.cnf
CA用の編集(一部追加)
vi /etc/pki/caCrt/openssl-ca.cnf
----
[ usr_cert ]
basicConstraints=CA:TRUE
nsCertType = client, email
subjectAltName=@alt_names
----
サーバ証明書用の編集(一部追加)
vi /etc/pki/caCrt/openssl-server.cnf
----
[ usr_cert ]
basicConstraints = CA:FALSE
nsCertType =  server
subjectAltName=@alt_names
----
クライアント証明書用の編集(一部追加)
vi /etc/pki/caCrt/openssl-client.cnf
----
[ usr_cert ]
basicConstraints=CA:FALSE
nsCertType =  client, email, objsign
----
DNS設定
vi /etc/pki/caCrt/san.txt
----
subjectAltName=DNS:sample.com,DNS:*.sample.com
----

CA(自己認証局)作成

秘密鍵作成
openssl genrsa \
    -aes256 \
    -out cakey.pem 2048
> Enter pass phrase for cakey.pem: [秘密鍵のパスワード]
> Verifying - Enter pass phrase for cakey.pem: [秘密鍵のパスワード]
CSRファイル生成
openssl req -new \
    -config /etc/pki/caCrt/openssl-ca.cnf \
    -key cakey.pem \
    -out cacert.csr
> Enter pass phrase for cakey.pem: [秘密鍵のパスワード]
> You are about to be asked to enter information that will be incorporated
> into your certificate request.
> What you are about to enter is what is called a Distinguished Name or a DN.
> There are quite a few fields but you can leave some blank
> For some fields there will be a default value,
> If you enter '.', the field will be left blank.
> -----
> Country Name (2 letter code) [XX]:JP  ← なんか適当に
> State or Province Name (full name) []:Fukuoka ← なんか適当に
> Locality Name (eg, city) [Default City]:Fukuoka ← なんか適当に
> Organization Name (eg, company) [Default Company Ltd]:OreOre inc. ← なんか適当に
> Organizational Unit Name (eg, section) []: ← ← なんか適当に
> Common Name (eg, your name or your server's hostname) []:localhost ← なんか適当に
> Email Address []:  ← ブランク
> 
> Please enter the following 'extra' attributes
> to be sent with your certificate request
> A challenge password []: ← ブランク
> An optional company name []: ← ブランク
CA証明書作成
openssl x509 \
    -days 3650 \
    -in cacert.csr \
    -req -signkey cakey.pem \
    -out cacert.pem
> Signature ok
> subject=/C=JP/ST=Fukuoka/L=Fukuoka/O=OreOre inc./CN=localhost
> Getting Private key
> Enter pass phrase for cakey.pem: [秘密鍵のパスワード]
認証局管理用のファイルを初期化
touch /etc/pki/CA/index.txt
echo 00 > /etc/pki/CA/serial

サーバ証明書

秘密鍵を作成
openssl genrsa -aes256 -out privkey.pem 2048
> Enter pass phrase for privkey.pem:
> Verifying - Enter pass phrase for privkey.pem:
CSR作成
openssl req \
    -new -key privkey.pem \
    -out domain_name.csr
> Enter pass phrase for privkey.pem: [password]
> You are about to be asked to enter information that will be incorporated
> into your certificate request.
> What you are about to enter is what is called a Distinguished Name or a DN.
> There are quite a few fields but you can leave some blank
> For some fields there will be a default value,
> If you enter '.', the field will be left blank.
> -----
> Country Name (2 letter code) [XX]:JP
> State or Province Name (full name) []:Fukuoka
> Locality Name (eg, city) [Default City]:Fukuoka
> Organization Name (eg, company) [Default Company Ltd]:OreOre inc.
> Organizational Unit Name (eg, section) []:
> Common Name (eg, your name or your server's hostname) []:*.sample.com
> Email Address []:
> 
> Please enter the following 'extra' attributes
> to be sent with your certificate request
> A challenge password []:
> An optional company name []:
サーバ証明書作成
openssl ca \
    -config /etc/pki/caCrt/openssl-server.cnf \
    -keyfile cakey.pem -cert cacert.pem \
    -in domain_name.csr \
    -out domain_name.crt.pem \
    -days 825 \
    -extfile /etc/pki/caCrt/san.txt
> Using configuration from /etc/pki/tls/openssl.cnf
> Enter pass phrase for cakey.pem:
> Check that the request matches the signature
> Signature ok
> Certificate Details:
>         Serial Number: 1 (0x1)
>         Validity
>             Not Before: Feb 15 04:46:21 2020 GMT
>             Not After : May 20 04:46:21 2022 GMT
・
・
・
なんかいろいろでる
サーバ証明書の鍵ファイルよりパスフレーズ除去版を作成
openssl rsa \
    -in privkey.pem \
    -out privkey-nopass.pem
> Enter pass phrase for privkey.pem:
> writing RSA key

クライアント証明書

秘密鍵を作成
openssl genrsa \
    -aes256 \
    -out client-privatekey.pem 2048
> Enter pass phrase for client-privatekey.pem:
> Verifying - Enter pass phrase for client-privatekey.pem:
CSR発行
openssl req \
    -new \
    -key client-privatekey.pem \
    -out client.csr
> Enter pass phrase for client-privatekey.pem:
> You are about to be asked to enter information that will be incorporated
> into your certificate request.
> What you are about to enter is what is called a Distinguished Name or a DN.
> There are quite a few fields but you can leave some blank
> For some fields there will be a default value,
> If you enter '.', the field will be left blank.
> -----
> Country Name (2 letter code) [XX]:JP
> State or Province Name (full name) []:Fukuoka
> Locality Name (eg, city) [Default City]:Fukuoka
> Organization Name (eg, company) [Default Company Ltd]:OreOre inc.
> Organizational Unit Name (eg, section) []:
> Common Name (eg, your name or your server's hostname) []:OreOre client
> Email Address []:
> 
> Please enter the following 'extra' attributes
> to be sent with your certificate request
> A challenge password []:
> An optional company name []:
クライアント証明書作成
openssl ca \
    -config /etc/pki/caCrt/openssl-client.cnf \
    -cert cacert.pem \
    -keyfile cakey.pem \
    -out clientcert.crt \
    -infiles client.csr
> Using configuration from /etc/pki/caCrt/openssl-client.cnf
> Enter pass phrase for cakey.pem:
> Check that the request matches the signature
> Signature ok
> Certificate Details:
>         Serial Number: 2 (0x2)
>         Validity
>             Not Before: Feb 15 05:08:00 2020 GMT
>             Not After : Feb 14 05:08:00 2021 GMT
>         Subject:
・
・
なんかいろいろでる
クライアント証明書をpkcs12でエクスポート
openssl pkcs12 \
    -export \
    -in clientcert.crt \
    -inkey client-privatekey.pem \
    -out clientcertinbrowser.pfx -name "hogehoge"
> Enter pass phrase for client-privatekey.pem: [password]
> Enter Export Password: [ブランク]
> Verifying - Enter Export Password: [ブランク]

nginx設定

初期設定のものを書き換え
vi /etc/nginx/conf.d/default.conf
----
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
server {
    listen       443;
    server_name  dev.sample.com;

    ssl on;
    ssl_certificate /vagrant/domain_name.crt.pem;
    ssl_certificate_key /vagrant/privkey-nopass.pem;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}
----


f:id:m_shige1979:20200215210413p:plain

まあ、これでよいかな・・・

■追記
 webブラウザで証明書のインポートが別途必要