Ahmed235 commited on
Commit
263f3ae
1 Parent(s): 702241a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -1,9 +1,8 @@
1
- import json
 
2
  import numpy as np
3
  import gradio as gr
4
- import tensorflow as tf
5
- from PIL import Image
6
- from tensorflow.keras.models import load_model
7
 
8
  # Load the model
9
  model_path = 'final_teath_classifier.h5'
@@ -12,7 +11,6 @@ model = tf.keras.models.load_model(model_path)
12
  # Define preprocessing function
13
  def preprocess_image(image):
14
  # Resize the image to match input size
15
- image = Image.fromarray(image)
16
  image = image.resize((256, 256))
17
  # Convert image to array and preprocess input
18
  img_array = np.array(image) / 255.0
@@ -22,6 +20,14 @@ def preprocess_image(image):
22
 
23
  # Define prediction function
24
  def predict_image(image):
 
 
 
 
 
 
 
 
25
  img_array = preprocess_image(image)
26
  outputs = model(img_array)
27
  predictions = tf.nn.softmax(outputs.logits, axis=-1)
@@ -32,16 +38,16 @@ def predict_image(image):
32
  predict_label = "Carries"
33
  confidence = float(np.max(predictions))
34
  prediction_dict = {"prediction": predict_label, "confidence": confidence}
35
- return prediction_dict
36
 
37
  # Create the interface
38
- input_interface = gr.Image()
39
- output_interface = gr.JSON()
40
 
41
  iface = gr.Interface(
42
  fn=predict_image,
43
  inputs=input_interface,
44
- outputs=output_interface)
45
 
46
  # Launch the interface
47
  iface.launch(share=True)
 
1
+ from PIL import Image
2
+ 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'
 
11
  # Define preprocessing function
12
  def preprocess_image(image):
13
  # Resize the image to match input size
 
14
  image = image.resize((256, 256))
15
  # Convert image to array and preprocess input
16
  img_array = np.array(image) / 255.0
 
20
 
21
  # Define prediction function
22
  def predict_image(image):
23
+ # Save the image to a file-like object
24
+ image_bytes = io.BytesIO()
25
+ image.save(image_bytes, format="jpg")
26
+ image_bytes.seek(0) # Reset file pointer to start
27
+
28
+ # Load the image from the file-like object
29
+ image = tf.keras.preprocessing.image.load_img(image_bytes, target_size=(256, 256))
30
+
31
  img_array = preprocess_image(image)
32
  outputs = model(img_array)
33
  predictions = tf.nn.softmax(outputs.logits, axis=-1)
 
38
  predict_label = "Carries"
39
  confidence = float(np.max(predictions))
40
  prediction_dict = {"prediction": predict_label, "confidence": confidence}
41
+ return json.dumps(prediction_dict, indent=2)
42
 
43
  # Create the interface
44
+ input_interface = gr.inputs.Image(shape=(256, 256), image_mode='RGB')
45
+ output_interface = "json"
46
 
47
  iface = gr.Interface(
48
  fn=predict_image,
49
  inputs=input_interface,
50
+ outputs=gr.Textbox("output"))
51
 
52
  # Launch the interface
53
  iface.launch(share=True)