Defkhan5960 commited on
Commit
891e0e9
·
1 Parent(s): bf24ba2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
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
- # Load the trained model
11
  model = load_model("efficent_net224B0.h5")
12
 
13
- # Define the classes
14
  waste_labels = {0: 'Fibres', 1: 'Nanowires', 2: 'Particles', 3: 'Powder'}
15
 
16
- # Define the Gradio interface
17
  def classify_image(pil_image):
18
- # Convert PIL.Image to Numpy array
19
  img = image.img_to_array(pil_image)
20
 
21
- # Resize to the model's expected input size
22
  img = tf.image.resize(img, (224, 224))
23
 
24
- # Expand dimensions to create a batch size of 1
25
  img = np.expand_dims(img, axis=0)
26
 
27
- # Preprocess the input for the EfficientNet model
28
  img = preprocess_input(img)
29
 
30
- # Make prediction
31
  prediction = model.predict(img)
32
 
33
- # Get predicted class and confidence
34
  predicted_class = np.argmax(prediction)
35
  predicted_class_name = waste_labels[predicted_class]
36
  confidence = prediction[0, np.argmax(prediction)]
37
 
38
- # Get class names and probabilities for all classes
39
  class_names = list(waste_labels.values())
40
  probabilities = prediction[0]
41
 
42
- # Plot bar chart and save it to a file
 
 
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
- # Prepare output text with all class probabilities
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
- # Create the Gradio interface
57
  iface = gr.Interface(fn=classify_image, inputs="image", outputs=["text", "image"],live=True)
58
 
59
- # Launch the Gradio interface
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()