Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
import cv2 # opencv-python
|
5 |
+
|
6 |
+
|
7 |
+
model = tf.keras.models.load_model("/content/TP_MNIST_CNN_model.h5")
|
8 |
+
|
9 |
+
|
10 |
+
def preprocess_image(img):
|
11 |
+
img = tf.image.rgb_to_grayscale(img) # Convert to grayscale
|
12 |
+
img = tf.image.resize(img, (28, 28)) # Resize to model input size
|
13 |
+
img = img / 255.0 # Normalize pixel values
|
14 |
+
img = np.expand_dims(img, axis=0) # Add batch dimension
|
15 |
+
return img
|
16 |
+
|
17 |
+
# Function to make predictions
|
18 |
+
def predict_image(img):
|
19 |
+
processed_img = preprocess_image(img)
|
20 |
+
prediction = model.predict(processed_img)
|
21 |
+
return {str(i): float(prediction[0][i]) for i in range(10)}
|
22 |
+
|
23 |
+
gr.Interface(
|
24 |
+
fn=predict_image,
|
25 |
+
inputs=gr.Image(),
|
26 |
+
outputs=gr.Label(num_top_classes=3),
|
27 |
+
examples=["minst6.png", "mnist1.png", "mnist2_5.png", "mnist69.png"], # Example images for interface
|
28 |
+
title='Handwritten Digits Classification stage DREAMS Team'
|
29 |
+
).launch(share=True)
|