rocioadlc commited on
Commit
48c2268
Β·
verified Β·
1 Parent(s): 984455c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -5
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
- # Cell 1: Image Classification Model
44
- image_pipeline = pipeline(task="image-classification", model="guillen/vit-basura-test1")
 
 
 
45
 
46
- def predict_image(input_img):
47
- predictions = image_pipeline(input_img)
48
- return {p["label"]: p["score"] for p in predictions}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,