# 使用 Raspberry Pi Camera 和 AWS Greengrass + QIoT + QuAI進行對象檢測範例 (中文) 準備事項 : 1. AWS 帳號 2. Raspberry Pi 3. USB 網路攝影機 ## 應用範例-1 * USB 網路攝影機 --> AWS Greengrass IoT 裝置 --> AWS Greengrass 核心 --> QIoT Suite Lite --> QuAI --> AWS Greengrass 核心 Lambda --> AWS Cloud --> AWS S3 儲存貯體  ### 步驟-1 : 設定 AWS Greengrass 核心 1. 從App Center安裝AWS Greengrass APP  2. 在QNAP AWS Greengrass App中設定您的AWS Greengrass 群組&核心。 有關詳細信息,請參閱此連結 : https://www.qnap.com/en/how-to/tutorial/article/how-to-setup-aws-greengrass-on-qnap-nas/ 3. 建立AWS Greengrass Lambda : **"SendGGImageToQIoT"** & **"QIoTIntegration"** 。對於本範例,我們使用基於Node.js的Lmabda函數,您還應該更新它的Memeory限制和Timeout設定。可以再此資料夾中找到Demo Lambda的原始碼 : [AWS_Greengrass_Lambda](https://github.com/qnap-dev/qnap-qiot-sdks/tree/master/projects/AWSGreengrass-Integration-Scenarios/Greengrass_device_QIoT_QuAI/AWS_Greengrass_Lambda). (建立Lambda function請參考AWS說明文件 : https://docs.aws.amazon.com/greengrass/latest/developerguide/create-lambda.html)   4. 在Greengrass 群組裝置部分中建立一個新裝置,如下圖所示 : [https://docs.aws.amazon.com/greengrass/latest/developerguide/device-group.html](https://docs.aws.amazon.com/greengrass/latest/developerguide/device-group.html) **<font color=red># 注意:請保留您的裝置證書,解壓縮並上傳到您的Raspberry Pi</font>**  5. 建立以下3個訂閱列表 : * Greengrass IoT 裝置到 **"SendGGImageToQIoT:9"** Lambda,此規則將攝影畫面發送給此 Lambda 函數送到QIoT Suite Lite轉送給QuAI進行圖像檢測。設定資訊 : * 來源 --> **GG_Camera** * 目標 --> **SendGGImageToQIoT** * 主題 --> **cameraImage** * 此 **"QIoTIntegration"** Lambda 函數是將圖片跟辨識結果上傳到 IoT Cloud 與 S3。設定資訊 : * 來源 --> **QIoTIntegration** * 目標 --> **IoT Cloud** * 主題 --> **#** * Greengrass IoT 裝置到 **"QIoTIntegration:16"** Lambda,此規則是利用Greengrass IoT 裝置觸發此Lambda函數執行。設定資訊 : * 來源 --> **GG_Camera** * 目標 --> **QIoTIntegration** * 主題 --> **triggerLambda** 有關這3個訂閱列表,目標和主題詳細信息,請參閱下圖 :  6. 部署 Greengrass 群組. </span> 7. 在 QNAP Greengrass App 中驗證 Greengrass 核心守護程序狀態  8. 要觸發 **QIoTIntegration** Lambda 函數來接收 QIoT Suite Lite 訊息,您必須下載 AWS IoT Python SDK --> [**basicDiscovery.py**](https://github.com/aws/aws-iot-device-sdk-python/blob/master/samples/greengrass/basicDiscovery.py) 並使用 **GG_Camera** 裝置證書文件來執行以下命令 : ```bash python basicDiscovery.py -e <youtiothostname>.iot.<region>.amazonaws.com -r root-ca.pem -c XXXXXXXX.cert.pem -k XXXXXXXX.private.key -n GG_Camera -m publish -t triggerLambda -M "{'status':'start'}" ``` - **-e** : Aws IoT 終端節點(在 **AWS IoT** 主頁**設定**下,記下**終端節點**的值) - **-r** : 根證書路徑 (In here : https://docs.aws.amazon.com/iot/latest/developerguide/managing-device-certs.html) - **-c** : IoT 裝置證書路徑 - **-k** : IoT 裝置金鑰路徑 - **-n** : IoT 裝置名稱 ### 步驟-2 : 設定 AWS Greengrass 裝置 安裝 **AWSIoTPythonSDK** 軟體自您的 Raspberry Pi 中 **(參考此說明文件來安裝 : https://docs.aws.amazon.com/greengrass/latest/developerguide/IoT-SDK.html)** 並且從 [**RaspberryPi_side**](https://github.com/qnap-dev/qnap-qiot-sdks/tree/master/projects/AWSGreengrass-Integration-Scenarios/Greengrass_device_QIoT_QuAI/RaspberryPi_side)上傳 **"send_image_AWSGG.py"** 到您的 Raspberry Pi 中,以及輸入此命令安裝影像擷取軟體 : ```bash sudo apt update && sudo apt install fswebcam ``` ### 步驟-3 : 設定示範 QuAI 1. 開啟NAS上的Container Station 2. 切換到 **Crtare** 頁面,於搜尋列搜尋 **"qeekdev/aipredict"** 3. 點選 **Install** 進行安裝  4. 點擊 **Advanced Settings** 進入進階設定頁面  5. 切換到 **Network** 頁面設定Port,參考下圖設定(若您有服務與此Port相衝突到,請自行更換),完成後請點擊 **Create** 建立容器 :  4. 建立完成後,位於左邊的 **Overview** 功能項目裡,可檢視剛建立完成的新容器,該容器功能為AI辨識照片的服務。   5. 瀏覽器進入 http://\<IP\>:8082/api/v1/ 進行測試,操作結果如下 :   ***其他佈署資訊可參考Dockerhub上的頁面 : https://hub.docker.com/r/qeekdev/aipredict/*** ### 步驟-4 : 設定 QIoT Suite Lite 在 QIoT Suite Lite 中建立一個新的 IoT 應用,從 IoT 應用匯出文件 [**LiveDemo.json**](https://github.com/qnap-dev/qnap-qiot-sdks/tree/master/projects/AWSGreengrass-Integration-Scenarios/Greengrass_device_QIoT_QuAI/QIoT_IoT_App) 匯入到 QIoT Suite Lite中, 或者您可以自己建立一個新的 IoT 應用,可以按照以下步驟建立 : **<font color=red># 注意:您需要在QIoT Suite Lite規則中安裝 “[node-red-contrib-file-upload](https://flows.nodered.org/node/node-red-contrib-file-upload)” 模組,安裝方式參考 : [How to install other modules in QIoT Suite Lite NodeRED?](https://www.qnap.com/go/how-to/tutorial/article/how-to-install-other-modules-in-qiot-suite-lite-nodered)</font>** 1. 建立 IoT 應用和2個 IoT 裝置 : **"cameraPi"** 和 **"AWSgreengrass"**  2. 建立裝置資源 : **cameraPi-->"image"** and **AWSgreengrass-->"resolve"**   3. 匯入 [**rulesJson.json**](https://github.com/qnap-dev/qnap-qiot-sdks/tree/master/projects/AWSGreengrass-Integration-Scenarios/Greengrass_device_QIoT_QuAI/QIoT_IoT_App) 到Node-red中,切換到 Rules 頁面 --> 匯入 --> 剪貼簿,您將會看見兩個流程匯入   4. 驗證您的儀表板 * 點擊下圖紅框處測試 Dashboard 、 AI Sample Container 和 AWS Greengrass是否有正常運作  * 觀看右側Debug欄位是否有出現正常log  * 切換到Dashboard頁面是否有呈現數值及照片  ### 步驟-5 : 設定 AWS cloud S3 與規則 1. 建立 **MoveImageToS3** Node.js Lambda 函數在 AWS Lambda 服務. **(請在此資料夾中找到 Demo Lambda 的原始碼 [AWS_Greengrass_Lambda](https://github.com/qnap-dev/qnap-qiot-sdks/tree/master/projects/AWSGreengrass-Integration-Scenarios/Greengrass_device_QIoT_QuAI/AWS_Greengrass_Lambda))**(建立Lambda 函數請參考AWS說明文件 : https://docs.aws.amazon.com/greengrass/latest/developerguide/create-lambda.html) 2. 建立一個新的 S3 儲存貯體 **"qiotquaiggdemo"** 在 AWS S3 服務上 (建立 S3 儲存貯體請參考AWS說明文件 : https://aws.amazon.com/s3/getting-started/?nc1=h_ls) 3. 參考下圖修改 Lambda 函數中的 S3 儲存貯體設定,依據您的 AWS IAM 及 S3 儲存貯體上的設定填入 accessKeyId、secretAccessKey、Bucket 等資訊:  4. 建立一個行動(規則)在 AWS IoT 中,使用Lambda 函數上傳圖片到 S3 儲存貯體,請選擇 **"叫用傳遞訊息資料的 Lambda 函數"**   5. 在下拉選單中選擇 **MoveImageToS3** 並點擊 **更新**   ### 步驟-6 : 啟動 demo 在 Raspberry Pi 中通過執行以下命令啟動程式: ```bash python send_image_AWSGG.py -e <host>.iot.<region>.amazonaws.com -r root.ca.pem -c <GG_Camrea_Cert_pem_file> -k GG_Camrea_Cert_private_key_file -n GG_Camera -m publish -t "cameraImage" ``` - **-e** : Aws IoT 終端節點(在 **AWS IoT** 主頁**設定**下,記下**終端節點**的值) - **-r** : 根證書路徑 (In here : https://docs.aws.amazon.com/iot/latest/developerguide/managing-device-certs.html) - **-c** : IoT 裝置證書路徑 - **-k** : IoT 裝置金鑰路徑 - **-n** : IoT 裝置名稱 ### 步驟-7 : 驗證 demo 1. 在 QIoT Suite Lite 儀錶板 :  2. 在 AWS IoT Cloud :  3. 在 AWS S3 :  ## 應用範例-2 * USB 網路攝影機 --> QIoT Suite Lite IoT 裝置 --> QIoT Suite Lite --> QuAI --> AWS Greengrass 核心 Lambda --> AWS Cloud --> S3 儲存貯體  此方案中的步驟與**應用範例-1**相同,只是在Raspberry Pi上運行的應用程序不同。 請參考 **[QIoT_device_QuAI_Greengrass](https://github.com/qnap-dev/qnap-qiot-sdks/tree/master/projects/AWSGreengrass-Integration-Scenarios/QIoT_device_QuAI_Greengrass/)** 設定您的裝置。 參考步驟 : 1. 安裝依賴庫和軟體 ```bash pip install paho-mqtt sudo apt update && sudo apt install fswebcam ``` 2. 跟隨步驟 **應用範例-1**--> **"步驟-1"** to **"步驟-5"** 3. 上傳 **[RaspberryPi_side](https://github.com/qnap-dev/qnap-qiot-sdks/tree/master/projects/AWSGreengrass-Integration-Scenarios/QIoT_device_QuAI_Greengrass/RaspberryPi_side)** 資料夾到 Raspberry Pi 4. 至 QIoT Suite Lite **cameraPi** IoT裝置下載 **"resourceinfo.json"** ,並且上傳 **resourceinfo.json** 到 **"/res"** 資料夾下 5. 執行命令 : ```bash python main.py ``` 6. 跟隨步驟 **應用範例-1**--> **"步驟-7"** ###### tags: `Tutorial`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up