def daun(): # fixed-sizes for image fixed_size = tuple((1000,1000)) #Resize pixel menjadi px x px filename = open('data.csv', 'r') dataframe = pandas.read_csv(filename) kelas = dataframe.drop(dataframe.columns[:-1], axis=1) data = dataframe.drop(dataframe.columns[-1:], axis=1) # print(data) # print(kelas) # empty lists to hold feature vectors and labels global_features = [] labels = [] i, j = 0, 0 k = 0 # create all the machine learning models models = [] models.append(('Random Forest',RandomForestClassifier(max_depth=None, random_state=0))) # variables to hold the results and names results = [] names = [] scoring = "accuracy" # filter all the warnings import warnings warnings.filterwarnings('ignore') # 10-fold cross validation for name, model in models: kfold = KFold(n_splits=10, random_state=7) cv_results = cross_val_score(model, data, kelas, cv=kfold, scoring=scoring) results.append(cv_results) names.append(name) msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std()) print(msg) import matplotlib.pyplot as plt # create the model - Random Forests clf = RandomForestClassifier(max_depth=None, random_state=0) # fit the training data to the model clf.fit(data,kelas) image = cv2.imread('daona2_2.jpg') image = cv2.resize(image, fixed_size) humoments = mp.hu_moments(image) cannywhite = mp.canny(image) morphsum = mp.morph(image) H,S,V = mp.rataHSV(image) diamA, diamB = mp.diameterDetect(image) red,green,blue = mp.rataRGB(image) global_feature = np.hstack([humoments, cannywhite, morphsum, H, S, V, diamA, diamB]) prediction = clf.predict(global_feature.reshape(1,-1))[0] return render_template('result.html', prediksi = prediction)
clf.fit(data, kelas) # Path dari data test test_path = "./data_test/" for file in glob.glob( test_path + "/*.jpg" ): #Melakukan loop ke semua file di dalam folder dan menambahkan nama filenya yang berekstensi .jpg image = cv2.imread(file) #Membaca image wpercent = (mywidth / float(image.size[0])) hsize = int((float(image.size[1]) * float(wpercent))) image = image.resize((mywidth, hsize), PIL.Image.ANTIALIAS) #Ekstrasi informasi dari gambar humoments = mp.hu_moments(image) cannywhite = mp.canny(image) morphsum = mp.morph(image) H, S, V = mp.rataHSV(image) diamA, diamB = mp.diameterDetect(image) #Menggabungkan informasi atribut global_feature = np.hstack( [humoments, cannywhite, morphsum, H, S, V, diamA, diamB]) # Melakukan prediksi gambar prediction = clf.predict(global_feature.reshape(1, -1))[0] print(prediction) # Jika ingin melihat hasil output gambar, hapus # pada kode dibawah #cv2.putText(image,prediction, (20,30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,255), 3) #plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) #plt.show() tanya = input("Apakah klasifikasi benar? Y/N") if (tanya == 'Y' or tanya == 'y'): local_feature = np.hstack([ humoments, cannywhite, morphsum, H, S, V, diamA, diamB, prediction