|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import streamlit as st |
|
|
import cv2 |
|
|
import numpy as np |
|
|
from streamlit_drawable_canvas import st_canvas |
|
|
from keras.models import load_model |
|
|
|
|
|
|
|
|
st.sidebar.title("Canvas Settings") |
|
|
drawing_mode = st.sidebar.selectbox("Drawing tool:", ("freedraw", "line", "rect", "circle", "transform")) |
|
|
stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 10) |
|
|
stroke_color = st.sidebar.color_picker("Stroke color hex: ", "#000000") |
|
|
bg_color = st.sidebar.color_picker("Background color hex: ", "#FFFFFF") |
|
|
bg_image = st.sidebar.file_uploader("Background image:", type=["png", "jpg"]) |
|
|
realtime_update = st.sidebar.checkbox("Update in realtime", True) |
|
|
|
|
|
|
|
|
mode = st.sidebar.radio("Select Prediction Mode", ["Single Digit", "Multi Digit"]) |
|
|
|
|
|
|
|
|
@st.cache_resource |
|
|
def load_single_digit_model(): |
|
|
return load_model("mnist_model.keras") |
|
|
|
|
|
@st.cache_resource |
|
|
def load_multi_digit_model(): |
|
|
return load_model("best_model.keras") |
|
|
|
|
|
model_single = load_single_digit_model() |
|
|
model_multi = load_multi_digit_model() |
|
|
|
|
|
|
|
|
st.title("π§ Digit Recognition App") |
|
|
st.subheader(f"βοΈ Mode: {mode}") |
|
|
|
|
|
|
|
|
canvas_result = st_canvas( |
|
|
fill_color="rgba(255, 165, 0, 0.3)", |
|
|
stroke_width=stroke_width, |
|
|
stroke_color=stroke_color, |
|
|
background_color=bg_color, |
|
|
update_streamlit=realtime_update, |
|
|
height=280, |
|
|
width=280, |
|
|
drawing_mode=drawing_mode, |
|
|
key="canvas", |
|
|
) |
|
|
|
|
|
|
|
|
if canvas_result.image_data is not None: |
|
|
st.markdown("---") |
|
|
st.subheader("π§ͺ Prediction Results") |
|
|
|
|
|
|
|
|
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY) |
|
|
img = 255 - img |
|
|
_, thresh = cv2.threshold(img, 30, 255, cv2.THRESH_BINARY) |
|
|
|
|
|
if mode == "Single Digit": |
|
|
resized = cv2.resize(thresh, (28, 28)) |
|
|
norm = resized.astype("float32") / 255.0 |
|
|
input_img = norm.reshape(1, 28, 28, 1) |
|
|
|
|
|
prediction = model_single.predict(input_img) |
|
|
digit = np.argmax(prediction) |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
with col1: |
|
|
st.image(resized, width=200, caption="28x28 Preprocessed") |
|
|
with col2: |
|
|
st.success(f"π§ Predicted Digit: **{digit}**") |
|
|
|
|
|
elif mode == "Multi Digit": |
|
|
resized = cv2.resize(thresh, (80, 28)) |
|
|
norm = resized.astype("float32") / 255.0 |
|
|
input_seq = norm.reshape(1, 28, 80, 1) |
|
|
|
|
|
preds = model_multi.predict(input_seq) |
|
|
|
|
|
|
|
|
predicted_digits = [np.argmax(p[0]) for p in preds] |
|
|
predicted_str = ''.join(str(d) for d in predicted_digits) |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
with col1: |
|
|
st.image(resized, width=300, caption="80x28 Multi-digit Input") |
|
|
with col2: |
|
|
st.success(f"π§ Predicted Sequence: **{predicted_str}**") |
|
|
|