|
from fastapi import FastAPI, UploadFile, File |
|
import uvicorn |
|
import tensorflow as tf |
|
from tensorflow import keras |
|
from keras import models |
|
from PIL import Image |
|
from io import BytesIO |
|
import numpy as np |
|
import cv2 |
|
|
|
|
|
IMG_SIZE = (32,32) |
|
APP_HOST = '127.0.1.1' |
|
APP_PORT = '5000' |
|
|
|
|
|
char_map = { |
|
0:'๐(0)', 1:'๐(1)', 2:'๐(2)', 3:'๐(3)', 4: '๐(4)', 5: '๐(5)', 6: '๐(6)', 7: '๐(7)', |
|
8:'๐(8)', 9:'๐(9)', 10:'๐(OM)', 11:'๐(A)', 12: '๐(AA)', 13: '๐๐
(AH)', 14: '๐(I)', |
|
15:'๐(II)',16:'๐(U)', 17:'๐
(UU)', 18:'๐(R)', 19: '๐๐บ(RR)', 20: '๐(E)', 21: '๐(AI)', 22: '๐(O)', |
|
23:'๐(AU)', 24:'๐(L)', 25:'๐(LL)', 26:'๐(KA)', 27: '๐๐๐ณ(KSA)', 28: '๐(KHA)',29: '๐(GA)', 30: '๐(GHA)', |
|
31:'๐(NGA)',32:'๐(CA)', 33:'๐(CHA)', 34:'๐(JA)', 35: '๐๐๐(JรฑA)', 36: '๐(JHA)',37: '๐(JHA-alt)',38: '๐(NYA)', |
|
39:'๐(TA)', 40:'๐(TTHA)', 41:'๐(DDA)', 42:'๐(DHA)', 43: '๐(NNA)', 44: '๐(TA)', 45: '๐๐๐ฌ(TRA)', 46: '๐ (THA)', |
|
47:'๐ก(DA)', 49:'๐ฃ(NA)', 50:'๐ฅ(PA)', 51:'๐ฆ(PHA)', 52: '๐ง(BA)', 53: '๐จ(BHA)', 54: '๐ฉ(MA)', 55: '๐ซ(YA)', |
|
56:'๐ฌ(RA)', 57: '๐ฎ(LA)', 58:'๐ฐ(WA)', 59:'๐ฑ(SHA)', 60: '๐ฑ(SHA-alt)', 61: '๐ฒ(SSA)', 62: '๐ณ(SA)', 63: '๐ด(HA)' |
|
} |
|
|
|
|
|
|
|
|
|
model = models.load_model('vgg.h5') |
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
def file_to_array(data) -> np.ndarray: |
|
image = np.array(Image.open(BytesIO(data))) |
|
|
|
return image |
|
|
|
|
|
@app.get('/') |
|
async def root_func(): |
|
return {'message': 'this is the root function'} |
|
|
|
|
|
@app.post('/predict_image') |
|
async def upload_image(file: UploadFile = File(...)): |
|
image = Image.open(BytesIO(await file.read())) |
|
|
|
image = cv2.resize(np.array(image), IMG_SIZE) |
|
image = image.astype('float32') |
|
image = np.expand_dims(image, axis=0) |
|
|
|
output = model.predict(image) |
|
result = char_map[np.argmax(output)] |
|
|
|
return {'prediction': result} |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
uvicorn.run(app, host=APP_HOST, port=APP_PORT) |