109-2_Multimedia Technologies and Applications

Midterm Project 期中專案

期中專案一 ── 剪刀石頭布

第十二組

資工二 108590049 符芷琪

資工二108590050 李浩銘


請利用本課程至今所教學的內容,設計一個程式,程式要求如下 :

  • 需自行收集影像資料集,其中包含剪刀、石頭、布三個類別的手勢。
  • 影像前處理方法不限制,需將前處理後的結果送入SVM分類器進行分類。

程式需包含以下功能 :

  • 當使用者輸入一手勢之影像,可將此影像調整成適當格式並以SVM進行分類。
  • 以亂數產生剪刀、石頭、布任一結果,並與輸入手勢影像比較勝負。
  • 亂數產生之結果,需以視窗展示出來,包含三個視窗(影像大小均可自行調整):
  • 使用者的輸入手勢
  • 程式亂數產生的手勢 (亂數產生手勢圖片可自行選擇)
  • 以下三者的組合圖片
  • 使用者手勢
  • 程式亂數產生的手勢
  • 勝負結果的組合圖片 (勝負示意圖可自行選擇)

Algorithms

BoW Model + SVM Model BoW Model: SIFT Features Extraction K-Means clustering Standardization

SVM Model: GridSearch Fit

SIFT Algorithm

We use cv2.SIFT_create() instead of cv2.features2d.SIFT_create().

def _SIFT_(img):
    sift = cv2.SIFT_create();
    kp, des = sift.detectAndCompute(img, None);

    return des;

K-Means Clustering Algorithm

There are 3 K-Means Algorithm, cv2.kmeans(), sklearn.cluster.KMeans(), scipy.cluster.vq.kmeans(), respectively.

cv2.kmeans()

    criteria = cv2.kmeans(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 0.1);
    flags = cv2.KMEANS_RANDOM_CENTERS;

    compactness, labels, centers = cv2.kmeans(features, util.K_MEANS_CLUSTERS, None, criteria, 20, flags);

    return centers;

sklearn.cluster.KMeans()

    kmeans = KMeans(n_clusters=util.K_MEANS_CLUSTERS, random_state=0).fit(features);

    return kmeans.cluster_centers_;

scipy.cluster.vq.kmeans()

    centers, variance = kmeans(features, util.K_MEANS_CLUSTERS);

    return centers;

The accuracy of scipy.cluster.vq.kmeans() is the best, but the algorithm does not accept random_state as a parameter. The output will be different every time. Therefore, do not use scipy.cluster.vq.kmeans() on the developing, use sklearn.cluster.KMeans() instead.

For cv2.kmeans(), please use cv.KMEANS_PP_CENTERS as the flags on the developing.

Standardization

For the features standardization, we use 'sklearn.preprocessing.StandardScaler()'.

    stdSlr = StandardScaler().fit(data);
    data = stdSlr.transform(data);

How to Use

Split your train data and test data of classes, then put them under the folder 'dataset'.

/dataset
├ /train
│ ├ /paper
│ ├ /rock
│ └ /scissors
└ /test \

├ /paper \
├ /rock \
└ /scissors

Train model

Run the following command to train the model.

model.py TRAIN

If you want to find out the best parameters automatically, run the following command.

model.py TRAIN GRID

Test model

Run the following command to test the model.

model.py TEST

All of the command above will generate LOG file under the folder log.

Play

To play the Rock-Paper-Scissors game, run the following command.

midterm.py <image path>

If you want to check out the failure cases, run the following command.

midterm.py

It will show all the failure cases one by one, and save them under doc/error output.

Reference

Rock Paper Scissors Dataset (CC By 2.0 @Laurence Moroney lmoroney@gmail.com / laurencemoroney.com)

DrGFreeman/rps-cv (MIT LICENSE)

使用OpenCV与sklearn实现基于词袋模型(Bag of Word)的图像分类预测与搜索

SVM+Sift+K-means实现图像分类(python)

LICENSE

GNU General Public License v3.0