Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,11 +9,10 @@ import os
|
|
| 9 |
import tempfile
|
| 10 |
from pathlib import Path
|
| 11 |
|
| 12 |
-
# Import EXACT SAME functions from main.py
|
| 13 |
from preprocess import preprocess_image
|
| 14 |
from train import create_model
|
| 15 |
|
| 16 |
-
#
|
| 17 |
try:
|
| 18 |
with open('./class_indices.json', 'r') as f:
|
| 19 |
class_indices = json.load(f)
|
|
@@ -23,7 +22,7 @@ try:
|
|
| 23 |
PIECES[idx] = name
|
| 24 |
print(f"Ordre des classes chargé: {PIECES}")
|
| 25 |
except FileNotFoundError:
|
| 26 |
-
#
|
| 27 |
PIECES = ['Bishop_Black', 'Bishop_White', 'Empty', 'King_Black', 'King_White', 'Knight_Black',
|
| 28 |
'Knight_White', 'Pawn_Black', 'Pawn_White', 'Queen_Black', 'Queen_White', 'Rook_Black', 'Rook_White']
|
| 29 |
print(f"Fichier class_indices.json non trouvé, utilisation ordre par défaut")
|
|
@@ -44,7 +43,7 @@ LABELS = {
|
|
| 44 |
'Pawn_Black': 'p',
|
| 45 |
}
|
| 46 |
|
| 47 |
-
#
|
| 48 |
print("Loading model...")
|
| 49 |
model = create_model()
|
| 50 |
model.load_weights('./model_weights.weights.h5')
|
|
@@ -52,9 +51,8 @@ print("Model loaded!")
|
|
| 52 |
|
| 53 |
|
| 54 |
def classify_image(img):
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
# IMPORTANT: Normaliser l'image comme dans l'entraînement (rescale=1/255)
|
| 58 |
if img.max() > 1.0:
|
| 59 |
img = img.astype(np.float32) / 255.0
|
| 60 |
else:
|
|
@@ -82,7 +80,7 @@ def analyze_board(img):
|
|
| 82 |
row.append(LABELS[piece])
|
| 83 |
arr.append(row)
|
| 84 |
|
| 85 |
-
# King-Queen
|
| 86 |
blackKing = False
|
| 87 |
whiteKing = False
|
| 88 |
whitePos = (-1, -1)
|
|
@@ -126,12 +124,12 @@ def board_to_fen(board):
|
|
| 126 |
|
| 127 |
|
| 128 |
def analyze_chess_image(image_input):
|
| 129 |
-
|
| 130 |
if image_input is None:
|
| 131 |
return "❌ No image provided", None
|
| 132 |
|
| 133 |
try:
|
| 134 |
-
#
|
| 135 |
with tempfile.NamedTemporaryFile(suffix='.jpg', delete=False) as tmp:
|
| 136 |
if isinstance(image_input, np.ndarray):
|
| 137 |
cv2.imwrite(tmp.name, cv2.cvtColor(image_input, cv2.COLOR_RGB2BGR))
|
|
@@ -139,18 +137,18 @@ def analyze_chess_image(image_input):
|
|
| 139 |
image_input.save(tmp.name)
|
| 140 |
temp_path = tmp.name
|
| 141 |
|
| 142 |
-
#
|
| 143 |
img = preprocess_image(temp_path, save=False)
|
| 144 |
|
| 145 |
# EXACT SAME as main.py
|
| 146 |
arr = analyze_board(img)
|
| 147 |
fen = board_to_fen(arr)
|
| 148 |
|
| 149 |
-
#
|
| 150 |
board = chess.Board(fen)
|
| 151 |
board_svg = chess.svg.board(board=board, size=400)
|
| 152 |
|
| 153 |
-
#
|
| 154 |
os.unlink(temp_path)
|
| 155 |
|
| 156 |
return f"{fen}", board_svg
|
|
@@ -162,13 +160,11 @@ def analyze_chess_image(image_input):
|
|
| 162 |
|
| 163 |
|
| 164 |
# Build Gradio interface
|
| 165 |
-
with gr.Blocks(title="Chess Board
|
| 166 |
gr.Markdown("""
|
| 167 |
-
# ♟️
|
| 168 |
|
| 169 |
Upload a chess board image to automatically detect all pieces and get the FEN notation.
|
| 170 |
-
|
| 171 |
-
**Uses EXACT SAME preprocessing (LAPS) and model as main.py**
|
| 172 |
""")
|
| 173 |
|
| 174 |
with gr.Row():
|
|
|
|
| 9 |
import tempfile
|
| 10 |
from pathlib import Path
|
| 11 |
|
|
|
|
| 12 |
from preprocess import preprocess_image
|
| 13 |
from train import create_model
|
| 14 |
|
| 15 |
+
# On charge l'ordre des classes depuis le fichier généré par train.
|
| 16 |
try:
|
| 17 |
with open('./class_indices.json', 'r') as f:
|
| 18 |
class_indices = json.load(f)
|
|
|
|
| 22 |
PIECES[idx] = name
|
| 23 |
print(f"Ordre des classes chargé: {PIECES}")
|
| 24 |
except FileNotFoundError:
|
| 25 |
+
# Si jamais le fichier n'est pas load correctement ou erreur
|
| 26 |
PIECES = ['Bishop_Black', 'Bishop_White', 'Empty', 'King_Black', 'King_White', 'Knight_Black',
|
| 27 |
'Knight_White', 'Pawn_Black', 'Pawn_White', 'Queen_Black', 'Queen_White', 'Rook_Black', 'Rook_White']
|
| 28 |
print(f"Fichier class_indices.json non trouvé, utilisation ordre par défaut")
|
|
|
|
| 43 |
'Pawn_Black': 'p',
|
| 44 |
}
|
| 45 |
|
| 46 |
+
# On charge notre modele
|
| 47 |
print("Loading model...")
|
| 48 |
model = create_model()
|
| 49 |
model.load_weights('./model_weights.weights.h5')
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
def classify_image(img):
|
| 54 |
+
# On donne une image d'une pièce unique, on la classifie en une seule classe definie (Son nom est PIECE)
|
| 55 |
+
# Ici on normalise notre image comme dans notre entrainement (ici on fait un rescale=1/255)
|
|
|
|
| 56 |
if img.max() > 1.0:
|
| 57 |
img = img.astype(np.float32) / 255.0
|
| 58 |
else:
|
|
|
|
| 80 |
row.append(LABELS[piece])
|
| 81 |
arr.append(row)
|
| 82 |
|
| 83 |
+
# Ajustement King-Queen detection
|
| 84 |
blackKing = False
|
| 85 |
whiteKing = False
|
| 86 |
whitePos = (-1, -1)
|
|
|
|
| 124 |
|
| 125 |
|
| 126 |
def analyze_chess_image(image_input):
|
| 127 |
+
# Logique gradio pour notre main.
|
| 128 |
if image_input is None:
|
| 129 |
return "❌ No image provided", None
|
| 130 |
|
| 131 |
try:
|
| 132 |
+
# On sauvegarde temporairement
|
| 133 |
with tempfile.NamedTemporaryFile(suffix='.jpg', delete=False) as tmp:
|
| 134 |
if isinstance(image_input, np.ndarray):
|
| 135 |
cv2.imwrite(tmp.name, cv2.cvtColor(image_input, cv2.COLOR_RGB2BGR))
|
|
|
|
| 137 |
image_input.save(tmp.name)
|
| 138 |
temp_path = tmp.name
|
| 139 |
|
| 140 |
+
# preprocess_image() utilise le modele LAPS
|
| 141 |
img = preprocess_image(temp_path, save=False)
|
| 142 |
|
| 143 |
# EXACT SAME as main.py
|
| 144 |
arr = analyze_board(img)
|
| 145 |
fen = board_to_fen(arr)
|
| 146 |
|
| 147 |
+
# On génère l'echiquier
|
| 148 |
board = chess.Board(fen)
|
| 149 |
board_svg = chess.svg.board(board=board, size=400)
|
| 150 |
|
| 151 |
+
# on clean le fichier temporairement sauvegarder
|
| 152 |
os.unlink(temp_path)
|
| 153 |
|
| 154 |
return f"{fen}", board_svg
|
|
|
|
| 160 |
|
| 161 |
|
| 162 |
# Build Gradio interface
|
| 163 |
+
with gr.Blocks(title="Chess Board picture -> FEN notation", theme=gr.themes.Soft()) as demo:
|
| 164 |
gr.Markdown("""
|
| 165 |
+
# ♟️ YOCO: You Only Look Once
|
| 166 |
|
| 167 |
Upload a chess board image to automatically detect all pieces and get the FEN notation.
|
|
|
|
|
|
|
| 168 |
""")
|
| 169 |
|
| 170 |
with gr.Row():
|