mouadenna commited on
Commit
0cdfe60
1 Parent(s): 138bbdc

Update pages/4_Writing.py

Browse files
Files changed (1) hide show
  1. pages/4_Writing.py +38 -6
pages/4_Writing.py CHANGED
@@ -1,5 +1,15 @@
1
  import streamlit as st
2
  from streamlit_drawable_canvas import st_canvas
 
 
 
 
 
 
 
 
 
 
3
 
4
  def add_logo():
5
  st.markdown(
@@ -24,13 +34,24 @@ def add_logo():
24
  unsafe_allow_html=True,
25
  )
26
  add_logo()
 
 
27
 
 
 
 
 
 
 
 
 
 
28
 
29
  canvas_result = st_canvas(
30
- fill_color="rgba(255, 165, 0, 0.3)",
31
- stroke_width=10,
32
- stroke_color="#000000",
33
- background_color="#ffffff",
34
  update_streamlit=True,
35
  height=400,
36
  width=400,
@@ -38,5 +59,16 @@ canvas_result = st_canvas(
38
  key="canvas",
39
  )
40
 
41
- #if canvas_result.image_data is not None:
42
- # 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
+
7
+
8
+ arabic_chars = ['alef','beh','teh','theh','jeem','hah','khah','dal','thal','reh','zain','seen','sheen',
9
+ 'sad','dad','tah','zah','ain','ghain','feh','qaf','kaf','lam','meem','noon','heh','waw','yeh']
10
+
11
+
12
+
13
 
14
  def add_logo():
15
  st.markdown(
 
34
  unsafe_allow_html=True,
35
  )
36
  add_logo()
37
+ def predict_image(image_path, model_path):
38
+ model = load_model(model_path)
39
 
40
+ img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
41
+ img = cv2.resize(img, (32, 32))
42
+ img = img.reshape(1, 32, 32, 1)
43
+ img = img.astype('float32') / 255.0
44
+
45
+ pred = model.predict(img)
46
+ predicted_label = arabic_chars[np.argmax(pred)]
47
+
48
+ return predicted_label
49
 
50
  canvas_result = st_canvas(
51
+ fill_color="rgba(255, 255, 255, 0.3)", # Filled color (white)
52
+ stroke_width=30, # Stroke width
53
+ stroke_color="#FFFFFF", # Stroke color (white)
54
+ background_color="#000000", # Canvas background color (black)
55
  update_streamlit=True,
56
  height=400,
57
  width=400,
 
59
  key="canvas",
60
  )
61
 
62
+ if st.button("Predict"):
63
+ if canvas_result.image_data is not None:
64
+ image = canvas_result.image_data
65
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
66
+ image = cv2.resize(image, (32, 32))
67
+ cv2.imwrite("temp_image.png", image)
68
+
69
+ model_path = "saved_model.h5" # Replace with the path to your trained model
70
+ predicted_label = predict_image("temp_image.png", model_path)
71
+
72
+ st.write(f"Predicted Character: {predicted_label}")
73
+ else:
74
+ st.write("Please draw something on the canvas.")