Tree
Tree
#-----------------------------------
# GLOBAL FEATURE EXTRACTION
#-----------------------------------
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MinMaxScaler
import numpy as np
import mahotas
import cv2
import os
import h5py
#--------------------
# tunable-parameters
#--------------------
images_per_class = 800
fixed_size = tuple((500, 500))
train_path = r"/content/drive/MyDrive/sharunplant/archive/dataset/train"
h5_train_data = r"/content/drive/MyDrive/sharunplant/train_data.h5/train_data.h5"
h5_train_labels = r"/content/drive/MyDrive/sharunplant/train_labels.h5"
bins = 8
def rgb_bgr(image):
rgb_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return rgb_img
def bgr_hsv(rgb_img):
hsv_img = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2HSV)
return hsv_img
# image segmentation
def img_segmentation(rgb_img,hsv_img):
lower_green = np.array([25,0,20])
upper_green = np.array([100,255,255])
healthy_mask = cv2.inRange(hsv_img, lower_green, upper_green)
result = cv2.bitwise_and(rgb_img,rgb_img, mask=healthy_mask)
lower_brown = np.array([10,0,10])
upper_brown = np.array([30,255,255])
disease_mask = cv2.inRange(hsv_img, lower_brown, upper_brown)
disease_result = cv2.bitwise_and(rgb_img, rgb_img, mask=disease_mask)
final_mask = healthy_mask + disease_mask
final_result = cv2.bitwise_and(rgb_img, rgb_img, mask=final_mask)
return final_result
# feature-descriptor-1: Hu Moments
def fd_hu_moments(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
feature = cv2.HuMoments(cv2.moments(image)).flatten()
return feature
['diseased', 'healthy']
RGB_BGR = rgb_bgr(image)
BGR_HSV = bgr_hsv(RGB_BGR)
IMG_SEGMENT = img_segmentation(RGB_BGR,BGR_HSV)
fv_hu_moments = fd_hu_moments(IMG_SEGMENT)
fv_haralick = fd_haralick(IMG_SEGMENT)
fv_histogram = fd_histogram(IMG_SEGMENT)
# Concatenate
h5f_data.close()
h5f_label.close()
# training
#-----------------------------------
# TRAINING OUR MODEL
#-----------------------------------
import h5py
import numpy as np
import os
import glob
import cv2
import warnings
from matplotlib import pyplot
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.model_selection import KFold, StratifiedKFold
from sklearn.metrics import confusion_matrix, accuracy_score, classification_report
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
#from sklearn.externals import joblib
from joblib import dump, load
warnings.filterwarnings('ignore')
#--------------------
# tunable-parameters
#--------------------
num_trees = 100
test_size = 0.20
seed = 9
train_path = r"/content/drive/MyDrive/sharunplant/archive/dataset/train"
test_path = "dataset/test"
h5_train_data =r"/content/drive/MyDrive/sharunplant/train_data.h5/train_data.h5"
h5_train_labels = r"/content/drive/MyDrive/sharunplant/train_labels.h5"
scoring = "accuracy"
if not os.path.exists(test_path):
os.makedirs(test_path)
global_features_string = h5f_data['dataset_1']
global_labels_string = h5f_label['dataset_1']
global_features = np.array(global_features_string)
global_labels = np.array(global_labels_string)
h5f_data.close()
h5f_label.close()
print(trainDataGlobal)
clf.fit(trainDataGlobal, trainLabelsGlobal)
▾ RandomForestClassifier
RandomForestClassifier(random_state=9)
y_predict=clf.predict(testDataGlobal)
print(y_predict)
[1 1 1 0 0 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1 0 1 0 0 0 0 0 0 1 1 1 0 1 0 1
1 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 1 1 1 0 1 0 0 1 0 0 0 1 1 1 0 0 0 1 0
0 1 0 1 1 0 1 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 0 1 1 0 1 0
1 0 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0 1 1 0 1 0 1 0 0 0 1 1 0 0 0 1 1 0 0
0 1 0 1 0 1 0 0 1 1 0 0 0 1 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 1 1 0 0 1 0 0
1 1 1 1 0 1 1 1 0 1 0 0 0 0 1 0 1 0 0 0 1 0 0 1 1 0 0 1 1 1 1 0 1 1 1 0 0
1 0 1 1 0 0 0 0 0 1 1 0 1 1 1 1 0 1 0 1 1 0 1 1 1 0 1 1 0 0 0 0 0 0 0 1 1
1 1 1 1 1 1 0 1 0 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1
1 1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 1 1 0 0 1 1 0 0]
cm = confusion_matrix(testLabelsGlobal,y_predict)
import seaborn as sns
sns.heatmap(cm ,annot=True)
<Axes: >
print(classification_report(testLabelsGlobal,y_predict))
0.98125
import joblib
['trained_model.pkl']
import cv2
import numpy as np
import joblib
def extract_features(image):
# Extract features using the same feature extraction functions used during training
# You can reuse fd_hu_moments, fd_haralick, and fd_histogram functions from your code
rgb_bgr_image = rgb_bgr(image)
bgr_hsv_image = bgr_hsv(rgb_bgr_image)
segmented_image = img_segmentation(rgb_bgr_image, bgr_hsv_image)
feature_vector = np.hstack([fd_histogram(segmented_image), fd_haralick(segmented_image), fd_hu_moments(seg
return feature_vector