Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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',
|
11 |
-
|
12 |
# Function to classify the image
|
13 |
-
def classify_image(
|
14 |
-
|
15 |
-
|
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 |
-
|
23 |
-
|
24 |
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
|
33 |
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
|
41 |
-
|
42 |
|
43 |
-
# Return the
|
44 |
-
return
|
45 |
|
46 |
# Define the Gradio interface
|
47 |
-
inputs = gr.inputs.Image(
|
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()
|