Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,16 +11,18 @@ import numpy as np
|
|
| 11 |
|
| 12 |
model = tf.keras.models.load_model('mnist.keras') # a modell betoltese
|
| 13 |
|
| 14 |
-
def preprocess_image(img): #
|
| 15 |
-
if len(img.shape) == 3 and img.shape[-1] == 3:
|
| 16 |
-
img = tf.image.rgb_to_grayscale(img).numpy().squeeze()
|
|
|
|
| 17 |
|
| 18 |
-
img = tf.image.resize(img[None, ..., None], (28, 28)).numpy().squeeze()
|
|
|
|
| 19 |
|
| 20 |
-
if img.mean() > 127:
|
| 21 |
img = 255 - img
|
| 22 |
|
| 23 |
-
img = img.astype('float32') / 255.0 #
|
| 24 |
img = img.reshape(784) # Flatten the image
|
| 25 |
|
| 26 |
return img
|
|
@@ -29,7 +31,7 @@ def predict_digit(img): # Predict the digit in the image
|
|
| 29 |
processed_img = preprocess_image(img)
|
| 30 |
processed_img = np.expand_dims(processed_img, axis=0) # Add batch dimension
|
| 31 |
prediction = model.predict(processed_img)[0]
|
| 32 |
-
return {str(i): float(prediction[i]) for i in range(10)}
|
| 33 |
|
| 34 |
## Fonti a mukodteto funkcio kodok
|
| 35 |
## Alabb a Gradio interface resz
|
|
|
|
| 11 |
|
| 12 |
model = tf.keras.models.load_model('mnist.keras') # a modell betoltese
|
| 13 |
|
| 14 |
+
def preprocess_image(img): # Potenci谩lis RGB 2 gray konverzi贸
|
| 15 |
+
if len(img.shape) == 3 and img.shape[-1] == 3: # ha szines
|
| 16 |
+
img = tf.image.rgb_to_grayscale(img).numpy().squeeze()
|
| 17 |
+
# hatekony gray conv. > np array lesz bel艖le
|
| 18 |
|
| 19 |
+
img = tf.image.resize(img[None, ..., None], (28, 28)).numpy().squeeze() # resize
|
| 20 |
+
# dimenzi贸t is v谩ltogatunk, a tf.image.resizenak kell ez
|
| 21 |
|
| 22 |
+
if img.mean() > 127: # szin invert
|
| 23 |
img = 255 - img
|
| 24 |
|
| 25 |
+
img = img.astype('float32') / 255.0 # Normaliz谩l
|
| 26 |
img = img.reshape(784) # Flatten the image
|
| 27 |
|
| 28 |
return img
|
|
|
|
| 31 |
processed_img = preprocess_image(img)
|
| 32 |
processed_img = np.expand_dims(processed_img, axis=0) # Add batch dimension
|
| 33 |
prediction = model.predict(processed_img)[0]
|
| 34 |
+
return {str(i): float(prediction[i]) for i in range(10)} # Gradio comaptible kiiras
|
| 35 |
|
| 36 |
## Fonti a mukodteto funkcio kodok
|
| 37 |
## Alabb a Gradio interface resz
|