Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.9 kB
2
Indexable
Never
import os
import numpy as np
import pandas as pd
import cv2 # OpenCV
from sklearn.svm import SVC # SVM klasifikator
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier # KNN
import matplotlib.pyplot as plt
%matplotlib inline

def load_image(path):
#     return cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2GRAY)
    return cv2.imread(path)

def display_image(image):
    plt.imshow(image, 'gray')



#ucitavanje svih trening podataka
train_dir = 'data3/train/'
# Snowdrop, LilyValley,
# Bluebell, Tigerlily i Fritillary

snowdrop_imgs = []
lilyvalley_imgs = []
bluebell_imgs = []
tigerlily_imgs = []
fritillary_imgs = []



df = pd.read_csv("data3/train/train_labels.csv", sep=",", engine='python')
for index, row in df.iterrows():
    img_name = row['file']
    img_path = os.path.join(train_dir,img_name)
    img_label = row['labels']
    img = load_image(img_path)
    scaled_image = cv2.resize(img, (250, 250), interpolation=cv2.INTER_LINEAR)
    transforms = [
    ('flip_vertical', cv2.flip(scaled_image, 1)),
    ('flip_horizontal', cv2.flip(scaled_image, 0)),]
    if index == 1:
        display_image(scaled_image)
    if img_label == 'snowdrop':
        snowdrop_imgs.append(scaled_image)
        for name, transformed_image in transforms:
            snowdrop_imgs.append(transformed_image)
    elif img_label == 'lilyvalley':
        lilyvalley_imgs.append(scaled_image)
        for name, transformed_image in transforms:
            lilyvalley_imgs.append(transformed_image)
    elif img_label == 'bluebell':
        bluebell_imgs.append(scaled_image)
        for name, transformed_image in transforms:
            bluebell_imgs.append(transformed_image)
    elif img_label == 'tigerlily':
        tigerlily_imgs.append(scaled_image)
        for name, transformed_image in transforms:
            tigerlily_imgs.append(transformed_image)
    elif img_label == 'fritillary':
        fritillary_imgs.append(scaled_image)
        for name, transformed_image in transforms:
            fritillary_imgs.append(transformed_image)


snowdrop_features = []
lilyvalley_features = []
bluebell_features = []
tigerlily_features = []
fritillary_features = []


labels = []
img=scaled_image
nbins = 8 
cell_size = (8, 8) 
block_size = (3, 3) 

hog = cv2.HOGDescriptor(_winSize=(img.shape[1] // cell_size[1] * cell_size[1], 
                                  img.shape[0] // cell_size[0] * cell_size[0]),
                        _blockSize=(block_size[1] * cell_size[1],
                                    block_size[0] * cell_size[0]),
                        _blockStride=(cell_size[1], cell_size[0]),
                        _cellSize=(cell_size[1], cell_size[0]),
                        _nbins=nbins)



for img in snowdrop_imgs:
    #pozivanjem computa metode nad slikom izracunavamo hog deskriptor i onda taj deskriptor dodajemo u listu pozitivnih featura
    snowdrop_features.append(hog.compute(img))
    labels.append(0)
    
    

for img in lilyvalley_imgs:
    lilyvalley_features.append(hog.compute(img))
    labels.append(1)
    
    
    
for img in bluebell_imgs:
    bluebell_features.append(hog.compute(img))
    labels.append(2)
   
    

for img in tigerlily_imgs:
    tigerlily_features.append(hog.compute(img))
    labels.append(3)
    
    

for img in fritillary_imgs:
    fritillary_features.append(hog.compute(img))
    labels.append(4)
    
    

print(len(snowdrop_features))
print(len(lilyvalley_features))
print(len(bluebell_features))
print(len(tigerlily_features))
print(len(fritillary_features))

snowdrop_features = np.array(snowdrop_features)
lilyvalley_features = np.array(lilyvalley_features)
bluebell_features = np.array(bluebell_features)
tigerlily_features = np.array(tigerlily_features)
fritillary_features = np.array(fritillary_features)


x = np.vstack((snowdrop_features, lilyvalley_features, bluebell_features, tigerlily_features, fritillary_features))
y = np.array(labels)



x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)

def reshape_data(input_data):
    nsamples, nx, ny = input_data.shape
    return input_data.reshape((nsamples, nx*ny))
# x = reshape_data(x)
x_train = reshape_data(x_train)
x_test = reshape_data(x_test)

import time

# početno vrijeme
start = time.perf_counter()

clf_svm = SVC(kernel='linear', probability=True) 
clf_svm.fit(x_train, y_train)
y_train_pred = clf_svm.predict(x_train)
y_test_pred = clf_svm.predict(x_test)
print("Train accuracy: ", accuracy_score(y_train, y_train_pred))
print("Validation accuracy: ", accuracy_score(y_test, y_test_pred))

end = time.perf_counter()

# vrijeme izvršavanja
elapsed_time = end - start
print("vrijeme izvrsavanja ",elapsed_time)