Ahmed235 commited on
Commit
95c2dbb
·
verified ·
1 Parent(s): 9e46b98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -18
app.py CHANGED
@@ -3,39 +3,49 @@ import tensorflow as tf
3
  import numpy as np
4
  import gradio as gr
5
  import io
 
6
 
7
  # Load the model
8
  model_path = 'final_teath_classifier.h5'
9
  model = tf.keras.models.load_model(model_path)
 
 
 
 
 
 
 
 
 
 
 
10
  # Define prediction function
11
  def predict_image(image):
 
12
  image_bytes = io.BytesIO()
13
  image.save(image_bytes, format="JPEG")
14
-
15
  # Load the image from the file-like object
16
  image = tf.keras.preprocessing.image.load_img(image_bytes, target_size=(256, 256))
17
- img_array = np.array(image) / 255.0
18
- image = np.expand_dims(img_array, axis=0)
19
-
20
- # Make a prediction
21
- prediction = model.predict(image)
22
-
23
- # Get the probability of being 'Good'
24
- probability_good = prediction[0][0] # Assuming it's a binary classification
25
-
26
- # Define the prediction result
27
- result = {
28
- "prediction": probability_good
29
- }
30
-
31
- return result
32
 
33
  # Create the interface
34
- input_interface = gr.Image(type="pil")
 
 
35
  iface = gr.Interface(
36
  fn=predict_image,
37
  inputs=input_interface,
38
- outputs="json")
39
 
40
  # Launch the interface
41
  iface.launch(share=True)
 
3
  import numpy as np
4
  import gradio as gr
5
  import io
6
+ import json
7
 
8
  # Load the model
9
  model_path = 'final_teath_classifier.h5'
10
  model = tf.keras.models.load_model(model_path)
11
+
12
+ # Define preprocessing function
13
+ def preprocess_image(image):
14
+ # Resize the image to match input size
15
+ #image = image.resize((256, 256))
16
+ # Convert image to array and preprocess input
17
+ image = tf.keras.preprocessing.image.img_to_array(image)
18
+ # Add batch dimension
19
+ img_array = np.expand_dims(image, axis=0)
20
+ return img_array
21
+
22
  # Define prediction function
23
  def predict_image(image):
24
+ # Save the image to a file-like object
25
  image_bytes = io.BytesIO()
26
  image.save(image_bytes, format="JPEG")
 
27
  # Load the image from the file-like object
28
  image = tf.keras.preprocessing.image.load_img(image_bytes, target_size=(256, 256))
29
+ img_array = preprocess_image(image)
30
+ outputs = model.predict(img_array)
31
+ predictions = tf.nn.softmax(outputs.logits, axis=-1)
32
+ predicted_class = np.argmax(predictions)
33
+ if predicted_class == 0:
34
+ predict_label = "Clean"
35
+ else:
36
+ predict_label = "Carries"
37
+ confidence = float(np.max(predictions))
38
+ prediction_dict = {"prediction": predict_label, "confidence": confidence}
39
+ return prediction_dict
 
 
 
 
40
 
41
  # Create the interface
42
+ input_interface = gr.Image(type = "pil")
43
+ output_interface = "json"
44
+
45
  iface = gr.Interface(
46
  fn=predict_image,
47
  inputs=input_interface,
48
+ outputs=gr.Textbox("output"))
49
 
50
  # Launch the interface
51
  iface.launch(share=True)