MO3ALIMI / pages /4_Writing.py
mouadenna's picture
Update pages/4_Writing.py
0cdfe60 verified
raw
history blame
2.3 kB
import streamlit as st
from streamlit_drawable_canvas import st_canvas
import cv2
from tensorflow.keras.models import load_model
import numpy as np
arabic_chars = ['alef','beh','teh','theh','jeem','hah','khah','dal','thal','reh','zain','seen','sheen',
'sad','dad','tah','zah','ain','ghain','feh','qaf','kaf','lam','meem','noon','heh','waw','yeh']
def add_logo():
st.markdown(
"""
<style>
[data-testid="stSidebarNav"] {
/*background-image: url(http://placekitten.com/200/200);*/
background-repeat: no-repeat;
#padding-top: 120px;
background-position: 20px 20px;
}
[data-testid="stSidebarNav"]::before {
content: "MO3ALIMI sidebar";
margin-left: 20px;
margin-top: 20px;
font-size: 29px;
position: relative;
top: 0px;
}
</style>
""",
unsafe_allow_html=True,
)
add_logo()
def predict_image(image_path, model_path):
model = load_model(model_path)
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (32, 32))
img = img.reshape(1, 32, 32, 1)
img = img.astype('float32') / 255.0
pred = model.predict(img)
predicted_label = arabic_chars[np.argmax(pred)]
return predicted_label
canvas_result = st_canvas(
fill_color="rgba(255, 255, 255, 0.3)", # Filled color (white)
stroke_width=30, # Stroke width
stroke_color="#FFFFFF", # Stroke color (white)
background_color="#000000", # Canvas background color (black)
update_streamlit=True,
height=400,
width=400,
drawing_mode="freedraw",
key="canvas",
)
if st.button("Predict"):
if canvas_result.image_data is not None:
image = canvas_result.image_data
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.resize(image, (32, 32))
cv2.imwrite("temp_image.png", image)
model_path = "saved_model.h5" # Replace with the path to your trained model
predicted_label = predict_image("temp_image.png", model_path)
st.write(f"Predicted Character: {predicted_label}")
else:
st.write("Please draw something on the canvas.")