Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- #from sklearn import datasets
- #from sklearn.metrics import confusion_matrix
- #from sklearn.model_selection import train_test_split
- #from PIL import Image
- import numpy as np
- import cv2
- import sys
- from sklearn.svm import SVC
- path = 'C:\\Users\Kushal\Desktop\TLdataset01\data01'
- image_paths = [os.path.join(path, f) for f in os.listdir(path)]
- # images list will contain image data. i.e. pixel intensities
- images = []
- # labels list will contain the label that is assigned to the image
- labels = []
- name_map={}
- for image_path in image_paths:
- # Read the image and convert to grayscale
- image_pil = Image.open(image_path).convert('L')
- # Convert the image format into numpy array
- # image = np.array(image_pil, 'uint8')
- # Get the label of the image
- nbr = int(os.path.split(image_path)[1].split("-")[0])
- print(nbr)
- # Get the subject name for the label
- name = os.path.split(image_path)[1].split("-")[1]
- print(name)
- name_map[nbr] = name
- images.append(image)
- labels.append(nbr)
- (X_train, X_test, y_train, y_test) = train_test_split(images, labels, test_size=0.30)
- # training a linear SVM classifier
- svm_model_linear = SVC(kernel='linear', C=1).fit(X_train, y_train)
- svm_predictions = svm_model_linear.predict(X_test)
- # model accuracy for X_test
- accuracy = svm_model_linear.score(X_test, y_test)
- # creating a confusion matrix
- cm = confusion_matrix(y_test, svm_predictions)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement