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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -23
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(image):
13
- # Convert the numpy array to a PIL Image object
14
- pil_image = Image.fromarray(np.uint8(image)).convert('RGB')
 
 
 
 
 
15
 
16
- # Resize the image
17
- pil_image = pil_image.resize((224, 224))
18
 
19
- # Convert the PIL Image object to a numpy array
20
- image_array = np.array(pil_image)
21
 
22
- # Normalize the image
23
- normalized_image_array = (image_array.astype(np.float32) / 255.0)
24
 
25
- # Reshape the image
26
- data = normalized_image_array.reshape((1, 224, 224, 3))
27
 
28
- # Make the prediction
29
- prediction = model.predict(data)[0]
30
 
31
- # Get the predicted class name
32
- predicted_class = class_names[np.argmax(prediction)]
33
 
34
- # Get the confidence score for the predicted class
35
- confidence_score = np.max(prediction)
36
 
37
- # Return the predicted class and confidence score
38
- return f"{predicted_class} ({confidence_score*100:.2f}%)"
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, title="Image Classification", description="Classify an image into one of six classes: Phosphorus, Magnesium, Nitrogen,Potassium, Calcium, Sulfur.")
 
 
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()