CoolFace
Apppublic

tinkvu/MathSymbolClassification

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
app.py78 linesDownload Raw Back to root
1#!pip install streamlit>=1.14.0 tensorflow>=2.13.0 keras>=2.13.0 numpy>=1.23.5 pillow>=8.4.0 streamlit-drawable-canvas2 3 4 5import streamlit as st6from tensorflow import keras7from tensorflow.keras.preprocessing import image8import numpy as np9from PIL import Image10from streamlit_drawable_canvas import st_canvas11 12# Load the trained model13model = keras.models.load_model("model.h5")14 15 16# Get class names from the output layer17class_names = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'dot', 'minus', 'plus', 'slash', 'w', 'x', 'y', 'z']18 19def preprocess_image(img_array):20    # Ensure the image has 3 channels (RGB)21    img_array = img_array[:, :, :3]22    23    # Resize the image to target size24    img = Image.fromarray(img_array)25    img = img.resize((64, 64))26    img_array = np.array(img)27    img_array = img_array / 255.0  # Normalize the image28    img_array = np.expand_dims(img_array, axis=0)29    return img_array30 31def predict(img_array):32    img_array = preprocess_image(img_array)33    prediction = model.predict(img_array)34    predicted_class = np.argmax(prediction)35    confidence = np.max(prediction) * 10036    return class_names[predicted_class], confidence37 38def main():39    st.title("Math Symbol Identification using CNN")40    st.write("The model is trained on 27,000 images of Math Symbols.")41    #image_url = "/symbols.gif"  # Replace with the URL of your image42    #st.image(image_url,use_column_width=True)43    st.write("Try drawing any symbol on the canvas below:")44    45    46    47    # Create a drawing canvas48    canvas_result = st_canvas(49        fill_color="rgba(255, 165, 0, 0.3)",  # Initial drawing color50        stroke_width=5,51        stroke_color="rgb(0, 0, 0)",52        background_color="#fff",53        height=64,54        width=64,55        drawing_mode="freedraw",56        key="canvas",57    )58 59    if st.button("Predict"):60        if canvas_result.image_data is not None:61            # Make prediction62            class_name, confidence = predict(canvas_result.image_data)63            st.write(f"Prediction: {class_name}")64            st.write(f"Confidence: {confidence:.2f}%")65            66 67            68             # Add a button for reporting69            if st.button("Report Irrelevant Prediction"):70                st.write("Thank you for reporting! Our team will review the prediction.")71 72        else:73            st.warning("Please draw an image before predicting.")74 75    76# Run the Streamlit app77if __name__ == "__main__":78    main()