File size: 1,493 Bytes
e4d5725
 
 
 
ecbfdf7
e4d5725
 
ecbfdf7
b132a13
e4d5725
177054a
ecbfdf7
 
 
 
 
e4d5725
ecbfdf7
 
 
e4d5725
ecbfdf7
177054a
ecbfdf7
 
 
 
e4d5725
ecbfdf7
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import gradio as gr
from PIL import Image
import numpy as np
from tensorflow.keras.preprocessing import image as keras_image
from tensorflow.keras.applications.xception import preprocess_input
from tensorflow.keras.models import load_model

# Load your trained model
model = load_model('/home/user/app/xception_model.h5')  # Ensure this path is correct

def predict_character(img):
    img = Image.fromarray(img.astype('uint8'), 'RGB')  # Ensure the image is in RGB format
    img = img.resize((299, 299))  # Resize the image to the required size for Xception
    img_array = keras_image.img_to_array(img)  # Convert the image to an array
    img_array = np.expand_dims(img_array, axis=0)  # Expand dimensions to match the model input
    img_array = preprocess_input(img_array)  # Preprocess the input for Xception
    
    prediction = model.predict(img_array)  # Make a prediction with the model
    classes = ['bishop', 'knight', 'rook']  # Specific character names
    return {classes[i]: float(prediction[0][i]) for i in range(3)}  # Return the prediction

# Define the Gradio interface
interface = gr.Interface(fn=predict_character, 
                         inputs="image",  # Simplified input type
                         outputs="label",  # Simplified output type
                         title="Chess Piece Classifier",
                         description="Upload an image of a chess piece to classify it as a bishop, knight, or rook.")

# Launch the interface
interface.launch()