---
# System prepended metadata

title: Hand Posture Recognition with CNN

---

#  Hand Posture Recognition with CNN


---
# 目錄
[TOC]
# 問題描述
訓練卷積神經網路用以辨識三種不同的手勢



---

# 解法
流程為:
讀取照片&處理照片⇨ 建立CNN模型 ⇨ 訓練神經網路 ⇨ 
根據Loss&Accuracy作圖

---
### 讀取照片&處理照片

```python=
def enumerate_files(dirs, path="D://All_gray_1_32_32", n_poses=3, n_samples=20):
    filenames, targets = [], []
    for p in dirs:
        for n in range(n_poses):
            for j in range(3):
                dir_name = path + '//' + p + '//000' + str(n*3+j) + '//'
                for s in range(n_samples):
                    d = dir_name + '%04d'%s + '//'
                    for f in os.listdir(d):
                        if f.endswith('jpg'):
                            filenames += [d+f]
                            targets.append(n)
    return filenames, targets
def read_images(files):
    imgs = []
    for f in files:
        img = cv2.imread(f, cv2.IMREAD_GRAYSCALE)
        imgs.append(img)
        #print(img)
    return imgs
def read_datasets(datasets):
    files, labels = enumerate_files(datasets)
    list_of_arrays = read_images(files)
    return np.array(list_of_arrays), labels
    
train_sets = ['Set1', 'Set2', 'Set3']
test_sets = ['Set4', 'Set5']
train_images, train_labels = read_datasets(train_sets)
test_images, test_labels = read_datasets(test_sets)

train_images = train_images.reshape((540 ,32 ,32 ,1))
train_images = train_images.astype('float32')/255
test_images = test_images.reshape((360 ,32 ,32 ,1))
test_images = test_images.astype('float32')/255

#print(train_images)
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
```
透過read_datasets()呼叫enumerate_files()讀取位於各資料夾的照片位置跟label並回傳
read_images()則使用cv2進行讀取照片
其餘則是對照片進行初步的處理。

### 建立CNN模型
```python=
model = models.Sequential()
model.add(layers.Conv2D(10,(5,5),activation='relu',input_shape=(32,32,1)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(20,(5,5),activation='relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Dropout(rate=0.25))
model.add(layers.Flatten())
model.add(layers.Dense(100,activation='relu'))
model.add(layers.Dense(3,activation='softmax'))
print(model.summary())
```
使用兩層卷積層、一層池化層和一層dropout

### 訓練神經網路
```python=
model.compile(optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics=["accuracy"])
epoch=20
i = model.fit(train_images, train_labels, validation_data=(test_images, test_labels), 
              batch_size=32, epochs=epoch, verbose=2)
```

為了方便後續作圖使用到epochs，先另行定義一個變數epoch並給定其值為20


![](https://i.imgur.com/pN1Dc8B.jpg)
![](https://i.imgur.com/DzBT96x.png)



### 根據Loss&Accuracy作圖
```python=
import matplotlib.pyplot as plt
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, epoch), i.history["loss"], label="train_loss")
plt.plot(np.arange(0, epoch), i.history["val_loss"], label="val_loss")
plt.title("Training Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend()
plt.figure()
plt.plot(np.arange(0, epoch), i.history["accuracy"], label="accuracy")
plt.plot(np.arange(0, epoch), i.history["val_accuracy"], label="val_accuracy")
plt.title("Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Accuracy")
plt.legend()
plt.show()
```
![](https://i.imgur.com/T9XebA8.png)
![](https://i.imgur.com/tK8t8y3.png)

