Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,40 +7,48 @@ 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 |
# Function to classify the image
|
12 |
-
def classify_image(
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
|
23 |
-
|
24 |
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
|
33 |
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
# Return the
|
38 |
-
return
|
39 |
|
40 |
# Define the Gradio interface
|
41 |
-
inputs = gr.inputs.Image()
|
42 |
outputs = gr.outputs.Textbox()
|
43 |
-
interface = gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs,
|
|
|
|
|
44 |
|
45 |
# Launch the interface
|
46 |
-
interface.launch()
|
|
|
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()
|