Skip to content

Commit a932129

Browse files
Latest
1 parent 844bd5b commit a932129

19 files changed

+934
-662
lines changed

.vscode/PythonImportHelper-v2-Completion.json

+225-26
Large diffs are not rendered by default.

CNN_RGB.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
# Define constants
77
IMAGE_SIZE = (64, 64) # You can adjust this based on your image size
8-
BATCH_SIZE = 32
8+
BATCH_SIZE = 16
99
NUM_CLASSES = 3 # Defective, Raw, Ripened
10-
EPOCHS = 20
10+
EPOCHS = 6
1111

1212
# Data generators
1313
train_datagen = keras.preprocessing.image.ImageDataGenerator(

classification.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from sklearn.ensemble import RandomForestClassifier
66
from sklearn.svm import SVC
77
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
8-
from sklearn.metrics import accuracy_score
8+
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
99
from sklearn.impute import SimpleImputer
1010

1111
# Load CSV files
@@ -29,15 +29,17 @@
2929
y = combined_df["Label"]
3030

3131
# Impute missing values (NaN) with mean of the column
32-
imputer = SimpleImputer(strategy='mean')
32+
imputer = SimpleImputer(strategy="mean")
3333
X_imputed = imputer.fit_transform(X)
3434

3535
# Scale the features
3636
scaler = StandardScaler()
3737
X_scaled = scaler.fit_transform(X_imputed)
3838

3939
# Perform 80:20 train-test split
40-
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=37)
40+
X_train, X_test, y_train, y_test = train_test_split(
41+
X_scaled, y, test_size=0.2, random_state=37
42+
)
4143

4244
# Initialize classifiers
4345
classifiers = {
@@ -49,16 +51,30 @@
4951

5052
# Train and evaluate classifiers
5153
results = {}
54+
precision = {}
55+
recall = {}
56+
f1 = {}
5257
for name, clf in classifiers.items():
5358
clf.fit(X_train, y_train)
5459
y_pred = clf.predict(X_test)
5560
accuracy = accuracy_score(y_test, y_pred)
5661
results[name] = accuracy
62+
precision[name] = precision_score(y_test, y_pred, average="weighted")
63+
recall[name] = recall_score(y_test, y_pred, average="weighted")
64+
f1[name] = f1_score(y_test, y_pred, average="weighted")
5765
print(f"{name} Accuracy: {accuracy}")
5866

5967
# Save results to a DataFrame
6068
results_df = pd.DataFrame.from_dict(results, orient="index", columns=["Accuracy"])
6169
results_df.index.name = "Classifier"
62-
results_csv = "results/thermal/resultsWithGLCMAndColorOTSU.csv"
70+
results_csv = "results/thermal/resultsWithGLCMAndColor.csv"
6371
results_df.to_csv(results_csv)
6472
print(f"Results saved to {results_csv}")
73+
74+
# Print precision, recall, and F1-score
75+
print("\nMetrics:")
76+
for name in classifiers.keys():
77+
print(f"{name}:")
78+
print(f" Precision: {precision[name]}")
79+
print(f" Recall: {recall[name]}")
80+
print(f" F1-score: {f1[name]}")

classification_RGB.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
from sklearn.ensemble import RandomForestClassifier
66
from sklearn.svm import SVC
77
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
8-
from sklearn.metrics import accuracy_score
8+
from sklearn.metrics import (
9+
accuracy_score,
10+
confusion_matrix,
11+
precision_score,
12+
recall_score,
13+
f1_score,
14+
)
915
from sklearn.impute import SimpleImputer
1016

1117
# Load CSV files
@@ -49,16 +55,36 @@
4955

5056
# Train and evaluate classifiers
5157
results = {}
58+
confusion_matrices = {}
5259
for name, clf in classifiers.items():
5360
clf.fit(X_train, y_train)
5461
y_pred = clf.predict(X_test)
5562
accuracy = accuracy_score(y_test, y_pred)
63+
confusion_matrices[name] = confusion_matrix(y_test, y_pred)
5664
results[name] = accuracy
5765
print(f"{name} Accuracy: {accuracy}")
5866

5967
# Save results to a DataFrame
6068
results_df = pd.DataFrame.from_dict(results, orient="index", columns=["Accuracy"])
6169
results_df.index.name = "Classifier"
62-
results_csv = "results/rgb/resultsWithGLCMAndColorOTSU_RGB.csv"
70+
results_csv = "results/rgb/resultsWithGLCMAndColor_RGB.csv"
6371
results_df.to_csv(results_csv)
6472
print(f"Results saved to {results_csv}")
73+
74+
# Compute precision, recall, and F1-score
75+
precision = {}
76+
recall = {}
77+
f1 = {}
78+
for name, cm in confusion_matrices.items():
79+
y_pred = classifiers[name].predict(X_test)
80+
precision[name] = precision_score(y_test, y_pred, average="weighted")
81+
recall[name] = recall_score(y_test, y_pred, average="weighted")
82+
f1[name] = f1_score(y_test, y_pred, average="weighted")
83+
84+
# Print metrics
85+
print("\nMetrics:")
86+
for name in classifiers.keys():
87+
print(f"{name}:")
88+
print(f" Precision: {precision[name]}")
89+
print(f" Recall: {recall[name]}")
90+
print(f" F1-score: {f1[name]}")

classify.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import tensorflow
4+
from tensorflow.keras.preprocessing import image
5+
6+
# Load the trained model
7+
model = tensorflow.keras.models.load_model("mango_classification_model_RGB.h5")
8+
9+
# Load the defective image
10+
defective_image_path = (
11+
"data/RGB data/test/defective/IMG20230519180913_jpg.rf.3e5798971b05532ed162e96e4166a5aa (2).jpg" # Update with the path to your defective image
12+
)
13+
defective_img = image.load_img(
14+
defective_image_path, target_size=(64, 64)
15+
) # Resize as needed
16+
defective_img_array = image.img_to_array(defective_img)
17+
defective_img_array = np.expand_dims(defective_img_array, axis=0)
18+
defective_img_array /= 255.0 # Normalize the image
19+
20+
# Predict the class label
21+
predicted_class = model.predict(defective_img_array)
22+
class_label = np.argmax(predicted_class)
23+
24+
# Define class labels
25+
class_labels = {0: "Defective", 1: "Raw", 2: "Ripened"} # Update with your class labels
26+
27+
# Display the image and predicted class label
28+
plt.imshow(defective_img)
29+
plt.title(f"Predicted Class: {class_labels[class_label]}")
30+
plt.axis("off")
31+
plt.show()

mango_classification_model_RGB.h5

0 Bytes
Binary file not shown.

preprocessing/histogramEqualizer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def load_image(image_path):
77
return img
88

99

10-
image_path = 'data/RGB data/Defective/IMG20230519180913.jpg'
10+
image_path = "../data/RGB data/test/defective/IMG20230519180913_jpg.rf.3e5798971b05532ed162e96e4166a5aa (2).jpg"
1111
# image_path = "data/RGB data/Raw/IMG20230703125745.jpg"
1212
# image_path ='data/RGB data/Ripened/IMG20230703130447.jpg'
1313
# image_path = 'data/Thermal imaging/test/defective/Mango_5_jpg.rf.73ebfaf73a2e0a8ee6411ae9c549083a.jpg'

preprocessing/otsu.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# read the image
66
image = cv2.imread(
7-
"../data/RGB data/train/defective/IMG20230519180945_jpg.rf.5ca9a11519bae6b5bf1af3017b426ca8 (2).jpg"
7+
"../data/RGB data/test/raw/IMG20230703125745_jpg.rf.1a157bf80a0a09e48380dfdf24630525.jpg"
88
)
99

1010
# convert it to grayscale

results/rgb/resultsWithCNN.csv

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Accuracy
2-
1.0
2+
0.9545454382896423

results/rgb/resultsWithColor_RGB.csv

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Classifier,Accuracy
2-
KNN,0.8620689655172413
3-
Random Forest,0.9137931034482759
4-
SVM,1.0
5-
LDA,0.9137931034482759
2+
KNN,0.9310344827586207
3+
Random Forest,0.9310344827586207
4+
SVM,0.9827586206896551
5+
LDA,1.0
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Classifier,Accuracy
2-
KNN,0.896551724137931
2+
KNN,0.9310344827586207
33
Random Forest,0.9310344827586207
4-
SVM,1.0
4+
SVM,0.9827586206896551
55
LDA,1.0

results/thermal/resultsWithColor.csv

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Classifier,Accuracy
2-
KNN,0.734375
3-
Random Forest,0.78125
4-
SVM,0.765625
5-
LDA,0.734375
2+
KNN,0.609375
3+
Random Forest,0.734375
4+
SVM,0.65625
5+
LDA,0.65625

results/thermal/resultsWithGLCM.csv

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Classifier,Accuracy
2-
KNN,0.359375
3-
Random Forest,0.5
4-
SVM,0.3125
5-
LDA,0.328125
2+
KNN,0.5625
3+
Random Forest,0.609375
4+
SVM,0.53125
5+
LDA,0.515625
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Classifier,Accuracy
2-
KNN,0.75
3-
Random Forest,0.6875
4-
SVM,0.8125
5-
LDA,0.765625
2+
KNN,0.6875
3+
Random Forest,0.765625
4+
SVM,0.84375
5+
LDA,0.8125

test.csv

+33-33
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
Contrast,Correlation,Avg_Red,Avg_Green,Avg_Blue,Label
2-
623.7052284249604,0.9575111483791071,85.293310546875,124.560654296875,68.10009765625,defective
3-
504.0618730591513,0.9605854499540133,79.420947265625,119.43806640625,48.720869140625,defective
4-
723.2186363915142,0.9197126544659211,39.92380859375,74.7543359375,71.2094140625,defective
5-
831.1257604882682,0.9031763812404133,37.4070703125,65.7117578125,61.3258984375,defective
6-
714.7442624516852,0.9370992026310151,58.941396484375,106.592705078125,101.683251953125,defective
7-
1655.768248809685,0.8901809909634287,137.188369140625,128.8351953125,91.481064453125,defective
8-
981.2594429963593,0.9322814782997321,132.9951953125,117.724716796875,67.659521484375,defective
9-
888.6744584500657,0.9225627060248748,104.027841796875,72.470341796875,32.373271484375,defective
10-
1340.7598211531597,0.9051736120564143,144.59279296875,133.65455078125,81.55158203125,defective
11-
1480.0028056426356,0.9010825674440476,111.71595703125,86.647353515625,43.77658203125,defective
12-
1361.0931798480472,0.9093542214172917,109.984794921875,111.296123046875,45.37701171875,Raw
13-
1077.8788587690308,0.9012879797696387,51.403583984375,47.726943359375,10.824873046875,Raw
14-
1504.0026466150957,0.89207450769976,75.385283203125,70.029130859375,26.09359375,Raw
15-
1182.3193325075624,0.8554381183493754,40.816318359375,35.84876953125,8.896201171875,Raw
16-
1309.8215333464548,0.8846837831984499,55.925029296875,47.50673828125,15.789150390625,Raw
17-
2578.2693983576114,0.8454913163999036,111.278359375,101.82787109375,63.04935546875,Raw
18-
1360.577449078797,0.9153638314555895,91.4822265625,82.611416015625,35.298310546875,Raw
19-
1586.9567839649687,0.9034760734475067,122.5566796875,117.845322265625,63.475380859375,Raw
20-
1507.7220994884276,0.889493963398244,80.306298828125,71.073359375,29.82517578125,Raw
21-
794.7188346545271,0.9457267630136313,130.727861328125,122.894248046875,75.649697265625,Raw
22-
1943.2798934678522,0.8624722032131387,118.8928125,97.62328125,51.308330078125,Raw
23-
1293.9057470471691,0.9104208787174929,127.813798828125,116.34275390625,75.691650390625,Raw
24-
2072.7282372073855,0.8472459812415448,106.68798828125,78.77357421875,47.77654296875,Raw
25-
830.4843566378772,0.9394909991527847,130.687158203125,116.010322265625,58.08513671875,Raw
26-
1250.7611548493358,0.9107782666254343,91.83458984375,64.28552734375,25.83955078125,Ripened
27-
765.2680594277003,0.944723325983793,157.42982421875,143.74787109375,85.115234375,Ripened
28-
1539.1406096593191,0.8893231820591607,87.53939453125,63.3770703125,26.59134765625,Ripened
29-
2421.4400222195018,0.8417412575465559,117.995771484375,114.029345703125,75.447978515625,Ripened
30-
1868.53933491248,0.8871655217229618,118.9267578125,114.020869140625,74.68669921875,Ripened
31-
960.0888430965124,0.9116427250506746,58.1466796875,46.25865234375,16.5309765625,Ripened
32-
1377.2611851777904,0.9098801025137525,91.573193359375,82.286396484375,40.109697265625,Ripened
33-
1459.448517959607,0.9106023296502229,110.892119140625,109.418349609375,65.137109375,Ripened
34-
1621.0109925292527,0.8974034997341761,96.724130859375,89.5792578125,51.80416015625,Ripened
2+
453.6139189400098,0.9625124692684074,89.948916015625,130.297685546875,90.737470703125,defective
3+
291.49002808653063,0.9734651008255244,83.8688671875,123.4744140625,67.7666015625,defective
4+
426.0629926779097,0.9415985536346776,45.327109375,81.976611328125,99.046513671875,defective
5+
513.3002126999205,0.9285425252678633,42.821630859375,72.42203125,87.750185546875,defective
6+
384.23941074423095,0.9552435879810321,65.650869140625,114.62109375,126.355166015625,defective
7+
951.7421134328047,0.9182003292534576,145.94220703125,136.331328125,108.894814453125,defective
8+
469.3097247006235,0.9590862698510012,145.4925390625,124.49232421875,84.222158203125,defective
9+
297.4529971569952,0.9651782716362195,138.720087890625,81.165869140625,47.033505859375,defective
10+
553.2443467729712,0.9458357895329647,158.312060546875,141.721875,99.864921875,defective
11+
255.7587940209692,0.9698561612359357,164.8953515625,108.273154296875,67.280712890625,defective
12+
369.56229739537383,0.9551107867737039,159.84560546875,128.788232421875,59.751630859375,Raw
13+
413.51760154032695,0.939507675011654,135.1898828125,69.09201171875,30.184111328125,Raw
14+
447.90476035708093,0.9452068488354195,145.517880859375,92.876337890625,47.2473046875,Raw
15+
579.2305067999656,0.8981954957986541,123.48091796875,52.392197265625,23.487451171875,Raw
16+
500.7947832902558,0.9305874709236024,140.553251953125,68.17951171875,36.827177734375,Raw
17+
726.5514351444548,0.9095886598080348,168.011669921875,126.34453125,90.255107421875,Raw
18+
253.4033503613264,0.9686471571976701,172.418837890625,106.961552734375,57.65466796875,Raw
19+
469.6485969793773,0.9478571587524837,169.161220703125,135.675576171875,83.06875,Raw
20+
502.1071851637762,0.9403132155030137,145.42203125,88.5787109375,48.457978515625,Raw
21+
394.6255448781617,0.9652296452821013,138.61537109375,130.3225,95.083330078125,Raw
22+
624.6314405451548,0.9341507846355639,137.12025390625,113.752841796875,73.18611328125,Raw
23+
743.6530077516858,0.9324542552627961,137.228740234375,124.795009765625,92.985751953125,Raw
24+
788.1462933848402,0.9187018304867761,127.608330078125,94.3251953125,67.871591796875,Raw
25+
390.9812471843343,0.9643030387147639,138.115263671875,122.619404296875,77.70212890625,Raw
26+
321.5977653394643,0.9647793300544506,152.866796875,83.84869140625,46.98640625,Ripened
27+
320.2456092649854,0.9724783886231493,165.3382421875,147.856015625,93.733193359375,Ripened
28+
417.62740394091935,0.9511242662985734,149.1058984375,86.672890625,51.1206640625,Ripened
29+
738.9835246537876,0.9121470377380702,158.250107421875,131.969287109375,96.16048828125,Ripened
30+
676.3700066108722,0.9371262122420289,150.894931640625,129.228466796875,94.780205078125,Ripened
31+
421.21371278927955,0.9437046367358531,135.084091796875,61.666845703125,37.06755859375,Ripened
32+
398.5875833151338,0.9541327501186646,156.325263671875,101.885322265625,64.84625,Ripened
33+
489.93776169375224,0.9516951203195602,151.8007421875,125.191806640625,86.49818359375,Ripened
34+
643.5776351599403,0.9351086114230407,146.390703125,106.39341796875,75.1730078125,Ripened

0 commit comments

Comments
 (0)