Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from sklearn.ensemble import RandomForestClassifier
  3. import h5py
  4. import numpy as np
  5. import os
  6. import glob
  7. import cv2
  8. from matplotlib import pyplot
  9. from sklearn.svm import LinearSVC
  10. from sklearn.externals import joblib
  11.  
  12.  
  13. fixed_size = tuple((500, 500))
  14.  
  15.  
  16. clf = LinearSVC()
  17.  
  18.  
  19.  
  20. h5f_data = h5py.File('output/data.h5', 'r')
  21. h5f_label = h5py.File('output/labels.h5', 'r')
  22.  
  23. global_features_string = h5f_data['dataset_1']
  24. global_labels_string = h5f_label['dataset_1']
  25.  
  26. global_features = np.array(global_features_string)
  27. global_labels = np.array(global_labels_string)
  28.  
  29. h5f_data.close()
  30. h5f_label.close()
  31.  
  32.  
  33.  
  34.  
  35. clf.fit(global_features,global_labels)
  36.  
  37.  
  38.  
  39. train_path = "dataset/train"
  40.  
  41.  
  42. train_labels = os.listdir(train_path)
  43.  
  44.  
  45. train_labels.sort()
  46.  
  47.  
  48.  
  49. test_path = "dataset/test"
  50.  
  51.  
  52. bins = 8
  53.  
  54.  
  55.  
  56.  
  57.  
  58. def fd_histogram(image, mask=None):
  59. image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  60. hist = cv2.calcHist([image], [0, 1, 2], None, [bins, bins, bins], [0, 256, 0, 256, 0, 256])
  61. hist = cv2.normalize(hist, hist)
  62. return hist.flatten()
  63.  
  64.  
  65.  
  66. for file in glob.glob(test_path + "/*.jpg"):
  67. image = cv2.imread(file)
  68. image = cv2.resize(image, fixed_size)
  69. fv_histogram = fd_histogram(image)
  70.  
  71.  
  72. global_feature = np.hstack([fv_histogram])
  73.  
  74.  
  75. prediction = clf.predict(global_feature.reshape(1,-1))[0]
  76.  
  77.  
  78. cv2.putText(image, train_labels[prediction], (20,30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,255), 3)
  79.  
  80.  
  81. #plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
  82. #plt.show()
  83. #img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  84. cv2.imshow('Image',image)
  85. cv2.waitKey(1500)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement