amirTamer commited on
Commit
c6b938d
·
1 Parent(s): 9b275e5

Add application file

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from skimage.feature import hog
4
+
5
+
6
+
7
+ # =========================
8
+ # PLACEHOLDER: LOAD MODEL
9
+ # =========================
10
+ # Example:
11
+ # import pickle
12
+ # with open("gnb_digit6_package.pkl", "rb") as f:
13
+ # package = pickle.load(f)
14
+ #
15
+ # model = package["model"]
16
+ # pca = package["pca"]
17
+ # best_weight = package["best_weight"]
18
+ # model_package = {
19
+ # "model": gnb,
20
+ # "pca": pca,
21
+ # "best_weight": best_weight,
22
+ # "hog_params": {
23
+ # "orientations": 9,
24
+ # "pixels_per_cell": (4, 4),
25
+ # "cells_per_block": (2, 2),
26
+ # "block_norm": "L2-Hys"
27
+ # }
28
+ # }
29
+
30
+ import pickle
31
+ with open("../pickleGNB/gnb_digit6_package.pkl" , "rb") as f:
32
+ package = pickle.load(f)
33
+ model = package['model']
34
+ pca = package['pca']
35
+ best_weight = package['best_weight']
36
+ hog_params = package['hog_params']
37
+
38
+ def preprocess_image(image):
39
+
40
+ image = np.array(image)
41
+ #Normalize
42
+ image_N = image / 255.0
43
+ image_hog = hog(
44
+ image_N,
45
+ **hog_params #Zay el ... fi el JS el spread
46
+ )
47
+ image_pca = pca.transform(image_hog)
48
+ return image_pca
49
+
50
+
51
+ def classify_image(image):
52
+ if image is None:
53
+ return "Please upload an image."
54
+
55
+ processedImage = preprocess_image(image)
56
+
57
+ pred = model.predict(
58
+ processedImage,
59
+ class_weights={0: 1.0, 1: best_weight}
60
+ )
61
+ if pred == 1:
62
+ return "Is 6"
63
+ else:
64
+ return "Not 6"
65
+
66
+
67
+ demo = gr.Interface(
68
+ fn=classify_image,
69
+ inputs=gr.Image(
70
+ image_mode="L",
71
+ type="numpy",
72
+ label="Upload Digit Image"
73
+ ),
74
+ outputs=gr.Textbox(label="Prediction"),
75
+ title="MNIST Digit 6 Classifier",
76
+ description="Upload an image and the model will classify whether it is digit 6 or not."
77
+ )
78
+ if __name__ == "__main__":
79
+ demo.launch()