Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,58 @@
|
|
1 |
import streamlit as st
|
2 |
from streamlit_drawable_canvas import st_canvas
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
canvas_result = st_canvas(
|
7 |
fill_color="rgba(255, 165, 0, 0.3)", # Filled color
|
8 |
-
stroke_width=
|
9 |
stroke_color="#000000", # Stroke color
|
10 |
background_color="#ffffff", # Canvas background color
|
11 |
update_streamlit=True,
|
12 |
-
height=
|
13 |
-
width=
|
14 |
drawing_mode="freedraw",
|
15 |
key="canvas",
|
16 |
)
|
17 |
|
18 |
if canvas_result.image_data is not None:
|
|
|
19 |
st.image(canvas_result.image_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from streamlit_drawable_canvas import st_canvas
|
3 |
+
import cv2
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
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 |
+
# Define the prediction function
|
13 |
+
def predict_image(image, model_path):
|
14 |
+
model = load_model(model_path)
|
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 |
+
|
24 |
+
return predicted_label
|
25 |
+
|
26 |
+
# Streamlit app
|
27 |
+
st.title("Arabic Character Recognition App")
|
28 |
|
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="#000000", # Stroke color
|
33 |
background_color="#ffffff", # Canvas background color
|
34 |
update_streamlit=True,
|
35 |
+
height=400,
|
36 |
+
width=400,
|
37 |
drawing_mode="freedraw",
|
38 |
key="canvas",
|
39 |
)
|
40 |
|
41 |
if canvas_result.image_data is not None:
|
42 |
+
# Display the drawn image
|
43 |
st.image(canvas_result.image_data)
|
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 |
+
|
48 |
+
# Save the image to a temporary file
|
49 |
+
temp_image_path = "temp_drawing.png"
|
50 |
+
image.save(temp_image_path)
|
51 |
+
|
52 |
+
# Predict the character
|
53 |
+
model_path = "path_to_your_model.h5" # Update with your model path
|
54 |
+
predicted_char = predict_image(image, model_path)
|
55 |
+
|
56 |
+
# Display the predicted character
|
57 |
+
st.subheader(f"Predicted Character: {predicted_char}")
|
58 |
+
|