Update app.py
Browse files
app.py
CHANGED
@@ -6,18 +6,29 @@ import numpy as np
|
|
6 |
from PIL import Image
|
7 |
|
8 |
# Define the list of Arabic characters
|
9 |
-
arabic_chars = ['alef','beh','teh','theh','jeem','hah','khah','dal','thal','reh','zain','seen','sheen',
|
10 |
-
'sad','dad','tah','zah','ain','ghain','feh','qaf','kaf','lam','meem','noon','heh','waw','yeh']
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
|
|
|
|
|
|
|
16 |
img = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
|
|
|
|
|
|
|
|
|
|
|
17 |
img = cv2.resize(img, (32, 32))
|
|
|
|
|
18 |
img = img.reshape(1, 32, 32, 1)
|
19 |
img = img.astype('float32') / 255.0
|
20 |
|
|
|
21 |
pred = model.predict(img)
|
22 |
predicted_label = arabic_chars[np.argmax(pred)]
|
23 |
|
@@ -29,8 +40,8 @@ st.title("Arabic Character Recognition App")
|
|
29 |
canvas_result = st_canvas(
|
30 |
fill_color="rgba(255, 165, 0, 0.3)", # Filled color
|
31 |
stroke_width=12, # Stroke width
|
32 |
-
stroke_color="#
|
33 |
-
background_color="#
|
34 |
update_streamlit=True,
|
35 |
height=400,
|
36 |
width=400,
|
@@ -44,16 +55,10 @@ if canvas_result.image_data is not None:
|
|
44 |
|
45 |
# Convert the canvas image data to a PIL image
|
46 |
image = Image.fromarray(canvas_result.image_data.astype('uint8'), 'RGBA').convert('RGB')
|
47 |
-
st.image(image)
|
48 |
-
|
49 |
-
# Save the image to a temporary file
|
50 |
-
temp_image_path = "temp_drawing.png"
|
51 |
-
image.save(temp_image_path)
|
52 |
|
53 |
# Predict the character
|
54 |
-
|
55 |
-
predicted_char = predict_image(image, model_path)
|
56 |
|
57 |
# Display the predicted character
|
58 |
st.subheader(f"Predicted Character: {predicted_char}")
|
59 |
-
|
|
|
6 |
from PIL import Image
|
7 |
|
8 |
# Define the list of Arabic characters
|
9 |
+
arabic_chars = ['alef', 'beh', 'teh', 'theh', 'jeem', 'hah', 'khah', 'dal', 'thal', 'reh', 'zain', 'seen', 'sheen',
|
10 |
+
'sad', 'dad', 'tah', 'zah', 'ain', 'ghain', 'feh', 'qaf', 'kaf', 'lam', 'meem', 'noon', 'heh', 'waw', 'yeh']
|
11 |
|
12 |
+
# Load the model once at the beginning
|
13 |
+
model_path = "saved_model.h5" # Update with your model path
|
14 |
+
model = load_model(model_path)
|
15 |
|
16 |
+
# Define the prediction function
|
17 |
+
def predict_image(image):
|
18 |
+
# Convert to grayscale
|
19 |
img = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
|
20 |
+
|
21 |
+
# Invert the image colors (black background with white letter)
|
22 |
+
img = cv2.bitwise_not(img)
|
23 |
+
|
24 |
+
# Resize the image to the size expected by the model
|
25 |
img = cv2.resize(img, (32, 32))
|
26 |
+
|
27 |
+
# Reshape and normalize the image
|
28 |
img = img.reshape(1, 32, 32, 1)
|
29 |
img = img.astype('float32') / 255.0
|
30 |
|
31 |
+
# Predict the character
|
32 |
pred = model.predict(img)
|
33 |
predicted_label = arabic_chars[np.argmax(pred)]
|
34 |
|
|
|
40 |
canvas_result = st_canvas(
|
41 |
fill_color="rgba(255, 165, 0, 0.3)", # Filled color
|
42 |
stroke_width=12, # Stroke width
|
43 |
+
stroke_color="#FFFFFF", # Stroke color (white)
|
44 |
+
background_color="#000000", # Canvas background color (black)
|
45 |
update_streamlit=True,
|
46 |
height=400,
|
47 |
width=400,
|
|
|
55 |
|
56 |
# Convert the canvas image data to a PIL image
|
57 |
image = Image.fromarray(canvas_result.image_data.astype('uint8'), 'RGBA').convert('RGB')
|
58 |
+
#st.image(image)
|
|
|
|
|
|
|
|
|
59 |
|
60 |
# Predict the character
|
61 |
+
predicted_char = predict_image(image)
|
|
|
62 |
|
63 |
# Display the predicted character
|
64 |
st.subheader(f"Predicted Character: {predicted_char}")
|
|