Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
model = tf.keras.models.load_model('mnist.keras') # .keras modellunk betoltese
|
| 2 |
+
|
| 3 |
+
def preprocess_image(img): # Szokasos preprocess eljaras
|
| 4 |
+
# Grayscale konverzio (ha nem ugy lenne)
|
| 5 |
+
if len(img.shape) == 3 and img.shape[-1] == 3:
|
| 6 |
+
img = tf.image.rgb_to_grayscale(img).numpy().squeeze()
|
| 7 |
+
|
| 8 |
+
img = tf.image.resize(img[None, ..., None], (28, 28)).numpy().squeeze() # atmeretez: 28x28
|
| 9 |
+
|
| 10 |
+
# szures: fekete hatter - feher digitek
|
| 11 |
+
if img.mean() > 127:
|
| 12 |
+
img = 255 - img
|
| 13 |
+
|
| 14 |
+
img = img.astype('float32') / 255.0 # Normalize
|
| 15 |
+
img = img.reshape(784) # Lapositjuk az adatot mert az ANN igy keri
|
| 16 |
+
|
| 17 |
+
return img
|
| 18 |
+
|
| 19 |
+
def predict_digit(img): # Predict funkcio - Ez indul a submit gombra
|
| 20 |
+
processed_img = preprocess_image(img) # Preprocess the image
|
| 21 |
+
processed_img = np.expand_dims(processed_img, axis=0) # Add batch dimension
|
| 22 |
+
prediction = model.predict(processed_img)[0] # Make prediction
|
| 23 |
+
|
| 24 |
+
# Return the prediction as a dictionary
|
| 25 |
+
return {str(i): float(prediction[i]) for i in range(10)}
|
| 26 |
+
|
| 27 |
+
# A gradio interface
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=predict_digit, # a fo funkcio
|
| 30 |
+
inputs=gr.Image(type="numpy", label="Tölts fel egy képet ..."),
|
| 31 |
+
outputs=gr.Label(num_top_classes=3, label="A feltöltött számjegy:"),
|
| 32 |
+
title="MNIST Digit Recognition",
|
| 33 |
+
description="Tölts fel egy kézzel írott szám képet (0-9) és a modell megállapítja melyik szám az",
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# iface.launch() # Interface indítása
|
| 37 |
+
iface.launch(share=True) # Interface indítása
|