# 如何在TS-253B使用AWS Greengrass ML
## 準備工作
1. 準備USB WebCam並接上NAS
2. 請先完成AWS Greengrass基本設定,參考此教學文章 :
[如何在 QNAP NAS 設定 AWS Greengrass](https://www.qnap.com/zh-tw/how-to/tutorial/article/%E5%A6%82%E4%BD%95%E5%9C%A8-qnap-nas-%E8%A8%AD%E5%AE%9A-aws-greengrass/)
## 安裝MXNet
1. 參照[AWS Greengrass ML文件 : https://docs.aws.amazon.com/greengrass/latest/developerguide/ml-console.html](https://docs.aws.amazon.com/greengrass/latest/developerguide/ml-console.html)步驟二下載MXNet Intel平台軟體,上傳至NAS Public資料夾:

2. SSH進入NAS,移動安裝檔並解壓縮(依據安裝磁碟區不同路徑可能也會有所不同)
```bash
cd /share/CACHEDEV1_DATA/.qpkg/Greengrass/package/greengrass
mv /share/Public/ggc-mxnet-v1.2.1-python-intel.tar .
tar -xzf ggc-mxnet-v1.2.1-python-intel.tar
```
3. 進入Greengrass Container安裝MXNet
```bash
system-docker exec -ti greengrass bash
cd greengrass/ggc-mxnet-v1.2.1-python-intel/
apt update && apt install sudo unzip
./mxnet_installer.sh -u
```
## 執行範例
### 編輯範例程式
1. 參照[AWS Greengrass ML文件](https://docs.aws.amazon.com/greengrass/latest/developerguide/ml-console.html)步驟二下載MXNet Raspberry平台(執行範例會用到)軟體
2. 解壓縮`ggc-mxnet-v1.2.1-python-raspi.tar.gz`,在解壓縮`greengrass-ml-squeezenet-object-classification-raspi-python.tar.gz`
3. 編輯`load_model.py`,將底下註釋掉 :
```python=2.7
'''
#Captures an image from the PiCamera, then sends it for prediction
def predict_from_cam(self, capfile='cap.jpg', reshape=(224, 224), N=5):
if self.camera is None:
self.camera = picamera.PiCamera()
stream = io.BytesIO()
self.camera.start_preview()
time.sleep(2)
self.camera.capture(stream, format='jpeg')
# Construct a numpy array from the stream
data = np.fromstring(stream.getvalue(), dtype=np.uint8)
# "Decode" the image from the array, preserving colour
image = cv2.imdecode(data, 1)
return self.predict_from_image(image, reshape, N)
'''
```
並新增 :
```python=2.7
#Captures an image from the webcam, then sends it for prediction
def predict_from_webcam(self, capfile='cap.jpg', reshape=(224, 224), N=5):
print "capture.......\n"
cap = cv2.VideoCapture(0)
flag, image = cap.read()
print "read.......\n"
return self.predict_from_image(image, reshape, N)
```
完成後儲存
4. 參照[AWS Greengrass ML文件](https://docs.aws.amazon.com/greengrass/latest/developerguide/ml-console.html)步驟五建立Lambda Function
### 編輯Greengrass Group
1. 在**Group**設定頁面上,選擇**Resource**
2. 在**Local Resources**選項中,選擇**Add local resource**
3. 在**Create a local resource**頁面上,使用以下值:
| Property | Value |
| -------- | -------- |
| Resource name | video |
| Resource type | Device |
| Device path | /dev/video0 |
| Group owner file access permission | Automatically add OS group permissions of the Linux group that owns the resource|

4. 在**Lambda function affiliations**下,點擊**Select**,選擇**greengrassObjectClassification**,選擇**Read and write access**訪問權限,然後點擊**Done**

5. 剩下上傳model步驟參考[AWS Greengrass ML文件](https://docs.aws.amazon.com/greengrass/latest/developerguide/ml-console.html) **uploading the squeezenet.zip model package to Amazon S3.** 段落操作
6. 設定Subscription跟Deploy也請參考:[AWS Greengrass ML文件](https://docs.aws.amazon.com/greengrass/latest/developerguide/ml-console.html)步驟8、9,以及後續測試步驟

## (其他)設定AWS Greengrass停止重啟MXNet Library資料不會被移除
預設只要在NAS重開機或是在App Center停止後,Greengrass Container就會被刪除,暫時不被刪除方法請參考如下步驟 :
1. 修改`/share/CACHEDEV1_DATA/.qpkg/Greengrass/Greengrass.sh`檔案(依據安裝磁碟區不同路徑可能也會有所不同) :
* 將Start區間的這行:
```bash
COMPOSE_HTTP_TIMEOUT=300 ${DOCKER_COMPOSE} -p $PROJECT_NAME -f $QPKG_PATH/docker-compose.yml up -d >> $QPKG_PATH/compose.log 2>&1
```
改成 :
```bash
COMPOSE_HTTP_TIMEOUT=300 ${DOCKER_COMPOSE} -p $PROJECT_NAME -f $QPKG_PATH/docker-compose.yml up --no-recreate -d >> $QPKG_PATH/compose.log 2>&1
```
* 以及Stop區間這行 :
```bash
COMPOSE_HTTP_TIMEOUT=300 $DOCKER_COMPOSE -p $PROJECT_NAME -f $QPKG_PATH/docker-compose.yml down >> $QPKG_PATH/compose_down.log 2>&1
```
改成 :
```bash
COMPOSE_HTTP_TIMEOUT=300 $DOCKER_COMPOSE -p $PROJECT_NAME -f $QPKG_PATH/docker-compose.yml stop >> $QPKG_PATH/compose_down.log 2>&1
```
2. 保存後即可
###### tags: `Tutorial`