Update app.py
Browse files
app.py
CHANGED
@@ -2,29 +2,29 @@ import gradio as gr
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
from tensorflow.keras.preprocessing import image as keras_image
|
5 |
-
from tensorflow.keras.applications.
|
6 |
from tensorflow.keras.models import load_model
|
7 |
|
8 |
-
#
|
9 |
-
model = load_model('/
|
10 |
|
11 |
def predict_character(img):
|
12 |
-
img = Image.fromarray(img.astype('uint8'), 'RGB') #
|
13 |
-
img = img.resize((299, 299)) #
|
14 |
-
img_array = keras_image.img_to_array(img) #
|
15 |
-
img_array = np.expand_dims(img_array, axis=0) #
|
16 |
-
img_array = preprocess_input(img_array) #
|
17 |
|
18 |
-
prediction = model.predict(img_array) #
|
19 |
-
classes = ['bishop', 'knight', 'rook'] #
|
20 |
-
return {classes[i]: float(prediction[0][i]) for i in range(3)} #
|
21 |
|
22 |
-
#
|
23 |
interface = gr.Interface(fn=predict_character,
|
24 |
-
inputs="image", #
|
25 |
-
outputs="label", #
|
26 |
-
title="
|
27 |
-
description="
|
28 |
|
29 |
-
#
|
30 |
-
interface.launch()
|
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
from tensorflow.keras.preprocessing import image as keras_image
|
5 |
+
from tensorflow.keras.applications.xception import preprocess_input
|
6 |
from tensorflow.keras.models import load_model
|
7 |
|
8 |
+
# Load your trained model
|
9 |
+
model = load_model('/path/to/final_model_xception.h5') # Ensure this path is correct
|
10 |
|
11 |
def predict_character(img):
|
12 |
+
img = Image.fromarray(img.astype('uint8'), 'RGB') # Ensure the image is in RGB format
|
13 |
+
img = img.resize((299, 299)) # Resize the image to the required size for Xception
|
14 |
+
img_array = keras_image.img_to_array(img) # Convert the image to an array
|
15 |
+
img_array = np.expand_dims(img_array, axis=0) # Expand dimensions to match the model input
|
16 |
+
img_array = preprocess_input(img_array) # Preprocess the input for Xception
|
17 |
|
18 |
+
prediction = model.predict(img_array) # Make a prediction with the model
|
19 |
+
classes = ['bishop', 'knight', 'rook'] # Specific character names
|
20 |
+
return {classes[i]: float(prediction[0][i]) for i in range(3)} # Return the prediction
|
21 |
|
22 |
+
# Define the Gradio interface
|
23 |
interface = gr.Interface(fn=predict_character,
|
24 |
+
inputs="image", # Simplified input type
|
25 |
+
outputs="label", # Simplified output type
|
26 |
+
title="Chess Piece Classifier",
|
27 |
+
description="Upload an image of a chess piece to classify it as a bishop, knight, or rook.")
|
28 |
|
29 |
+
# Launch the interface
|
30 |
+
interface.launch()
|