File size: 1,662 Bytes
168e71d
 
 
 
 
 
6a67fec
168e71d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68bc770
 
30e63eb
 
 
 
 
 
 
 
 
 
168e71d
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import gradio as gr
import tensorflow as tf
from PIL import Image
import numpy as np

# Modellpfad relativ zum aktuellen Arbeitsverzeichnis
model_path = 'chess_piece_classifier_mobilenet.keras'

# Modell laden
model = tf.keras.models.load_model(model_path)

# Klassenlabels (Passe diese entsprechend deinem Modell an)
labels = ['Black bishop', 'Black king', 'Black knight', 'Black pawn', 'Black queen', 'Black rook', 'White bishop', 'White king', 'White knight', 'White pawn', 'White queen', 'White rook']

# Vorhersagefunktion
def predict(image):
    try:
        # Bildvorverarbeitung
        image = image.resize((224, 224))  # Bildgröße auf 224x224 ändern
        image = np.array(image) / 255.0
        image = np.expand_dims(image, axis=0)
        
        # Vorhersage
        predictions = model.predict(image)
        confidences = {labels[i]: float(predictions[0][i]) for i in range(len(labels))}
        
        return confidences
    except Exception as e:
        return str(e)  # Fehlernachricht zurückgeben

# Gradio-Interface erstellen
iface = gr.Interface(
    fn=predict, 
    inputs=gr.Image(type="pil"),  # Bild als PIL-Objekt
    outputs=gr.Label(),
    description="Chess Piece Classifier",
    examples=[
        ['data/example1.jpg'],
        ['data/example2.jpg'],
        ['data/example3.jpg'],
        ['data/example4.jpg'],
        ['data/example5.jpg'],
        ['data/example6.jpg'],
        ['data/example7.jpg'],
        ['data/example8.jpg'],
        ['data/example9.jpg'],
        ['data/example10.jpg'],
        ['data/example11.jpg'],
        ['data/example12.jpg']
    ]
)

if __name__ == "__main__":
    iface.launch()