# **2021/07/14** [[2 Running the Docker Container]](https://github.com/dusty-nv/jetson-inference/blob/master/docs/aux-docker.md) [[2.5 Building the Project from Source]](https://github.com/dusty-nv/jetson-inference/blob/master/docs/building-repo-2.md) [[3 Classifying Images with "ImageNet"]](https://github.com/dusty-nv/jetson-inference/blob/master/docs/imagenet-console-2.md) [[4 Coding Your Own Image Recognition Program (Python)]](https://github.com/dusty-nv/jetson-inference/blob/master/docs/imagenet-example-python-2.md) ###### tags: `藍柏婷` ###### tags: `2021/07/14` ### **==== 啟動容器 Launching the Container ====** $ git clone --recursive https://github.com/dusty-nv/jetson-inference >-> 把 https://github.com/dusty-nv/jetson-inference 克隆(clone)進jetson nano裡 $ cd jetson-inference >-> 進入`jetson-inference`這個file $ docker/run.sh >*-> 自動根據你現在的JetPack版本去DockerHub抓取對應的容器 (Contrainer),並且如果你有使用攝影機的話這時候也會自動讀取 -> Docker Container -> 可以想像是一個獨立的虛擬環境,使用者進入容器之後會與你原生系統的檔案區(你的電腦)隔開來,但也是可以透過「掛接」的方式到容器中,像這次如果直接執行run.sh的話,會自動將`jetson-inference/data`掛載到docker的 `/jetson-inference/build/aarch64/bin/` 當中,所以你可以在外部新增或刪除圖片。* $ docker/run.sh --volume /my/host/path:/my/container/path >-> (我不確定跟上一句差在哪) --- ### **==== 運行應用程序 Running Applications ====** # cd build/aarch64/bin >-> 進入`build`這個資料夾 -> 進入`aarch64`這個資料夾 -> 進入`bin`這個資料夾 # ./video-viewer /dev/video0 >-> 引進鏡頭 # ./imagenet images/jellyfish.jpg images/test/jellyfish.jpg >-> 在當前目錄 `imagenet` 中,找 `images` 下的 `jellyfish.jpg` 照片當輸入,輸出存成 `images` 下 `test` 下的 `jellyfish.jpg`。 # ./detectnet images/peds_0.jpg images/test/peds_0.jpg >-> 跟上句差不多意思 # 按 Ctrl+D 退出容器 >-> 即退出#的輸入,回歸$的輸入 --- ### **==== 構建容器 Building the Container ====** $ docker/build.sh >-> 建立一個 `build.sh` 的資料夾 --- ### **==== 從源代碼構建項目 Building the Project from Source ====** #### **== 不使用docker的構建、安裝命令 Quick Reference ==** $ sudo apt-get update >-> 更新 $ sudo apt-get install git cmake libpython3-dev python3-numpy >-> 安裝 `git cmake libpython3-dev python3-numpy` $ git clone --recursive https://github.com/dusty-nv/jetson-inference $ cd jetson-inference $ mkdir build $ cd build $ cmake ../ $ make -j$(nproc) $ sudo make install $ sudo ldconfig >-> (這段我不確定有沒有打) #### **== Python開發包 Python Development Packages ==** $ sudo apt-get install libpython3-dev python3-numpy >-> 綁定(安裝)Python 3.6 開發包 #### **== 使用CMake進行配置 Configuring with CMake ==** $ cd jetson-inference >-> 進入`jetson-inference`這個資料夾 $ mkdir build >-> 在`jetson-inference`這個資料夾中建立一個新的項目`build` $ cd build >-> 進入`build`這個資料夾 $ cmake ../ >-> python的一種語法,可以自動運作一個工具(?) #### **== 下載模型 Downloading Models ==** (CMake會幫你自動跑出一個綠色框框,直接按OK就行) #### **== 安裝PyTorch(Installing PyTorch) ==** (安裝3.6版) #### **== 編譯項目 Compiling the Project ==** $ cd jetson-inference/build $ make $ sudo make install $ sudo ldconfig >`$ cd jetson-inference/build` -> 進入build資料夾中 `$ make` -> 運行make `$ sudo make install` -> 安裝make `$ sudo ldconfig` -> ldconfig是一個動態鏈接庫管理命令,為了讓動態鏈接庫為系統所共享。ldconfig通常在系統啟動時運行,而當用戶安裝了一個新的動態鏈接庫時,就需要手工運行這個命令。 (應用程序就能正確找到make相關鏈接文件了?) --- ### **==== 使用 ImageNet 對圖像進行分類 Classifying Images with ImageNet ====** $ cd jetson-inference/build/aarch64/bin >-> 先確保現在在`bin`中 $ ./imagenet.py images/orange_0.jpg images/test/output_0.jpg >`./imagenet.py` -> 在現在的資料中(bin)找 `imagenet.py` `images/orange_0.jpg` -> 在`images`中找`orange_0.jpg`(範例圖片) `images/test/output_0.jpg` -> 在`images`中的`test`中放入output的結果`output_0.jpg`(範例輸出)(記得每輸出一次,就要改一次output_的那個數字) ** 總之,只有 `orange_0.jpg` & `output_0.jpg` 可以更改 ** --- ### **==== 編寫自己的圖像識別程序(Python) ====** #### **== 設置項目 ==** # run these commands outside of container $ cd ~/ $ mkdir my-recognition-python $ cd my-recognition-python $ touch my-recognition.py $ chmod +x my-recognition.py $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/black_bear.jpg $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/brown_bear.jpg $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/polar_bear.jpg $ docker/run.sh --volume ~/my-recognition-python:/my-recognition-python (先到 `/my-recognition-python`中再輸入) ****更正!!!進入`jetson-inference`再執行 #### **== 源代碼 ==** 在`jetson-inference/python/examples`中會有一個`my-recognition.py`,點開就是以下的程式碼 ```python= #!/usr/bin/python3 import jetson.inference import jetson.utils import argparse # parse the command line parser = argparse.ArgumentParser() parser.add_argument("filename", type=str, help="filename of the image to process") parser.add_argument("--network", type=str, default="googlenet", help="model to use, can be: googlenet, resnet-18, ect. (see --help for others)") opt = parser.parse_args() # load an image (into shared CPU/GPU memory) img = jetson.utils.loadImage(opt.filename) # load the recognition network net = jetson.inference.imageNet(opt.network) # classify the image class_idx, confidence = net.Classify(img) # find the object description class_desc = net.GetClassDesc(class_idx) # print out the result print("image is recognized as '{:s}' (class #{:d}) with {:f}% confidence".format(class_desc, class_idx, confidence * 100)) ``` >```python=3 >import jetson.inference >``` >-> import相當於c++的include > >```python=8 ># parse the command line >parser = argparse.ArgumentParser() >parser.add_argument("filename", type=str, help="filename of the image to process") >parser.add_argument("--network", type=str, default="googlenet", help="model to use, can be: googlenet, resnet-18, ect. (see --help for others)") >opt = parser.parse_args() >``` >-> 解析命令行 > >```python=14 ># load an image (into shared CPU/GPU memory) >img = jetson.utils.loadImage(opt.filename) >``` >-> 從磁盤加載圖像,使用loadImage()的功能將圖像opt.filename加載至共享的 CPU/GPU 內存中。 > >```python=17 ># load the recognition network >net = jetson.inference.imageNet(opt.network) >``` >-> 加載識別網絡,使用imageNet()加載opt.network (GoogleNet) > >```python=20 ># classify the image >class_idx, confidence = net.Classify(img) >``` >-> 對圖像進行分類,使用Classify()辨識出類別及信心度 > >```python=23 ># find the object description >class_desc = net.GetClassDesc(class_idx) >``` >-> 找到對象描述 > >```python=26 ># print out the result >print("image is recognized as '{:s}' (class #{:d}) with {:f}% confidence".format(class_desc, class_idx, confidence * 100)) >``` >-> 打印出結果 #### **== 運行示例 ==** $ ./my-recognition.py polar_bear.jpg (先到`/build/aarch64/bin`中再輸入)