-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmachine_learning.py
55 lines (38 loc) · 1.28 KB
/
machine_learning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import cv2
import numpy as np
import glob
from drawing_utils import *
FEATURE_DIM = (30, 30)
FEATURE_LENGTH = FEATURE_DIM[0] * FEATURE_DIM[1]
def image_feature(img):
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
train = cv2.resize(gray, FEATURE_DIM)
train = train.reshape(1, FEATURE_LENGTH)
return train.astype(np.float32)
def knn():
total_samples = len(glob.glob("images/training_data/**/*"))
samples = np.zeros((total_samples, FEATURE_LENGTH), dtype=np.float32)
responses = []
training_folders = glob.glob("images/training_data/*")
i = 0
for f in training_folders:
piece_type = f[21:]
for training_image in glob.glob(f + "/*"):
img = cv2.imread(training_image)
samples[i] = image_feature(img)
i += 1
responses.append(ord(piece_type))
responses = np.array(responses, dtype=np.float32)
responses = responses.reshape((responses.size, 1))
test_samples = samples[-10:]
test_responses = responses[-10:]
samples = samples[:-10]
responses = responses[:-10]
knn = cv2.KNearest()
knn.train(samples, responses)
return knn
#ret, result, neighbours, dist = knn.find_nearest(test_samples, k=1)
#matches = result == test_responses
#correct = np.count_nonzero(matches)
#accuracy = correct*100.0 / result.size
#print accuracy