Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,14 @@
|
|
| 1 |
-
|
| 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('
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# Defining the FastAPI instance here
|
| 35 |
-
app = FastAPI()
|
| 36 |
|
| 37 |
# Function for reading image
|
| 38 |
-
def file_to_array(
|
| 39 |
-
image = np.array(Image.open(BytesIO(
|
| 40 |
|
| 41 |
return image
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 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 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
if __name__ == "__main__":
|
| 65 |
-
|
|
|
|
| 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()
|