annanau commited on
Commit
43fbc49
·
verified ·
1 Parent(s): 9b262c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -17
app.py CHANGED
@@ -1,28 +1,34 @@
1
- import os
2
- os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Disable GPU usage
3
  import gradio as gr
4
  import tensorflow as tf
5
  from tensorflow.keras.preprocessing import image
6
  import numpy as np
7
- from transformers import pipeline
8
 
9
- # Load image classification model from Hugging Face
10
- model = pipeline("image-classification", model="icputrd/gelderman_decomposition_classification")
11
 
 
 
12
 
13
  def classify_image(img):
14
- # Preprocess the image
15
- img = img.resize((299, 299)) # Resize to the input shape of Xception
16
- img_array = image.img_to_array(img) # Convert image to array
17
- img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
18
- img_array /= 255.0 # Normalize the image
19
 
20
  # Make prediction
21
- predictions = model.predict(img_array)
22
- predicted_class = np.argmax(predictions, axis=-1)[0] # Get the predicted class index
23
-
24
- return str(predicted_class) # Return the class index as a string
25
 
26
- # Gradio interface for image classification
27
- demo = gr.Interface(fn=classify_image, inputs=gr.inputs.Image(type="pil"), outputs="label")
28
- demo.launch()
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  from tensorflow.keras.preprocessing import image
4
  import numpy as np
5
+ from PIL import Image
6
 
7
+ # Load your trained Xception model
8
+ model = tf.keras.models.load_model("your_xception_model.h5")
9
 
10
+ # Define the labels for your classification (example: if you have 3 classes)
11
+ class_labels = ['class1', 'class2', 'class3'] # Replace with your actual class names
12
 
13
  def classify_image(img):
14
+ # Preprocess the image to fit the model input shape
15
+ img = img.resize((299, 299)) # Xception takes 299x299 input size
16
+ img = np.array(img) / 255.0 # Normalize the image
17
+ img = np.expand_dims(img, axis=0)
 
18
 
19
  # Make prediction
20
+ predictions = model.predict(img)
21
+ predicted_class = np.argmax(predictions, axis=1)[0]
22
+ confidence = np.max(predictions)
23
+ return {class_labels[i]: float(predictions[0][i]) for i in range(len(class_labels))}, confidence
24
 
25
+ # Gradio interface
26
+ demo = gr.Interface(
27
+ fn=classify_image,
28
+ inputs=gr.inputs.Image(type="pil"),
29
+ outputs=[gr.outputs.Label(num_top_classes=len(class_labels)), "number"],
30
+ live=True
31
+ )
32
+
33
+ if __name__ == "__main__":
34
+ demo.launch()