-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
55 lines (45 loc) · 1.38 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from network import Net
from PIL import Image
import torch
from flask import Flask, jsonify, request
from flask_cors import CORS
import requests
import io
from PIL import Image
import torchvision.transforms as transform
import base64
app = Flask(__name__)
CORS(app)
model_dict = torch.load('model/malayalamOCR.pt',
map_location=lambda storage, loc: storage)
model = Net()
model.load_state_dict(model_dict["model"])
def transform_image(image_bytes):
transformations = transform.Compose([
transform.Grayscale(1),
transform.Resize(255),
transform.CenterCrop(224),
transform.ToTensor(),
transform.Normalize([0.5], [0.5])
])
image = Image.open(image_bytes)
return transformations(image).unsqueeze(0)
def get_prediction(image_bytes):
tensor = transform_image(image_bytes=image_bytes)
model.eval()
outputs = model(tensor)
_, pred = outputs.max(1)
return pred.item()
@app.route('/')
def hello():
return 'Hello World!'
@app.route("/predict", methods=["POST"])
def predict():
if request.method == 'POST':
canvas = request.json["canvas"].split(',')[1]
with open("canvas.png", "wb") as f:
f.write(base64.decodebytes(canvas.encode()))
f.close()
character = get_prediction("./canvas.png")
print(character)
return jsonify({"alphabet": character})