-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
85 lines (61 loc) · 2.76 KB
/
main.py
File metadata and controls
85 lines (61 loc) · 2.76 KB
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
import json
import os
import pickle
from pathlib import Path
from typing import Tuple
import cv2
import numpy as np
# TODO: versions of libraries that will be used:
# Python 3.9 (you can use previous versions as well)
# numpy 1.19.4
# scikit-learn 0.22.2.post1
# opencv-python 4.2.0.34
def load_dataset(dataset_dir_path: Path) -> Tuple[np.ndarray, np.ndarray]:
x, y = [], []
for i, class_dir in enumerate(sorted(dataset_dir_path.iterdir())):
for file in class_dir.iterdir():
img_file = cv2.imread(str(file), cv2.IMREAD_GRAYSCALE)
x.append(img_file)
y.append(i)
return np.asarray(x), np.asarray(y)
def convert_descriptor_to_histogram(descriptors, vocab_model, normalize=True) -> np.ndarray:
features_words = vocab_model.predict(descriptors)
histogram = np.zeros(vocab_model.n_clusters, dtype=np.float32)
unique, counts = np.unique(features_words, return_counts=True)
histogram[unique] += counts
if normalize:
histogram /= histogram.sum()
return histogram
def apply_feature_transform(data: np.ndarray, feature_detector_descriptor, vocab_model) -> np.ndarray:
data_transformed = []
for image in data:
keypoints, image_descriptors = feature_detector_descriptor.detectAndCompute(image, None)
bow_features_histogram = convert_descriptor_to_histogram(image_descriptors, vocab_model)
data_transformed.append(bow_features_histogram)
return np.asarray(data_transformed)
def data_processing(x: np.ndarray) -> np.ndarray:
# TODO: add data processing here
return x
def project():
np.random.seed(42)
# TODO: fill the following values
first_name = 'Michał'
last_name = 'Gontarczyk'
data_path = Path('test') # You can change the path here
data_path = os.getenv('DATA_PATH', data_path) # Don't change that line
x, y = load_dataset(data_path)
# TODO: create a detector/descriptor here. Eg. cv2.AKAZE_create()
feature_detector_descriptor = cv2.ORB_create(5000)
# TODO: train a vocabulary model and save it using pickle.dump function
with Path('vocab_model.p').open('rb') as vocab_file: # Don't change the path here
vocab_model = pickle.load(vocab_file)
x_transformed = apply_feature_transform(x, feature_detector_descriptor, vocab_model)
# TODO: train a classifier and save it using pickle.dump function
with Path('clf.p').open('rb') as classifier_file: # Don't change the path here
clf = pickle.load(classifier_file)
score = clf.score(x_transformed, y)
print(f'{first_name} {last_name} score: {score}')
with Path(f'{last_name}_{first_name}_score.json').open('w') as score_file: # Don't change the path here
json.dump({'score': score}, score_file)
if __name__ == '__main__':
project()