glin2 commited on
Commit
900b775
1 Parent(s): 648f21f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+
3
+ dt_model = joblib.load('dt_model.joblib')
4
+ rfc_model = joblib.load('rfc_model.joblib')
5
+ abc_model = joblib.load('abc_model.joblib')
6
+ knn_model = joblib.load('knn_model.joblib')
7
+
8
+ from sklearn.metrics.cluster import normalized_mutual_info_score
9
+ import cv2
10
+ import numpy as np
11
+
12
+ def preprocess_image(image):
13
+ grayscale_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
14
+ normalized_img = grayscale_img / 255.0
15
+ resized_img = cv2.resize(normalized_img, (28,28))
16
+ flattened_img = resized_img.flatten()
17
+ processed_img = np.array([flattened_img])
18
+
19
+ return processed_img
20
+
21
+ def pred(input1, input2):
22
+ class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]
23
+ processed_image = preprocess_image(input1)
24
+
25
+ if input2 == "Decision Tree":
26
+ preds = dt_model.predict_proba(processed_image)[0]
27
+ elif input2 == "Random Forest":
28
+ preds = rfc_model.predict_proba(processed_image)[0]
29
+ elif input2 == "AdaBoost":
30
+ preds = abc_model.predict_proba(processed_image)[0]
31
+ elif input2 == "KNN":
32
+ preds = knn_model.predict_proba(processed_image)[0]
33
+
34
+ #print(class_names)
35
+ #print(preds)
36
+
37
+ return {label: float(pred) for label, pred in zip(class_names, preds)}
38
+
39
+
40
+ input_module1 = gr.inputs.Image(label = "Input Sample Image")
41
+ input_module2 = gr.inputs.Dropdown(choices=['Decision Tree', 'Random Forest', 'AdaBoost', 'KNN'], label = "ML Models Dropdown")
42
+
43
+ output_module1 = gr.outputs.Label(label = "Predicted Class")
44
+
45
+ gr.Interface(fn=pred,
46
+ inputs=[input_module1,input_module2],
47
+ outputs=output_module1).launch(debug = True)