Sobit commited on
Commit
a286c52
โ€ข
1 Parent(s): 6f6b918

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -31
app.py CHANGED
@@ -1,17 +1,14 @@
1
- from fastapi import FastAPI, UploadFile, File
2
- import uvicorn
3
  import tensorflow as tf
4
  from tensorflow import keras
5
  from keras import models
6
  from PIL import Image
7
- from io import BytesIO
8
  import numpy as np
9
  import cv2
 
10
 
11
  # Some constants to be used in the program
12
  IMG_SIZE = (32,32)
13
- APP_HOST = '127.0.1.1'
14
- APP_PORT = '5000'
15
 
16
  # Character mapping for the character prediction
17
  char_map = {
@@ -25,41 +22,35 @@ char_map = {
25
  56:'๐‘ฌ(RA)', 57: '๐‘ฎ(LA)', 58:'๐‘ฐ(WA)', 59:'๐‘ฑ(SHA)', 60: '๐‘ฑ(SHA-alt)', 61: '๐‘ฒ(SSA)', 62: '๐‘ณ(SA)', 63: '๐‘ด(HA)'
26
  }
27
 
28
-
29
-
30
  # Importing the model
31
- model = models.load_model('vgg.h5')
32
-
33
-
34
- # Defining the FastAPI instance here
35
- app = FastAPI()
36
 
37
  # Function for reading image
38
- def file_to_array(data) -> np.ndarray:
39
- image = np.array(Image.open(BytesIO(data)))
40
 
41
  return image
42
 
 
 
 
 
43
 
44
- @app.get('/')
45
- async def root_func():
46
- return {'message': 'this is the root function'}
47
-
48
-
49
- @app.post('/predict_image')
50
- async def upload_image(file: UploadFile = File(...)):
51
- image = Image.open(BytesIO(await file.read()))
52
 
53
- image = cv2.resize(np.array(image), IMG_SIZE)
54
- image = image.astype('float32')
55
- image = np.expand_dims(image, axis=0)
56
 
57
- output = model.predict(image)
58
- result = char_map[np.argmax(output)]
59
-
60
- return {'prediction': result}
61
 
 
 
 
 
62
 
63
-
64
  if __name__ == "__main__":
65
- uvicorn.run(app, host=APP_HOST, port=APP_PORT)
 
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 = {
 
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()