forked from norhanreda/neural-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sift_new.py
72 lines (52 loc) · 2.23 KB
/
sift_new.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
# Here is an example code for using PCA in SIFT descriptor and then SVM classifier in Python:
# 1. Import necessary libraries:
import cv2
import numpy as np
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# 2. Load the dataset and extract SIFT descriptors:
# Load dataset images and labels
train_images = []
train_labels = []
for i in range(1, 11):
for j in range(1, 6):
img = cv2.imread(f"dataset/{i}_{j}.jpg", cv2.IMREAD_GRAYSCALE)
train_images.append(img)
train_labels.append(i)
# Create SIFT object and extract descriptors from all images
sift = cv2.xfeatures2d.SIFT_create()
descriptors = []
for img in train_images:
_, des = sift.detectAndCompute(img, None)
descriptors.append(des)
# 3. Apply PCA to reduce the dimensionality of the descriptors:
# Flatten all descriptors into a single array and apply PCA to reduce dimensionality
descriptors_flat = np.concatenate(descriptors)
pca = PCA(n_components=128)
descriptors_pca = pca.fit_transform(descriptors_flat)
# 4. Train an SVM classifier on the reduced descriptors:
# Train SVM classifier on reduced descriptors using labels as target values
svm = SVC(kernel='linear')
svm.fit(descriptors_pca, train_labels)
# 5. Test the classifier on a test set and calculate accuracy:
# ```python
# Load test set images and labels, extract SIFT descriptors, apply PCA transformation,
# predict labels using SVM classifier, and calculate accuracy score.
test_images = []
test_labels = []
for i in range(1, 11):
for j in range(6, 11):
img = cv2.imread(f"dataset/{i}_{j}.jpg", cv2.IMREAD_GRAYSCALE)
test_images.append(img)
test_labels.append(i)
descriptors_test = []
for img in test_images:
_, des = sift.detectAndCompute(img, None)
descriptors_test.append(des)
descriptors_test_flat = np.concatenate(descriptors_test)
descriptors_test_pca = pca.transform(descriptors_test_flat)
predicted_labels = svm.predict(descriptors_test_pca)
accuracy = accuracy_score(test_labels, predicted_labels)
print(f"Accuracy: {accuracy}")
# Note: This is just an example code and may need to be modified based on your specific requirements.