<jupyter_start><jupyter_text>Classification with Plain SVM<jupyter_code>from skimage.transform import resize
from skimage.io import imread

# Path to folder containing the datasets
inputPaths = "C://Users//<NAME>//Documents//7th Sem//IRC//IRC-Rover-Files//Datasets//Creating Datasets//Downloaded Datasets//Final Datasets for Training"

# All possible labels/ directions
labels = []

# List to store the paths of all images in the dataset
imagePaths = list(paths.list_images(inputPaths))

# This list will be used to store all the images in Bitmap format from OpenCV's imread()
images = []

i = 0
for imagePath in imagePaths:
    label = imagePath.split(os.path.sep)[-2]
    labels.append(label)
    
    image = cv2.imread(imagePath)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = cv2.resize(image, (64, 64))
    print("Added image: ", i)
    i += 1
    
    images.append(image.flatten())
print("Number of images: ", len(images))
print("Number of labels: ", len(labels))
images = np.array(images)
labels = np.array(labels)

df = pd.DataFrame(images)
df['Labels'] = labels

x = df.iloc[:, : -1]
y = df.iloc[:, -1]
# SVM Model Construction

from sklearn import svm
from sklearn.model_selection import GridSearchCV, train_test_split

# Initializing model
svc = svm.SVC(probability = True)
params = {'C' : [0.1, 1, 10, 100], 'gamma' : [0.0001, 0.01, 0.1, 1], 'kernel' : ['rbf', 'poly']}
model = GridSearchCV(svc, params)

# Split the train and test datasets
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, stratify = y)
model.fit(x_train, y_train)
# Test the trained model

y_pred = model.predict(x_test)
print("Actual:\n", y_test)
print("Predicted:\n", y_pred)

print("Accuracy: ", (accuracy_score(y_pred, y_test) * 100), "%\n\n")
# Saving the model

import pickle

fileName = "arrowClassifier.sav"
modelPath = "C://Users//<NAME>//Documents//7th Sem//IRC//IRC-Rover-Files//Saved Models//Arrow Classifier//SVM Model"
os.chdir(modelPath)

pickle.dump(model, open(filename, 'wb'))<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup