|
import streamlit as st |
|
from streamlit_drawable_canvas import st_canvas |
|
import cv2 |
|
from tensorflow.keras.models import load_model |
|
import numpy as np |
|
from PIL import Image |
|
|
|
|
|
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'] |
|
|
|
|
|
model_path = "saved_model.h5" |
|
model = load_model(model_path) |
|
|
|
|
|
def predict_image(image): |
|
|
|
img = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY) |
|
|
|
|
|
img = cv2.bitwise_not(img) |
|
|
|
|
|
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 |
|
|
|
|
|
st.title("Arabic Character Recognition App") |
|
|
|
canvas_result = st_canvas( |
|
fill_color="rgba(255, 165, 0, 0.3)", |
|
stroke_width=12, |
|
stroke_color="#FFFFFF", |
|
background_color="#000000", |
|
update_streamlit=True, |
|
height=400, |
|
width=400, |
|
drawing_mode="freedraw", |
|
key="canvas", |
|
) |
|
|
|
if canvas_result.image_data is not None: |
|
|
|
st.image(canvas_result.image_data) |
|
|
|
|
|
image = Image.fromarray(canvas_result.image_data.astype('uint8'), 'RGBA').convert('RGB') |
|
|
|
|
|
|
|
predicted_char = predict_image(image) |
|
|
|
|
|
st.subheader(f"Predicted Character: {predicted_char}") |
|
|