# 人工智慧

## 自訂模型識別

```python
from AI import CustomModel
```

### 模型初始化
```python
CustomModel(labels,path)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| labels | 列表 | 標籤列表，必填 |
| path | 字串 | 模型路徑，必填 |

### 載入識別
```python
loadModel(img)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| img | 圖片 | 要識別的Image對象，必填 |

### 樣例
```python
from cocopiPython import cameraPython,Screen
from AI import CustomModel

Screen = Screen() # 畫布初始化
camera = cameraPython() # 攝像頭初始化

labels = ["load", "unload", "left", "right", "forward", "stop"] # 標籤
path = "/root/preset/model/autologistics" # 模型路徑
CustomModel = CustomModel(labels,path) # 初始化模型

while True:
    canvas = camera.capture() # 獲取攝像頭圖像
    canvas = canvas.crop(48,8,224,224) # 裁剪圖像為224*224
    data = CustomModel.load(canvas) # 載入模型進行識別
    for i in data:
        canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=(255,0,0),thickness=1) # 在螢幕繪製檢測框
        canvas.draw_string(i["X"],i["Y"]-20,(str(i["name"])+":"+str(i["score"])),color=(255,0,0)) # 在螢幕上繪製名稱與置信度
    Screen.show(canvas,48,8) # 顯示畫布
```
其中i["X"]表示識別結果的檢測框起點X軸坐標，i["Y"]表示識別結果的檢測框起點Y軸坐標，i["W"]表示識別結果的檢測框寬度，i["H"]表示識別結果的檢測框高度，i["name"]表示識別結果的名稱，i["score"]表示識別結果的置信度，i["middleX"]表示識別結果的檢測框中心點的X軸坐標，i["middleY"]表示識別結果的檢測框中心點的Y軸坐標。

## 邊緣檢測

```python
from AI import EdgeDetection
```

### 模型初始化
```python
EdgeDetection()
```

### 載入識別
```python
loadModel(img)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| img | 圖片 | 要識別的Image對象，必填 |

### 樣例
```python
from cocopiPython import cameraPython,Screen
from AI import EdgeDetection

Screen = Screen() # 畫布初始化
camera = cameraPython() # 攝像頭初始化
EdgeDetection = EdgeDetection() # 初始化

while True:
    img = camera.capture() # 獲取攝像頭資料
    img = img.crop(48,8,224,224) # 裁剪尺寸為224*224
    canvas = EdgeDetection.load(img) # 載入模型
    Screen.show(canvas,48,8) # 顯示畫布
```

## 人臉檢測

```python
from AI import Facedetection
```

### 模型初始化
```python
Facedetection()
```

### 載入識別
```python
loadModel(img)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| img | 圖片 | 要識別的Image對象，必填 |

### 樣例
```python
from cocopiPython import cameraPython,Screen
from AI import Facedetection

Screen = Screen() # 畫布初始化
camera = cameraPython() # 攝像頭初始化
face = Facedetection() # 人臉檢測初始化

while True:
    canvas = camera.capture() # 獲取攝像頭圖像
    canvas = canvas.crop(48,8,224,224) # 裁剪圖像尺寸
    data = face.load(canvas) # 載入人臉檢測模型
    for i in data:
        canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=(255,0,0), thickness=1) # 在螢幕上繪製人臉檢測框
        canvas.draw_string(5,5,str(len(data)), scale = 1, color = (255,255,255) , thickness = 1) # 在螢幕上顯示人臉數量
    Screen.show(canvas,48,8) # 顯示畫布
```

## 人臉識別

```python
from AI import FaceRecognition
```

### 模型初始化
```python
FaceRecognition()
```

### 載入模型
載入人臉檢測模型，獲取人臉資訊
```python
loadmodel(img)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| img | 圖片 | 要識別的Image對象，必填 |

### 添加人臉
錄入人臉資訊保存到本地
```python
add(name,feature,dataPath)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| name | 字串 | 識別到的人臉名稱，必填 |
| feature | 列表 | 人臉資料，必填 |
| dataPath | 字串 | 保存路徑，選填，預設為"/root/user/model/recorded_face_features.py" |

### 刪除人臉
刪除本地指定人臉資料
```python
remove(name,dataPath)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| name | 字串 | 要刪除的人臉名稱，必填 |
| dataPath | 字串 | 保存路徑，選填，預設為"/root/user/model/recorded_face_features.py" |

### 載入人臉信息
載入人臉採集時保存到本地的人臉資訊資料
```python
loadface(DataPath)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| dataPath | 字串 | 保存路徑，選填，預設為""/root/user/model/recorded_face_features.py" |

### 樣例

#### 人臉採集
```python
import time
from cocopiPython import cameraPython,Screen
from AI import FaceRecognition
from CocoPi import BUTTON

# 按鈕初始化
key_C = BUTTON(13)
key_D = BUTTON(7)

Screen = Screen()
camera = cameraPython()

FaceRecognition = FaceRecognition()
People_list = ["小明","小紅","小剛","小花"]

FaceNum = 0
faceScore = 0
color = (255,0,0)

while True:
    canvas = camera.capture() # 獲取攝像頭資料
    canvas = canvas.crop(48,8,224,224) # 裁剪圖片尺寸
    data = FaceRecognition.loadmodel(canvas) # 載入人臉檢測模型
    for i in data:
        canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=color,thickness=1) # 檢測框
        if key_C.is_pressed() and FaceNum < len(People_list):
            while not key_C.is_pressed()== False:
                time.sleep(0.01)
            FaceRecognition.add(People_list[len(FaceRecognition)],i["feature"]) # 錄入人臉保存
            canvas.draw_rectangle(0,0,320,20,color=(0,0,0),thickness=-1)
            canvas.draw_string(0,0,"已採集"+str(People_list[FaceNum])+"人臉數據",scale=1,color=(255,255,255),thickness=1)
            Screen.show(canvas,48,8)
            FaceNum = FaceNum + 1
            time.sleep(1)
    if key_D.is_pressed() and FaceNum > 0:
        while not key_D.is_pressed()== False:
            time.sleep(0.01)
        FaceRecognition.remove(People_list[len(FaceRecognition)-1]) # 刪除人臉
        canvas.draw_rectangle(0,0,320,20,color=(0,0,0),thickness=-1)
        canvas.draw_string(0,0,"已刪除"+str(People_list[FaceNum-1])+"人臉數據",scale=1,color=(255,255,255),thickness=1)
        Screen.show(canvas,48,8)
        FaceNum = FaceNum - 1
        time.sleep(1)
    Screen.show(canvas,48,8)
```

#### 人臉識別
```python
from cocopiPython import cameraPython,Screen
from AI import FaceRecognition

Screen = Screen()
camera = cameraPython()
FaceRecognition = FaceRecognition()

faceScore = 0
color = (255,0,0)
    
while True:
    canvas = camera.capture() # 獲取攝像頭資料
    canvas = canvas.crop(48,8,224,224) # 裁剪圖片尺寸
    data = FaceRecognition.loadmodel(canvas) # 載入人臉檢測模型
    FaceRecognition.loadface()
    for i in data:
        faceScore = round(i["recognizeScore"],2)
        canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=color,thickness=1) # 檢測框
        canvas.draw_string(i["X"],i["Y"],str(i["name"])+":"+str(faceScore),scale=1,color=color,thickness=1)
        if faceScore > 80:
            color = (0,255,0)
        else:
            color = (255,0,0)
    Screen.show(canvas,48,8)
```

#### 人臉採集並識別
```python
import time
from cocopiPython import cameraPython,Screen
from AI import FaceRecognition
from CocoPi import BUTTON

# 按鈕初始化
key_C = BUTTON(13)
key_D = BUTTON(7)

Screen = Screen()
camera = cameraPython()

FaceRecognition = FaceRecognition()
People_list = ["小明","小紅","小剛","小花"]

FaceNum = 0
faceScore = 0
color = (255,0,0)

while True:
    canvas = camera.capture() # 獲取攝像頭圖像
    canvas = canvas.crop(48,8,224,224) # 裁剪圖片尺寸
    data = FaceRecognition.loadmodel(canvas) # 載入人臉檢測模型
    for i in data:
        canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=color,thickness=1) # 檢測框
        if key_C.is_pressed() and FaceNum < len(People_list):
            while not key_C.is_pressed()== False:
                time.sleep(0.01)
            FaceRecognition.add(People_list[len(FaceRecognition)],i["feature"]) # 錄入人臉保存
            canvas.draw_rectangle(0,0,320,20,color=(0,0,0),thickness=-1)
            canvas.draw_string(0,0,"已採集"+str(People_list[FaceNum])+"人臉數據",scale=1,color=(255,255,255),thickness=1)
            Screen.show(canvas,48,8)
            FaceNum = FaceNum + 1
            time.sleep(1)
        faceScore = round(i["recognizeScore"],2)
        if faceScore > 70:
            color = (0,255,0)
            canvas.draw_string(i["X"],i["Y"],str(i["name"])+":"+str(faceScore),scale=1,color=color,thickness=1)
        else:
            color = (255,0,0)
    if key_D.is_pressed() and FaceNum > 0:
        while not key_D.is_pressed()== False:
            time.sleep(0.01)
        FaceRecognition.remove(People_list[len(FaceRecognition)-1]) # 刪除人臉
        canvas.draw_rectangle(0,0,320,20,color=(0,0,0),thickness=-1)
        canvas.draw_string(0,0,"已刪除"+str(People_list[FaceNum-1])+"人臉數據",scale=1,color=(255,255,255),thickness=1)
        Screen.show(canvas,48,8)
        FaceNum = FaceNum - 1
        time.sleep(1)
    Screen.show(canvas,48,8)
```

## 數位識別

```python
from AI import Digitalecognition
```

### 模型初始化
```python
Digitalecognition()
```

### 載入識別
```python
loadModel(img)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| img | 圖片 | 要識別的Image對象，必填 |

### 樣例
```python
from cocopiPython import cameraPython,Screen
from AI import Digitalecognition

Screen = Screen() # 畫布初始化
camera = cameraPython() # 攝像頭初始化
Digit = Digitalecognition() # 人臉檢測初始化

while True:
    canvas = camera.capture() # 獲取攝像頭圖像
    canvas = canvas.crop(48,8,224,224) # 裁剪圖像尺寸
    data = Digit.load(canvas)
    for i in data:
        canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=(255,0,0),thickness=1)
        canvas.draw_string(i["X"],i["Y"]-20,(str(i["name"])+":"+str(i["score"])),color=(255,0,0))
    Screen.show(canvas,48,8)
```

## 物體檢測

```python
from AI import ObjectDetection
```

### 模型初始化
```python
ObjectDetection()
```

### 載入識別
```python
loadModel(img)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| img | 圖片 | 要識別的Image對象，必填 |

### 樣例
```python
from cocopiPython import cameraPython,Screen
from AI import ObjectDetection

Screen = Screen() # 畫布初始化
camera = cameraPython() # 攝像頭初始化
object = ObjectDetection() # 物體檢測初始化

while True:
    canvas = camera.capture() # 獲取攝像頭圖像
    canvas = canvas.crop(48,8,224,224) # 裁剪圖像尺寸
    data = object.load(canvas)
    for i in data:
        canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=(255,0,0),thickness=1)
        canvas.draw_string(i["X"],i["Y"]-20,(str(i["name"])+":"+str(i["score"])),color=(255,0,0))
    Screen.show(canvas,48,8)
```

## 猜拳識別

```python
from AI import RockPaperScissors
```

### 模型初始化
```python
RockPaperScissors()
```

### 載入識別
```python
loadModel(img)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| img | 圖片 | 要識別的Image對象，必填 |

### 樣例
```python
from cocopiPython import cameraPython,Screen
from AI import RockPaperScissors

Screen = Screen()
camera = cameraPython()
RockPaperScissors = RockPaperScissors()

while True:
    canvas = camera.capture()
    canvas = canvas.crop(48,8,224,224)
    data = RockPaperScissors.loadModel(canvas)
    for i in data:
        canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=(255,0,0),thickness=1)
        canvas.draw_string(i["X"],i["Y"]-10,(str(i["name"])+":"+str(i["score"])),color=(255,0,0))
    Screen.show(canvas,48,8)
```

## 自學習識別

```python
from AI import SelfLearning
```

### 模型初始化
```python
SelfLearning(lables)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| lables | 清單 | 要識別的物體名稱清單，必填 |

### 添加类别
```python
addClass(img)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| img | 圖片 | 要識別的Image對象，必填 |

### 訓練
```python
trainSave(path)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| path | 字串 | 保存路徑，選填，預設為“/root/user/model/module.bin” |

### 載入識別
```python
loadModel(img,path)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| img | 圖片| 要識別的Image對象，必填 |
| path | 字串 | 保存路徑，選填，預設為“/root/user/model/module.bin” |

### 樣例

#### 採集
```python
import time
from cocopiPython import cameraPython,Screen
from AI import SelfLearning
from CocoPi import BUTTON

key_C = BUTTON(13)
key_D = BUTTON(7)

Screen = Screen()
camera = cameraPython()
lables=(['滑鼠','鍵盤','水杯'])
SL = SelfLearning(lables)

is_train = 0
count = 0
str_display = "按C鍵添加類別，按D鍵進行學習"

while True:
    # 獲取攝像頭資料
    canvas = camera.capture()
    canvas = canvas.crop(48,8,224,224)
    if key_C.is_pressed():
        while not key_C.is_pressed()== False:
            time.sleep(0.01)
        if count < len(lables):
            # 採集資料添加到清單中
            time.sleep(1)
            SL.addClass(canvas)
            count += 1
    if key_D.is_pressed():
        while not key_D.is_pressed()== False:
            time.sleep(0.01)
        if count >= len(lables) and is_train == 0:
            time.sleep(1)
            # 訓練並保存，參數：path 為保存路徑預設/root/user/model/module.bin
            SL.train()
            is_train = 1
            str_display = "學習已完成"
    canvas.draw_string(0,0,str_display,scale=1,color=(255,0,0),thickness=1)
    canvas.draw_string(0,16,"採集次數："+str(count),scale=1,color=(255,0,0),thickness=1)
    Screen.show(canvas,48,8)
```
#### 識別
```python
from cocopiPython import cameraPython,Screen
from AI import SelfLearning

Screen = Screen()
camera = cameraPython()
SL= SelfLearning(['滑鼠','鍵盤','水杯'])
SL.load()

while True:
    # 獲取攝像頭資料
    img = camera.capture()
    canvas = img.crop(48,8,224,224)
    data = SL.predict(canvas)
    canvas.draw_string(0,5,(str(data["name"])+":"+str(data["score"])),scale=1,color=(255,0,0),thickness=1)
    Screen.show(canvas,48,8)
```

## 拼音識別

```python
from AI import PinyinRecognition
```

### 模型初始化
```python
PinyinRecognition(keywords)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| keywords | 清單 | 拼音參數，文字的拼音與音調，必填 |

### 獲取結果
```python
getresult()
```

### 樣例
```python
from cocopiPython import Screen
from AI import PinyinRecognition

Screen = Screen()
canvas = Screen.create()

keywords = [["ni3 hao3",0.1],["hao3 de1",0.1],["wan3 shang4 hao3",0.1]] # 設置拼音參數
PinyinRecognition = PinyinRecognition(keywords)

data =""
while True:
    canvas.clear()
    data = str(PinyinRecognition.getResult()) # 獲取拼音識別結果
    print("jieguo:"+str(data))
    canvas.draw_string(0,0, "請對準麥克風說出指令", scale = 2, color = (255,0,0) , thickness = 1)
    if data != "None":
        canvas.draw_string(0, 30, "拼音識別結果:", scale=2, color=(255,0,0), thickness = 1)
        canvas.draw_string(30, 60, (data), scale=2, color=(255,255,255), thickness = 1)
    Screen.show(canvas)
```

## 車牌識別

```python
from AI import PlateRecognition
```

### 模型初始化
```python
PlateRecognition()
```

### 載入識別
```python
loadModel(img)
```

### 參數說明
| 參數 | 值 | 描述 |
| :--- | :--- | :--- |
| img | 圖片 | 要識別的Image對象，必填 |

### 樣例
```python
from cocopiPython import cameraPython,Screen
from AI import PlateRecognition

Screen = Screen() # 畫布初始化
camera = cameraPython() # 攝像頭初始化
PlateRecognition = PlateRecognition() # 車牌識別初始化
while True:
    canvas = camera.capture() # 獲取攝像頭圖像
    canvas = canvas.crop(48,8,224,224) # 裁剪圖像尺寸
    data = PlateRecognition.load(canvas) # 載入識別
    for i in data:
        canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=(255,0,0),thickness=1) # 檢測框
        canvas.draw_string(i["X"],i["Y"]-10,(str(i["name"])),color=(255,0,0)) # 名稱
    Screen.show(canvas,48,8) # 顯示
```

