hsv + bgr prediction
This commit is contained in:
parent
91baba6cc4
commit
6b57db324e
@ -4,20 +4,40 @@ from cv2.mat_wrapper import Mat as Mat
|
|||||||
import os
|
import os
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import csv
|
import csv
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
CSV_PATH = "src\\experiments\\algorithms\\data\\"
|
CSV_PATH = "src\\experiments\\algorithms\\data\\"
|
||||||
IMG_PATH = "dataset\\accasia_1210048 (2023_09_28 12_19_26 UTC).JPG"
|
IMG_PATH = "dataset\\accasia_1210048 (2023_09_28 12_19_26 UTC).JPG"
|
||||||
BARK_TYPES = 8
|
BARK_TYPES = 8
|
||||||
|
|
||||||
|
class Tree(Enum):
|
||||||
|
ACCASIA = 0
|
||||||
|
BERK = 1
|
||||||
|
EIK = 2
|
||||||
|
ELS = 3
|
||||||
|
ESDOORN = 4
|
||||||
|
ES = 5
|
||||||
|
LINDE = 6
|
||||||
|
PLATAAN = 7
|
||||||
|
|
||||||
|
class ColourSpace(Enum):
|
||||||
|
BGR = 0
|
||||||
|
HSV = 1
|
||||||
|
|
||||||
class colourTester:
|
class colourTester:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
pass
|
self.bgr_curves = [[[], [], []] for x in range(BARK_TYPES)]
|
||||||
|
self.bgr_ids = ["", "", ""]
|
||||||
|
self.hsv_curves = [[[], [], []] for x in range(BARK_TYPES)]
|
||||||
|
self.hsv_ids = ["", "", ""]
|
||||||
|
self.labels = ["" for x in range(BARK_TYPES)]
|
||||||
|
|
||||||
def loadImage(self) -> None:
|
def loadImage(self) -> None:
|
||||||
img = cv2.imread(self.im_path, 1)
|
img = cv2.imread(self.im_path, 1)
|
||||||
assert img is not None, \
|
assert img is not None, \
|
||||||
"Failed to load an image, check path"
|
"Failed to load an image, check path"
|
||||||
self.img = img
|
self.bgr_img = img
|
||||||
|
self.hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
||||||
return
|
return
|
||||||
|
|
||||||
def setImPath(self, path : str) -> None:
|
def setImPath(self, path : str) -> None:
|
||||||
@ -50,17 +70,42 @@ class colourTester:
|
|||||||
return (mean, std)
|
return (mean, std)
|
||||||
|
|
||||||
def loadCurves(self) -> None:
|
def loadCurves(self) -> None:
|
||||||
labels = []
|
|
||||||
ids = []
|
|
||||||
data = [[[], [], []] for x in range(BARK_TYPES)]
|
|
||||||
with open(self.csv_bgr_path, 'r') as bgr_file:
|
with open(self.csv_bgr_path, 'r') as bgr_file:
|
||||||
reader = csv.reader(bgr_file, delimiter=',')
|
reader = csv.reader(bgr_file, delimiter=',')
|
||||||
# fix whatever is happening here
|
j = 0
|
||||||
|
i = -1
|
||||||
|
for row in reader:
|
||||||
|
j+=1
|
||||||
|
i += 1 if (j-1) % 3 == 0 else 0
|
||||||
|
self.labels[i] = row[0]
|
||||||
|
self.bgr_ids[(j-1)%3] = row[1]
|
||||||
|
for col in row[2:]:
|
||||||
|
self.bgr_curves[i][(j-1)%3].append(col)
|
||||||
|
|
||||||
print(data)
|
with open(self.csv_hsv_path, 'r') as hsv_file:
|
||||||
|
reader = csv.reader(hsv_file, delimiter=',')
|
||||||
|
j = 0
|
||||||
|
i = -1
|
||||||
|
for row in reader:
|
||||||
|
j+=1
|
||||||
|
i += 1 if (j-1) % 3 == 0 else 0
|
||||||
|
self.labels[i] = row[0]
|
||||||
|
self.hsv_ids[(j-1)%3] = row[1]
|
||||||
|
for col in row[2:]:
|
||||||
|
self.hsv_curves[i][(j-1)%3].append(col)
|
||||||
return
|
return
|
||||||
|
|
||||||
def testImage(self) -> None:
|
def correlate(self, mean : float, tree : Tree, space : ColourSpace) -> [float, float, float]:
|
||||||
|
results = [0, 0, 0]
|
||||||
|
if space == ColourSpace.BGR:
|
||||||
|
curve = self.bgr_curves
|
||||||
|
elif space == ColourSpace.HSV:
|
||||||
|
curve = self.hsv_curves
|
||||||
|
for i in range(3):
|
||||||
|
results[i] = float(curve[tree.value][i][int(mean[i])])
|
||||||
|
return results
|
||||||
|
|
||||||
|
def testImage(self) -> Tree:
|
||||||
# load an image
|
# load an image
|
||||||
self.loadImage()
|
self.loadImage()
|
||||||
|
|
||||||
@ -68,16 +113,49 @@ class colourTester:
|
|||||||
self.loadCurves()
|
self.loadCurves()
|
||||||
|
|
||||||
# extract data
|
# extract data
|
||||||
mean, std = self.getData(self.img)
|
bgr_mean, bgr_std = self.getData(self.bgr_img)
|
||||||
|
hsv_mean, hsv_std = self.getData(self.hsv_img)
|
||||||
|
|
||||||
# correlate with curves
|
# correlate with curves
|
||||||
# dictionary? list? with 0 - 1 scores
|
av_results = np.zeros(BARK_TYPES)
|
||||||
pass
|
for tree in Tree:
|
||||||
|
bgr_res = self.correlate(bgr_mean, tree, ColourSpace.BGR)
|
||||||
|
hsv_res = self.correlate(hsv_mean, tree, ColourSpace.HSV)
|
||||||
|
av_results[tree.value] = (np.mean(bgr_res)+np.mean(hsv_res) / 2)
|
||||||
|
# print(str(IMG_PATH.split('_')[0])+';'+tree.name + ';' + str(np.mean(bgr_res)) + ';' + str(np.mean(hsv_res)))
|
||||||
|
return np.argmax(av_results)
|
||||||
|
|
||||||
|
def testDataset(self, path : str) -> None:
|
||||||
|
n_t = 0
|
||||||
|
n_f = 0
|
||||||
|
tree_type_cor = np.zeros(BARK_TYPES)
|
||||||
|
tree_type_inc = np.zeros(BARK_TYPES)
|
||||||
|
res = np.zeros(BARK_TYPES)
|
||||||
|
i = -1
|
||||||
|
last_name = ""
|
||||||
|
for file in os.listdir(path):
|
||||||
|
file_path = os.path.join(path, file)
|
||||||
|
self.setImPath(file_path)
|
||||||
|
name = file_path.split('_')[0].split('\\')[1].upper()
|
||||||
|
result = Tree(self.testImage()).name
|
||||||
|
|
||||||
|
if last_name != name:
|
||||||
|
i += 1
|
||||||
|
last_name = name
|
||||||
|
|
||||||
|
if(name == result):
|
||||||
|
tree_type_cor[i] += 1
|
||||||
|
else:
|
||||||
|
tree_type_inc[i] += 1
|
||||||
|
|
||||||
|
res = tree_type_cor / (tree_type_cor + tree_type_inc) * 100
|
||||||
|
for i in range(BARK_TYPES):
|
||||||
|
print("%s: %d%% correct"%(Tree(i).name, res[i]))
|
||||||
|
return
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
tester = colourTester()
|
tester = colourTester()
|
||||||
tester.setImPath(IMG_PATH)
|
|
||||||
tester.setCsvBgrPath(CSV_PATH + "bgr_curve.csv")
|
tester.setCsvBgrPath(CSV_PATH + "bgr_curve.csv")
|
||||||
tester.setCsvHsvPath(CSV_PATH + "hsv_curve.csv")
|
tester.setCsvHsvPath(CSV_PATH + "hsv_curve.csv")
|
||||||
tester.testImage()
|
tester.testDataset("dataset")
|
||||||
pass
|
pass
|
Loading…
Reference in New Issue
Block a user