gradio/demo/digit_classifier.py

35 lines
834 B
Python
Raw Normal View History

2020-08-22 05:20:05 +08:00
# Demo: (Image) -> (Label)
2020-06-10 08:59:21 +08:00
import tensorflow as tf
import gradio
import gradio as gr
2020-07-09 06:09:52 +08:00
from urllib.request import urlretrieve
2020-11-12 01:57:39 +08:00
import os
2020-06-10 08:59:21 +08:00
2020-07-09 09:01:43 +08:00
urlretrieve("https://gr-models.s3-us-west-2.amazonaws.com/mnist-model.h5", "mnist-model.h5")
2020-07-09 06:09:52 +08:00
model = tf.keras.models.load_model("mnist-model.h5")
2020-06-10 08:59:21 +08:00
2020-08-28 23:56:03 +08:00
2020-06-10 08:59:21 +08:00
def recognize_digit(image):
2020-08-20 05:27:22 +08:00
image = image.reshape(1, -1)
2020-07-09 06:09:52 +08:00
prediction = model.predict(image).tolist()[0]
2020-06-11 02:34:53 +08:00
return {str(i): prediction[i] for i in range(10)}
2020-06-10 08:59:21 +08:00
2020-11-12 20:10:51 +08:00
im = gradio.inputs.Image(shape=(28, 28), image_mode='L', invert_colors=False)
2020-07-09 05:53:24 +08:00
2020-11-11 22:15:53 +08:00
iface = gr.Interface(
2020-06-11 02:34:53 +08:00
recognize_digit,
2020-11-12 01:57:39 +08:00
im,
2020-06-11 02:34:53 +08:00
gradio.outputs.Label(num_top_classes=3),
2020-11-12 01:57:39 +08:00
examples=[['digits/' + x] for x in os.listdir('digits/')],
# live=True,
interpretation="default",
2020-07-09 06:09:52 +08:00
capture_session=True,
2020-08-28 23:56:03 +08:00
)
2020-11-11 22:15:53 +08:00
iface.test_launch()
if __name__ == "__main__":
iface.launch()