-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
answer_86.py
97 lines (72 loc) · 2.24 KB
/
answer_86.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import cv2
import numpy as np
import matplotlib.pyplot as plt
from glob import glob
# Dicrease color
def dic_color(img):
img //= 63
img = img * 64 + 32
return img
# Database
def get_DB():
# get training image path
train = glob("dataset/train_*")
train.sort()
# prepare database
db = np.zeros((len(train), 13), dtype=np.int32)
# prepare path database
pdb = []
# each image
for i, path in enumerate(train):
# read image
img = dic_color(cv2.imread(path))
#get histogram
for j in range(4):
db[i, j] = len(np.where(img[..., 0] == (64 * j + 32))[0])
db[i, j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0])
db[i, j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0])
# get class
if 'akahara' in path:
cls = 0
elif 'madara' in path:
cls = 1
# store class label
db[i, -1] = cls
# store image path
pdb.append(path)
return db, pdb
# test
def test_DB(db, pdb):
# get test image path
test = glob("dataset/test_*")
test.sort()
accurate_N = 0.
# each image
for path in test:
# read image
img = dic_color(cv2.imread(path))
# get histogram
hist = np.zeros(12, dtype=np.int32)
for j in range(4):
hist[j] = len(np.where(img[..., 0] == (64 * j + 32))[0])
hist[j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0])
hist[j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0])
# get histogram difference
difs = np.abs(db[:, :12] - hist)
difs = np.sum(difs, axis=1)
# get argmin of difference
pred_i = np.argmin(difs)
# get prediction label
pred = db[pred_i, -1]
if pred == 0:
pred_label = "akahara"
elif pred == 1:
pred_label = "madara"
gt = "akahara" if "akahara" in path else "madara"
if gt == pred_label:
accurate_N += 1
print(path, "is similar >>", pdb[pred_i], " Pred >>", pred_label)
accuracy = accurate_N / len(test)
print("Accuracy >>", accuracy, "({}/{})".format(int(accurate_N), len(test)))
db, pdb = get_DB()
test_DB(db, pdb)