Spaces:
Sleeping
Sleeping
Commit
·
891e0e9
1
Parent(s):
bf24ba2
Update app.py
Browse files
app.py
CHANGED
@@ -7,54 +7,56 @@ from tensorflow.keras.preprocessing import image
|
|
7 |
from tensorflow.keras.models import load_model
|
8 |
from tensorflow.keras.applications.efficientnet import preprocess_input
|
9 |
|
10 |
-
|
11 |
model = load_model("efficent_net224B0.h5")
|
12 |
|
13 |
-
|
14 |
waste_labels = {0: 'Fibres', 1: 'Nanowires', 2: 'Particles', 3: 'Powder'}
|
15 |
|
16 |
-
|
17 |
def classify_image(pil_image):
|
18 |
-
|
19 |
img = image.img_to_array(pil_image)
|
20 |
|
21 |
-
|
22 |
img = tf.image.resize(img, (224, 224))
|
23 |
|
24 |
-
|
25 |
img = np.expand_dims(img, axis=0)
|
26 |
|
27 |
-
|
28 |
img = preprocess_input(img)
|
29 |
|
30 |
-
|
31 |
prediction = model.predict(img)
|
32 |
|
33 |
-
|
34 |
predicted_class = np.argmax(prediction)
|
35 |
predicted_class_name = waste_labels[predicted_class]
|
36 |
confidence = prediction[0, np.argmax(prediction)]
|
37 |
|
38 |
-
|
39 |
class_names = list(waste_labels.values())
|
40 |
probabilities = prediction[0]
|
41 |
|
42 |
-
|
|
|
|
|
43 |
plt.bar(class_names, probabilities, color='blue')
|
44 |
plt.xlabel('Waste Classes')
|
45 |
plt.ylabel('Probability')
|
46 |
plt.title('Prediction Probabilities')
|
47 |
plt.savefig('prediction_plot.png')
|
48 |
|
49 |
-
|
50 |
output_text = f"Predicted Class: {predicted_class_name}, Confidence: {confidence:.4f}\n"
|
51 |
for class_name, prob in zip(class_names, probabilities):
|
52 |
output_text += f"{class_name}: {prob:.4f}\n"
|
53 |
|
54 |
return output_text, 'prediction_plot.png'
|
55 |
|
56 |
-
|
57 |
iface = gr.Interface(fn=classify_image, inputs="image", outputs=["text", "image"],live=True)
|
58 |
|
59 |
-
|
60 |
iface.launch()
|
|
|
7 |
from tensorflow.keras.models import load_model
|
8 |
from tensorflow.keras.applications.efficientnet import preprocess_input
|
9 |
|
10 |
+
|
11 |
model = load_model("efficent_net224B0.h5")
|
12 |
|
13 |
+
|
14 |
waste_labels = {0: 'Fibres', 1: 'Nanowires', 2: 'Particles', 3: 'Powder'}
|
15 |
|
16 |
+
|
17 |
def classify_image(pil_image):
|
18 |
+
|
19 |
img = image.img_to_array(pil_image)
|
20 |
|
21 |
+
|
22 |
img = tf.image.resize(img, (224, 224))
|
23 |
|
24 |
+
|
25 |
img = np.expand_dims(img, axis=0)
|
26 |
|
27 |
+
|
28 |
img = preprocess_input(img)
|
29 |
|
30 |
+
|
31 |
prediction = model.predict(img)
|
32 |
|
33 |
+
|
34 |
predicted_class = np.argmax(prediction)
|
35 |
predicted_class_name = waste_labels[predicted_class]
|
36 |
confidence = prediction[0, np.argmax(prediction)]
|
37 |
|
38 |
+
|
39 |
class_names = list(waste_labels.values())
|
40 |
probabilities = prediction[0]
|
41 |
|
42 |
+
print(class_names)
|
43 |
+
print(probabilities)
|
44 |
+
|
45 |
plt.bar(class_names, probabilities, color='blue')
|
46 |
plt.xlabel('Waste Classes')
|
47 |
plt.ylabel('Probability')
|
48 |
plt.title('Prediction Probabilities')
|
49 |
plt.savefig('prediction_plot.png')
|
50 |
|
51 |
+
|
52 |
output_text = f"Predicted Class: {predicted_class_name}, Confidence: {confidence:.4f}\n"
|
53 |
for class_name, prob in zip(class_names, probabilities):
|
54 |
output_text += f"{class_name}: {prob:.4f}\n"
|
55 |
|
56 |
return output_text, 'prediction_plot.png'
|
57 |
|
58 |
+
|
59 |
iface = gr.Interface(fn=classify_image, inputs="image", outputs=["text", "image"],live=True)
|
60 |
|
61 |
+
|
62 |
iface.launch()
|