File size: 2,045 Bytes
ef2e55d
 
2dfd60c
 
 
 
ef2e55d
2dfd60c
d36ca6b
 
2dfd60c
d36ca6b
 
 
2dfd60c
d36ca6b
 
 
2dfd60c
d36ca6b
 
 
 
 
2dfd60c
d36ca6b
 
2dfd60c
 
 
d36ca6b
2dfd60c
 
 
 
 
 
 
ef2e55d
 
 
2dfd60c
d36ca6b
 
ef2e55d
2dfd60c
 
ef2e55d
 
 
 
 
2dfd60c
ef2e55d
2dfd60c
 
 
d36ca6b
2dfd60c
 
d36ca6b
2dfd60c
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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

# Define the list of Arabic characters
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']

# Load the model once at the beginning
model_path = "saved_model.h5"  # Update with your model path
model = load_model(model_path)

# Define the prediction function
def predict_image(image):
    # Convert to grayscale
    img = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
    
    # Invert the image colors (black background with white letter)
    img = cv2.bitwise_not(img)
    
    # Resize the image to the size expected by the model
    img = cv2.resize(img, (32, 32))
    
    # Reshape and normalize the image
    img = img.reshape(1, 32, 32, 1)
    img = img.astype('float32') / 255.0

    # Predict the character
    pred = model.predict(img)
    predicted_label = arabic_chars[np.argmax(pred)]

    return predicted_label

# Streamlit app
st.title("Arabic Character Recognition App")

canvas_result = st_canvas(
    fill_color="rgba(255, 165, 0, 0.3)",  # Filled color
    stroke_width=12,  # 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 canvas_result.image_data is not None:
    # Display the drawn image
    st.image(canvas_result.image_data)

    # Convert the canvas image data to a PIL image
    image = Image.fromarray(canvas_result.image_data.astype('uint8'), 'RGBA').convert('RGB')
    #st.image(image)

    # Predict the character
    predicted_char = predict_image(image)

    # Display the predicted character
    st.subheader(f"Predicted Character: {predicted_char}")