Newer
Older
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
import os
import cv2
import numpy as np
"""This file computes feature vectors for every class."""
main_directory = "Plant_leave_diseases_dataset_without_augmentation"
result_directory = 'result_descriptors_and_labels/'
label_counter = 0
nfeatures = 3
#sift = cv2.SIFT.create(nfeatures=500)
#Go through all directories in the main directory
for folder_name in os.listdir(main_directory):
folder_path = os.path.join(main_directory, folder_name)
#check if folder path leads to a folder
if os.path.isdir(folder_path):
#Calculate the number of pictures in the sub-directory
num_elements = len(os.listdir(folder_path))
descriptor_matrix = np.zeros((num_elements, nfeatures*128), dtype= np.float16)
label_vec = np.reshape(np.array([label_counter]*num_elements), (num_elements, 1))
for i in range(num_elements):
sift = cv2.SIFT.create(nfeatures=nfeatures-1) #Create a sift object for every single picture
#print("HALLO")
#Read pictures from the sub-directory
img = cv2.imread(main_directory+"/"+folder_name+"/"+sorted(os.listdir(folder_path))[i])
#Save the descriptors of the picture in a list in the dictionary
kp, des = sift.detectAndCompute(img, None)
#flatten_des = np.ravel(des)
#descriptor_matrix[i, 0:len(flatten_des)] = flatten_des
flatten_des = np.ravel(des)
if len(flatten_des) < nfeatures * 128:
flatten_des = np.concatenate([flatten_des, np.zeros(nfeatures * 128 - len(flatten_des))])
elif len(flatten_des) > nfeatures * 128:
flatten_des = flatten_des[:nfeatures * 128]
descriptor_matrix[i, :len(flatten_des)] = flatten_des
print("HUHU")
num_rows_to_extract = int(np.floor(0.1 * descriptor_matrix.shape[0]))
test_matrix= np.zeros((num_rows_to_extract, nfeatures*128), dtype= np.float16)
test_matrix = descriptor_matrix[list(range(num_rows_to_extract)), :]
test_vec = np.zeros((num_rows_to_extract, 1), dtype= np.float16 )
test_vec = test_vec = label_vec[:num_rows_to_extract, :]
result_matrix = np.delete(descriptor_matrix, list(range(num_rows_to_extract)), axis=0)
result_vec = np.delete(label_vec, list(range(num_rows_to_extract)), axis=0)
#Save every list in a separate npz, so the data of the classes is not shuffled
print("Hallo1")
result_matrix = np.nan_to_num(result_matrix)
np.savez(result_directory+'train/'+folder_name+'_sift_descriptors', result_matrix) #shape: num_img, num_kp, len(descriptors); for 1 would be (360, 529, 128)
print("HAllo2")
result_vec = np.nan_to_num(result_vec)
np.savez(result_directory+'train/'+folder_name+'_label_vector', result_vec)
test_matrix = np.nan_to_num(test_matrix)
np.savez(result_directory+'test/'+folder_name+'_sift_descriptors', test_matrix)
test_vec = np.nan_to_num(test_vec)
np.savez(result_directory+'test/'+folder_name+'_label_vector', test_vec)
label_counter += 1
#Delete this line if you want to compute the values for every class. Otherwise, you only compute
#the values for the first class
#For debugging:
'''loaded_data = np.load(result_directory+'Peach___healthy_sift_descriptors.npz')
# Zeige die Formen der Arrays an
for key, value in loaded_data.items():
print(f"Shape von {key}: {value.shape}") '''
#TODO : confusion Matrix, F1 score, precision und recall