Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import os
  2. #from sklearn import datasets
  3. #from sklearn.metrics import confusion_matrix
  4. #from sklearn.model_selection import train_test_split
  5. #from PIL import Image
  6. import numpy as np
  7. import cv2
  8. import sys
  9. from sklearn.svm import SVC
  10.  
  11. path = 'C:\\Users\Kushal\Desktop\TLdataset01\data01'
  12. image_paths = [os.path.join(path, f) for f in os.listdir(path)]
  13. # images list will contain image data. i.e. pixel intensities
  14. images = []
  15. # labels list will contain the label that is assigned to the image
  16. labels = []
  17. name_map={}
  18.  
  19. for image_path in image_paths:
  20. # Read the image and convert to grayscale
  21. image_pil = Image.open(image_path).convert('L')
  22. # Convert the image format into numpy array
  23. # image = np.array(image_pil, 'uint8')
  24.  
  25. # Get the label of the image
  26. nbr = int(os.path.split(image_path)[1].split("-")[0])
  27. print(nbr)
  28. # Get the subject name for the label
  29. name = os.path.split(image_path)[1].split("-")[1]
  30. print(name)
  31. name_map[nbr] = name
  32. images.append(image)
  33. labels.append(nbr)
  34.  
  35. (X_train, X_test, y_train, y_test) = train_test_split(images, labels, test_size=0.30)
  36.  
  37. # training a linear SVM classifier
  38.  
  39. svm_model_linear = SVC(kernel='linear', C=1).fit(X_train, y_train)
  40. svm_predictions = svm_model_linear.predict(X_test)
  41.  
  42. # model accuracy for X_test
  43. accuracy = svm_model_linear.score(X_test, y_test)
  44. # creating a confusion matrix
  45. cm = confusion_matrix(y_test, svm_predictions)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement