digitalhub483 commited on
Commit
3aba4dd
·
verified ·
1 Parent(s): e008c2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -83
app.py CHANGED
@@ -1,84 +1,84 @@
1
- # import numpy as np
2
- # from tensorflow.keras.models import load_model
3
- # from tensorflow.keras.preprocessing import image
4
-
5
- # # Load trained model
6
- # model_path = r"Icream_pizza.model.h5"
7
- # model = load_model(model_path)
8
-
9
- # print("Model Loaded Successfully!")
10
-
11
- # # Image path
12
- # img_path = r"test_digit.png"
13
-
14
- # # Load image
15
- # img = image.load_img(img_path, target_size=(150, 150))
16
- # img_array = image.img_to_array(img) / 255.0
17
- # img_array = np.expand_dims(img_array, axis=0)
18
-
19
- # # Predict
20
- # prediction = model.predict(img_array)[0][0]
21
-
22
- # # Binary class probabilities
23
- # class_1_prob = float(prediction) # sigmoid output
24
- # class_0_prob = 1 - class_1_prob
25
-
26
- # print("\nBoth Class Probabilities:\n")
27
- # print(f"Class 0 Probability: {class_0_prob * 100:.2f}%")
28
- # print(f"Class 1 Probability: {class_1_prob * 100:.2f}%")
29
-
30
- # # Final predicted class
31
- # if prediction >= 0.5:
32
- # print("\nPredicted Class: 1")
33
- # print(f"Confidence: {class_1_prob * 100:.2f}%")
34
- # else:
35
- # print("\nPredicted Class: 0")
36
- # print(f"Confidence: {class_0_prob * 100:.2f}%")
37
-
38
-
39
- # gradio app
40
- import numpy as np
41
- import gradio as gr
42
- from tensorflow.keras.models import load_model
43
- from tensorflow.keras.preprocessing import image
44
-
45
- # Load model once
46
- model = load_model("pizza.model.h5")
47
-
48
-
49
- def predict(img):
50
- # Resize to model input size
51
- img = img.resize((150, 150))
52
-
53
- # Convert to array
54
- img_array = image.img_to_array(img) / 255.0
55
- img_array = np.expand_dims(img_array, axis=0)
56
-
57
- # Prediction
58
- pred = model.predict(img_array)[0][0]
59
-
60
- class_1_prob = float(pred)
61
- class_0_prob = 1 - class_1_prob
62
-
63
- if pred >= 0.5:
64
- label = "Class 1 (Pizza)"
65
- else:
66
- label = "Class 0"
67
-
68
- return {
69
- "Class 0 Probability": f"{class_0_prob * 100:.2f}%",
70
- "Class 1 Probability": f"{class_1_prob * 100:.2f}%",
71
- "Prediction": label
72
- }
73
-
74
-
75
- # Gradio UI
76
- app = gr.Interface(
77
- fn=predict,
78
- inputs=gr.Image(type="pil"),
79
- outputs="json",
80
- title="Binary Image Classifier Pizza",
81
- description="Upload an image to classify between 2 classes using CNN model"
82
- )
83
-
84
  app.launch()
 
1
+ # import numpy as np #ldfjlad
2
+ # from tensorflow.keras.models import load_model
3
+ # from tensorflow.keras.preprocessing import image
4
+
5
+ # # Load trained model
6
+ # model_path = r"Icream_pizza.model.h5"
7
+ # model = load_model(model_path)
8
+
9
+ # print("Model Loaded Successfully!")
10
+
11
+ # # Image path
12
+ # img_path = r"test_digit.png"
13
+
14
+ # # Load image
15
+ # img = image.load_img(img_path, target_size=(150, 150))
16
+ # img_array = image.img_to_array(img) / 255.0
17
+ # img_array = np.expand_dims(img_array, axis=0)
18
+
19
+ # # Predict
20
+ # prediction = model.predict(img_array)[0][0]
21
+
22
+ # # Binary class probabilities
23
+ # class_1_prob = float(prediction) # sigmoid output
24
+ # class_0_prob = 1 - class_1_prob
25
+
26
+ # print("\nBoth Class Probabilities:\n")
27
+ # print(f"Class 0 Probability: {class_0_prob * 100:.2f}%")
28
+ # print(f"Class 1 Probability: {class_1_prob * 100:.2f}%")
29
+
30
+ # # Final predicted class
31
+ # if prediction >= 0.5:
32
+ # print("\nPredicted Class: 1")
33
+ # print(f"Confidence: {class_1_prob * 100:.2f}%")
34
+ # else:
35
+ # print("\nPredicted Class: 0")
36
+ # print(f"Confidence: {class_0_prob * 100:.2f}%")
37
+
38
+
39
+ # gradio app
40
+ import numpy as np
41
+ import gradio as gr
42
+ from tensorflow.keras.models import load_model
43
+ from tensorflow.keras.preprocessing import image
44
+
45
+ # Load model once
46
+ model = load_model("pizza.model.h5")
47
+
48
+
49
+ def predict(img):
50
+ # Resize to model input size
51
+ img = img.resize((150, 150))
52
+
53
+ # Convert to array
54
+ img_array = image.img_to_array(img) / 255.0
55
+ img_array = np.expand_dims(img_array, axis=0)
56
+
57
+ # Prediction
58
+ pred = model.predict(img_array)[0][0]
59
+
60
+ class_1_prob = float(pred)
61
+ class_0_prob = 1 - class_1_prob
62
+
63
+ if pred >= 0.5:
64
+ label = "Class 1 (Pizza)"
65
+ else:
66
+ label = "Class 0"
67
+
68
+ return {
69
+ "Class 0 Probability": f"{class_0_prob * 100:.2f}%",
70
+ "Class 1 Probability": f"{class_1_prob * 100:.2f}%",
71
+ "Prediction": label
72
+ }
73
+
74
+
75
+ # Gradio UI
76
+ app = gr.Interface(
77
+ fn=predict,
78
+ inputs=gr.Image(type="pil"),
79
+ outputs="json",
80
+ title="Binary Image Classifier Pizza",
81
+ description="Upload an image to classify between 2 classes using CNN model"
82
+ )
83
+
84
  app.launch()