import cv2
import numpy as np
import os, glob, time
# ๊ฐ์ข
๋ณ์ ์ ์ธ---โ
startT = time.time() # ์์์๊ฐ ์ธก์ ์ ์ํ ์๊ฐ ์ ์ฅ
categories = ['airplanes', 'Motorbikes' ] # ์นดํ
๊ณ ๋ฆฌ ์ด๋ฆ
dictionary_size = 50 # ์ฌ์ ํฌ๊ธฐ, ํด๋ฌ์คํฐ ๊ฐฏ์
base_path = "../img/101_ObjectCategories/" # ํ์ต ์ด๋ฏธ์ง ๊ธฐ๋ณธ ๊ฒฝ๋ก
dict_file = './plane_bike_dict.npy' # ์ฌ์ ๊ฐ์ฒด ์ ์ฅํ ํ์ผ ์ด๋ฆ
svm_model_file = './plane_bike_svm.xml' # SVM ๋ชจ๋ธ ๊ฐ์ฒด ์ ์ฅํ ํ์ผ ์ด๋ฆ
# ์ถ์ถ๊ธฐ์ BOW ๊ฐ์ฒด ์ --- โก
detector = cv2.xfeatures2d.SIFT_create() # ์ถ์ถ๊ธฐ๋ก SIFT ์์ฑ
matcher = cv2.BFMatcher(cv2.NORM_L2) # ๋งค์นญ๊ธฐ๋ก BF ์์ฑ
bowTrainer = cv2.BOWKMeansTrainer(dictionary_size) # KMeans๋ก ๊ตฌํ๋ BWOTrainer ์์ฑ
bowExtractor = cv2.BOWImgDescriptorExtractor(detector, matcher) # ํ์คํ ๊ทธ๋จ ๊ณ์ฐํ BOW์ถ์ถ๊ธฐ ์์ฑ
# ํน์ง ๋์คํฌ๋ฆฝํฐ๋ฅผ KMeansTrainer์ ์ถ๊ฐ---โข
train_paths = [] # ํ๋ จ์ ์ฌ์ฉํ ๋ชจ๋ ์ด๋ฏธ์ง ๊ฒฝ๋ก
train_labels = [] # ํ์ต ๋ฐ์ดํ ๋ ์ด๋ธ
print('Adding descriptor to BOWTrainer...')
for idx, category in enumerate(categories): # ์นดํ
๊ณ ๋ฆฌ ์ํ
dir_path = base_path + category
img_paths = glob.glob(dir_path +'/*.jpg')
img_len = len(img_paths)
for i, img_path in enumerate(img_paths): # ์นดํ
๊ณ ๋ฆฌ ๋ด์ ๋ชจ๋ ์ด๋ฏธ์ง ํ์ผ ์ํ
train_paths.append(img_path)
train_labels.append(idx) # ํ์ต ๋ฐ์ดํ ๋ ์ด๋ธ, 0 ๋๋ 1
img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# ํน์ง์ ๊ณผ ํน์ง ๋์คํฌ๋ฆฝํฐ ์ถ์ถ ๋ฐ bowTrainer์ ์ถ๊ฐ ---โฃ
kpt, desc= detector.detectAndCompute(gray, None)
bowTrainer.add(desc)
print('\t%s %d/%d(%.2f%%)' \
%(category,i+1, img_len, (i+1)/img_len*100), end='\r')
print()
print('Adding descriptor completed...')
# KMeans ํด๋ฌ์คํฐ๋ก ๊ตฐ์งํํ์ฌ ์๊ฐ ์ฌ์ ์์ฑ ๋ฐ ์ ์ฅ---โค
print('Starting Dictionary clustering(%d)... \
It will take several time...'%dictionary_size)
dictionary = bowTrainer.cluster() # ๊ตฐ์งํ๋ก ์๊ฐ ์ฌ์ ์์ฑ
np.save(dict_file, dictionary) # ์๊ฐ ์ฌ์ ๋ฐ์ดํ(๋ํ์ผ)๋ฅผ ํ์ผ๋ก ์ ์ฅ
print('Dictionary Clustering completed...dictionary shape:',dictionary.shape)
# ์๊ฐ ์ฌ์ ๊ณผ ๋ชจ๋ ์ด๋ฏธ์ง์ ๋งค์นญ์ ์ผ๋ก ํ์คํ ๊ทธ๋จ ๊ณ์ฐ---โฅ
bowExtractor.setVocabulary(dictionary) # bowExtractor์ ์๊ฐ ์ฌ์ ์
ํ
train_desc = [] # ํ์ต ๋ฐ์ดํ
for i, path in enumerate(train_paths): # ๋ชจ๋ ํ์ต ๋์ ์ด๋ฏธ์ง ์ํ
img = cv2.imread(path) # ์ด๋ฏธ์ง ์ฝ๊ธฐ
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# ๋งค์นญ์ ์ ๋ํ ํ์คํ ๊ทธ๋จ ๊ณ์ฐ --- โฆ
hist = bowExtractor.compute(gray, detector.detect(gray))
train_desc.extend(hist)
print('Compute histogram training set...(%.2f%%)'\
%((i+1)/len(train_paths)*100),end='\r')
print("\nsvm items", len(train_desc), len(train_desc[0]))
# ํ์คํ ๊ทธ๋จ์ ํ์ต๋ฐ์ดํ๋ก SVM ํ๋ จ ๋ฐ ๋ชจ๋ธ ์ ์ฅ---โง
print('svm training...')
svm = cv2.ml.SVM_create()
svm.trainAuto(np.array(train_desc), cv2.ml.ROW_SAMPLE, np.array(train_labels))
svm.save(svm_model_file)
print('svm training completed.')
print('Training Elapsed: %s'\
%time.strftime('%H:%M:%S', time.gmtime(time.time()-startT)))
# ์๋์ ์ด๋ฏธ์ง๋ก ํ
์คํธ --- โจ
print("Accuracy(Self)")
for label, dir_name in enumerate(categories):
labels = []
results = []
img_paths = glob.glob(base_path + '/'+dir_name +'/*.*')
for img_path in img_paths:
labels.append(label)
img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
feature = bowExtractor.compute(gray, detector.detect(gray))
ret, result = svm.predict(feature)
resp = result[0][0]
results.append(resp)
labels = np.array(labels)
results = np.array(results)
err = (labels != results)
err_mean = err.mean()
print('\t%s: %.2f %%' % (dir_name, (1 - err_mean)*100))
์ถ๋ ฅ๊ฒฐ๊ณผ
[ WARN:0@0.050] global shadow_sift.hpp:15 cv::xfeatures2d::SIFT_create DEPRECATED: cv.xfeatures2d.SIFT_create() is deprecated due SIFT tranfer to the main repository. https://github.com/opencv/opencv/issues/16736
Adding descriptor to BOWTrainer...
airplanes 800/800(100.00%)
Motorbikes 798/798(100.00%)
Adding descriptor completed...
Starting Dictionary clustering(50)... It will take several time...
Dictionary Clustering completed...dictionary shape: (50, 128)
Compute histogram training set...(100.00%)
svm items 1598 50
svm training...
svm training completed.
Training Elapsed: 00:02:13
Accuracy(Self)
airplanes: 94.50 %
Motorbikes: 96.24 %