Sobit commited on
Commit
ac72df0
โ€ข
1 Parent(s): def1d5d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tensorflow as tf
3
+ from tensorflow import keras
4
+ from keras import models
5
+ from PIL import Image
6
+ import numpy as np
7
+ import cv2
8
+ import io
9
+
10
+ # Some constants to be used in the program
11
+ IMG_SIZE = (32,32)
12
+
13
+ # Character mapping for the character prediction
14
+ char_map = {
15
+ 0:'๐‘‘(0)', 1:'๐‘‘‘(1)', 2:'๐‘‘’(2)', 3:'๐‘‘“(3)', 4: '๐‘‘”(4)', 5: '๐‘‘•(5)', 6: '๐‘‘–(6)', 7: '๐‘‘—(7)',
16
+ 8:'๐‘‘˜(8)', 9:'๐‘‘™(9)', 10:'๐‘‘‰(OM)', 11:'๐‘€(A)', 12: '๐‘(AA)', 13: '๐‘€๐‘‘…(AH)', 14: '๐‘‚(I)',
17
+ 15:'๐‘ƒ(II)',16:'๐‘„(U)', 17:'๐‘…(UU)', 18:'๐‘†(R)', 19: '๐‘†๐‘บ(RR)', 20: '๐‘Š(E)', 21: '๐‘‹(AI)', 22: '๐‘Œ(O)',
18
+ 23:'๐‘(AU)', 24:'๐‘ˆ(L)', 25:'๐‘‰(LL)', 26:'๐‘Ž(KA)', 27: '๐‘Ž๐‘‘‚๐‘ณ(KSA)', 28: '๐‘(KHA)',29: '๐‘(GA)', 30: '๐‘‘(GHA)',
19
+ 31:'๐‘’(NGA)',32:'๐‘”(CA)', 33:'๐‘•(CHA)', 34:'๐‘–(JA)', 35: '๐‘–๐‘‘‚๐‘˜(JรฑA)', 36: '๐‘—(JHA)',37: '๐‘—(JHA-alt)',38: '๐‘˜(NYA)',
20
+ 39:'๐‘š(TA)', 40:'๐‘›(TTHA)', 41:'๐‘œ(DDA)', 42:'๐‘(DHA)', 43: '๐‘ž(NNA)', 44: '๐‘Ÿ(TA)', 45: '๐‘Ÿ๐‘‘‚๐‘ฌ(TRA)', 46: '๐‘ (THA)',
21
+ 47:'๐‘ก(DA)', 49:'๐‘ฃ(NA)', 50:'๐‘ฅ(PA)', 51:'๐‘ฆ(PHA)', 52: '๐‘ง(BA)', 53: '๐‘จ(BHA)', 54: '๐‘ฉ(MA)', 55: '๐‘ซ(YA)',
22
+ 56:'๐‘ฌ(RA)', 57: '๐‘ฎ(LA)', 58:'๐‘ฐ(WA)', 59:'๐‘ฑ(SHA)', 60: '๐‘ฑ(SHA-alt)', 61: '๐‘ฒ(SSA)', 62: '๐‘ณ(SA)', 63: '๐‘ด(HA)'
23
+ }
24
+
25
+ # Importing the model
26
+ model = models.load_model('tf_model.h5')
27
+
28
+ # Function for reading image
29
+ def file_to_array(file) -> np.ndarray:
30
+ image = np.array(Image.open(io.BytesIO(file)))
31
+
32
+ return image
33
+
34
+ # Main Streamlit app
35
+ def main():
36
+ st.title("Character Recognition")
37
+ st.write("Upload an image and the model will predict the character")
38
+
39
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
40
+
41
+ if uploaded_file is not None:
42
+ image = Image.open(uploaded_file)
43
+ st.image(image, caption='Uploaded Image.', use_column_width=True)
44
+
45
+ if st.button('Predict'):
46
+ image = cv2.resize(np.array(image), IMG_SIZE)
47
+ image = image.astype('float32')
48
+ image = np.expand_dims(image, axis=0)
49
+
50
+ output = model.predict(image)
51
+ result = char_map[np.argmax(output)]
52
+
53
+ st.success(f'Prediction: {result}')
54
+
55
+ if __name__ == "__main__":
56
+ main()