Update app.py
Browse files
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
|
8 |
|
9 |
-
# Load
|
10 |
-
model =
|
11 |
|
|
|
|
|
12 |
|
13 |
def classify_image(img):
|
14 |
-
# Preprocess the image
|
15 |
-
img = img.resize((299, 299)) #
|
16 |
-
|
17 |
-
|
18 |
-
img_array /= 255.0 # Normalize the image
|
19 |
|
20 |
# Make prediction
|
21 |
-
predictions = model.predict(
|
22 |
-
predicted_class = np.argmax(predictions, axis
|
23 |
-
|
24 |
-
return
|
25 |
|
26 |
-
# Gradio interface
|
27 |
-
demo = gr.Interface(
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|