1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
   | import os import cv2 import numpy as np from sklearn.model_selection import train_test_split from sklearn.metrics import confusion_matrix, classification_report from sklearn.svm import SVC from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt import matplotlib as mpl
  mpl.rcParams['font.sans-serif'] = ['KaiTi'] mpl.rcParams['font.serif'] = ['KaiTi']
 
 
 
 
 
  X = []   Y = []   Z = []  
  for i in range(0, 10):          for f in os.listdir("photo2/%s" % i):                  X.append("photo2//" + str(i) + "//" + str(f))                  Y.append(i)
 
 
 
 
 
 
 
 
 
 
  X = np.array(X) Y = np.array(Y)
 
  X_train, X_test, y_train, y_test = train_test_split(X, Y,                                                     test_size=0.2, random_state=1)
  print(len(X_train), len(X_test), len(y_train), len(y_test))
 
 
 
 
 
  XX_train = [] for i in X_train:               image = cv2.imdecode(np.fromfile(i, dtype=np.uint8), cv2.IMREAD_COLOR)
           img = cv2.resize(image, (256, 256),                      interpolation=cv2.INTER_CUBIC)
           hist = cv2.calcHist([img], [0, 1], None,                         [256, 256], [0.0, 255.0, 0.0, 255.0])
      XX_train.append(((hist / 255).flatten()))
 
  XX_test = [] for i in X_test:                    image = cv2.imdecode(np.fromfile(i, dtype=np.uint8), cv2.IMREAD_COLOR)
           img = cv2.resize(image, (256, 256),                      interpolation=cv2.INTER_CUBIC)
           hist = cv2.calcHist([img], [0, 1], None,                         [256, 256], [0.0, 255.0, 0.0, 255.0])
      XX_test.append(((hist / 255).flatten()))
 
 
 
 
 
  clf = SVC().fit(XX_train, y_train) clf = SVC(kernel="linear").fit(XX_train, y_train) predictions_labels = clf.predict(XX_test)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  print(u'预测结果:') print(predictions_labels) print(u'算法评价:') print(classification_report(y_test, predictions_labels))
 
   |