Kresna commited on
Commit
d5bd88f
·
1 Parent(s): 9f8bd32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -30
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
@@ -7,48 +9,40 @@ from PIL import Image
7
  model = tf.keras.models.load_model('Adam_8_1000_Acc 0.88_Nutrient-Model.h5')
8
 
9
  # Define the class names
10
- class_names = ['Calcium', 'Magnesium', 'Nitrogen', 'Phosphorus', 'Potassium', 'Sulfur']
11
-
12
  # Function to classify the image
13
- def classify_image(images):
14
- predictions = []
15
- for image in images:
16
- # Convert the numpy array to a PIL Image object
17
- pil_image = Image.fromarray(np.uint8(image)).convert('RGB')
18
-
19
- # Resize the image
20
- pil_image = pil_image.resize((224, 224))
21
 
22
- # Convert the PIL Image object to a numpy array
23
- image_array = np.array(pil_image)
24
 
25
- # Normalize the image
26
- normalized_image_array = (image_array.astype(np.float32) / 255.0)
27
 
28
- # Reshape the image
29
- data = normalized_image_array.reshape((1, 224, 224, 3))
30
 
31
- # Make the prediction
32
- prediction = model.predict(data)[0]
33
 
34
- # Get the predicted class name
35
- predicted_class = class_names[np.argmax(prediction)]
36
 
37
- # Get the confidence score for the predicted class
38
- confidence_score = np.max(prediction)
39
 
40
- # Append the predicted class and confidence score to the predictions list
41
- predictions.append(f"{predicted_class} ({confidence_score*100:.2f}%)")
42
 
43
- # Return the list of predictions
44
- return predictions
45
 
46
  # Define the Gradio interface
47
- inputs = gr.inputs.Image(type='numpy', label="Upload or Select Images", multiple=True)
48
  outputs = gr.outputs.Textbox()
49
- interface = gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs,
50
- title="Image Classification",
51
- description="Classify multiple images into one of six classes: Phosphorus, Magnesium, Nitrogen, Potassium, Calcium, Sulfur.")
52
 
53
  # Launch the interface
54
  interface.launch()
 
1
+ # app.py
2
+
3
  import gradio as gr
4
  import tensorflow as tf
5
  import numpy as np
 
9
  model = tf.keras.models.load_model('Adam_8_1000_Acc 0.88_Nutrient-Model.h5')
10
 
11
  # Define the class names
12
+ class_names = ['Calcium','Magnesium','Nitrogen','Phosphorus','Potassium','Sulfur']
 
13
  # Function to classify the image
14
+ def classify_image(image):
15
+ # Convert the numpy array to a PIL Image object
16
+ pil_image = Image.fromarray(np.uint8(image)).convert('RGB')
 
 
 
 
 
17
 
18
+ # Resize the image
19
+ pil_image = pil_image.resize((224, 224))
20
 
21
+ # Convert the PIL Image object to a numpy array
22
+ image_array = np.array(pil_image)
23
 
24
+ # Normalize the image
25
+ normalized_image_array = (image_array.astype(np.float32) / 255.0)
26
 
27
+ # Reshape the image
28
+ data = normalized_image_array.reshape((1, 224, 224, 3))
29
 
30
+ # Make the prediction
31
+ prediction = model.predict(data)[0]
32
 
33
+ # Get the predicted class name
34
+ predicted_class = class_names[np.argmax(prediction)]
35
 
36
+ # Get the confidence score for the predicted class
37
+ confidence_score = np.max(prediction)
38
 
39
+ # Return the predicted class and confidence score
40
+ return f"{predicted_class} ({confidence_score*100:.2f}%)"
41
 
42
  # Define the Gradio interface
43
+ inputs = gr.inputs.Image()
44
  outputs = gr.outputs.Textbox()
45
+ interface = gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs, title="Image Classification", description="Classify an image into one of six classes: Phosphorus, Magnesium, Nitrogen,Potassium, Calcium, Sulfur.")
 
 
46
 
47
  # Launch the interface
48
  interface.launch()