|
|
model = tf.keras.models.load_model('mnist.keras') |
|
|
|
|
|
def preprocess_image(img): |
|
|
|
|
|
if len(img.shape) == 3 and img.shape[-1] == 3: |
|
|
img = tf.image.rgb_to_grayscale(img).numpy().squeeze() |
|
|
|
|
|
img = tf.image.resize(img[None, ..., None], (28, 28)).numpy().squeeze() |
|
|
|
|
|
|
|
|
if img.mean() > 127: |
|
|
img = 255 - img |
|
|
|
|
|
img = img.astype('float32') / 255.0 |
|
|
img = img.reshape(784) |
|
|
|
|
|
return img |
|
|
|
|
|
def predict_digit(img): |
|
|
processed_img = preprocess_image(img) |
|
|
processed_img = np.expand_dims(processed_img, axis=0) |
|
|
prediction = model.predict(processed_img)[0] |
|
|
|
|
|
|
|
|
return {str(i): float(prediction[i]) for i in range(10)} |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=predict_digit, |
|
|
inputs=gr.Image(type="numpy", label="Tölts fel egy képet ..."), |
|
|
outputs=gr.Label(num_top_classes=3, label="A feltöltött számjegy:"), |
|
|
title="MNIST Digit Recognition", |
|
|
description="Tölts fel egy kézzel írott szám képet (0-9) és a modell megállapítja melyik szám az", |
|
|
) |
|
|
|
|
|
|
|
|
iface.launch(share=True) |