Update app.py
Browse files
app.py
CHANGED
@@ -40,12 +40,49 @@ import shutil
|
|
40 |
custom_title = "<span style='color: rgb(243, 239, 224);'>Green Greta</span>"
|
41 |
|
42 |
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
image_gradio_app = gr.Interface(
|
51 |
fn=predict_image,
|
|
|
40 |
custom_title = "<span style='color: rgb(243, 239, 224);'>Green Greta</span>"
|
41 |
|
42 |
|
43 |
+
from huggingface_hub import from_pretrained_keras
|
44 |
+
|
45 |
+
import tensorflow as tf
|
46 |
+
from tensorflow import keras
|
47 |
+
from PIL import Image
|
48 |
|
49 |
+
# Cell 1: Image Classification Model
|
50 |
+
model1 = from_pretrained_keras("rocioadlc/EfficientNetV2L")
|
51 |
+
|
52 |
+
class_labels = ['battery',
|
53 |
+
'biological',
|
54 |
+
'brown-glass',
|
55 |
+
'cardboard',
|
56 |
+
'clothes',
|
57 |
+
'green-glass',
|
58 |
+
'metal',
|
59 |
+
'paper',
|
60 |
+
'plastic',
|
61 |
+
'shoes',
|
62 |
+
'trash',
|
63 |
+
'white-glass']
|
64 |
+
|
65 |
+
def predict_image(input):
|
66 |
+
# Resize the image to the size expected by the model
|
67 |
+
image = input.resize((244, 224))
|
68 |
+
# Convert the image to a NumPy array
|
69 |
+
image_array = tf.keras.preprocessing.image.img_to_array(image)
|
70 |
+
# Normalize the image
|
71 |
+
image_array /= 255.0
|
72 |
+
# Expand the dimensions to create a batch
|
73 |
+
image_array = tf.expand_dims(image_array, 0)
|
74 |
+
# Predict using the model
|
75 |
+
predictions = model1.predict(image_array)
|
76 |
+
|
77 |
+
# Get the predicted class label
|
78 |
+
predicted_class_index = np.argmax(predictions, -1)
|
79 |
+
predicted_class_label = class_labels[predicted_class_index]
|
80 |
+
|
81 |
+
# Get the confidence score of the predicted class
|
82 |
+
confidence_score = predictions[0][predicted_class_index]
|
83 |
+
|
84 |
+
# Return predicted class label and confidence score
|
85 |
+
return {predicted_class_label: confidence_score}
|
86 |
|
87 |
image_gradio_app = gr.Interface(
|
88 |
fn=predict_image,
|