abhishekrs4 commited on
Commit
d0ba6da
1 Parent(s): 39371b7

added flask backend app

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import json
4
+ import torch
5
+ import logging
6
+
7
+ import numpy as np
8
+ from flask import Flask, jsonify, request
9
+ from flask.wrappers import Response
10
+
11
+ from iam_line_recognition.model_main import CRNN
12
+ from iam_line_recognition.utils import ctc_decode
13
+ from iam_line_recognition.dataset import HWRecogIAMDataset
14
+
15
+ app = Flask("IAM_Handwriting_Recognition")
16
+ logging.basicConfig(level=logging.INFO)
17
+
18
+ file_model_local = "artifacts/crnn_H_32_W_768_E_196.pth"
19
+ file_model_cont = "/data/models/crnn_H_32_W_768_E_196.pth"
20
+ device = "cpu"
21
+ num_classes = len(HWRecogIAMDataset.LABEL_2_CHAR) + 1
22
+ image_height = 32
23
+ mean_arr = np.array([[0.485, 0.456, 0.406]])
24
+ std_arr = np.array([[0.229, 0.224, 0.225]])
25
+ hw_recog_model = CRNN(num_classes, image_height)
26
+
27
+ try:
28
+ logging.info(f"loading model from {file_model_local}")
29
+ hw_recog_model.load_state_dict(
30
+ torch.load(file_model_local, map_location=device)
31
+ )
32
+ except:
33
+ logging.info(f"loading model from {file_model_cont}")
34
+ hw_recog_model.load_state_dict(
35
+ torch.load(file_model_cont, map_location=device)
36
+ )
37
+ hw_recog_model.to(device)
38
+ hw_recog_model.eval()
39
+
40
+
41
+ def predict_hw(img_test: np.ndarray) -> str:
42
+ img_test = np.expand_dims(img_test, 0)
43
+ img_test = img_test.astype(np.float32) / 255.0
44
+ img_test = (img_test - mean_arr) / std_arr
45
+ img_test = np.transpose(img_test, axes=[0, 3, 1, 2])
46
+ img_tensor = torch.tensor(img_test).float()
47
+ img_tensor = img_tensor.to(device, dtype=torch.float)
48
+ log_probs = hw_recog_model(img_tensor)
49
+ pred_labels = ctc_decode(log_probs.detach())
50
+ str_pred = [HWRecogIAMDataset.LABEL_2_CHAR[i] for i in pred_labels[0]]
51
+ str_pred = "".join(str_pred)
52
+ return str_pred
53
+
54
+ @app.route("/predict", methods=["POST"])
55
+ def predict() -> Response:
56
+ logging.info("IAM Handwriting recognition app")
57
+ img_file = request.files["image_file"]
58
+ try:
59
+ img_arr = np.fromstring(img_file.read(), np.uint8)
60
+ except:
61
+ img_arr = np.fromstring(img_file.getvalue(), np.uint8)
62
+ img_dec = cv2.imdecode(img_arr, cv2.IMREAD_COLOR)
63
+ img_dec = cv2.cvtColor(img_dec, cv2.COLOR_BGR2RGB)
64
+
65
+ img_dec = cv2.resize(img_dec, (768, 32), interpolation = cv2.INTER_LINEAR)
66
+
67
+ str_pred = predict_hw(img_dec)
68
+
69
+ dict_pred = {
70
+ "file_name": img_file.filename,
71
+ "prediction": str_pred,
72
+ }
73
+ try:
74
+ json_pred = jsonify(dict_pred)
75
+ except TypeError as e:
76
+ json_pred = jsonify({"error": str(e)})
77
+ logging.info(json_pred)
78
+ return json_pred
79
+
80
+ if __name__ == "__main__":
81
+ app.run(host="0.0.0.0", debug=True, port=7860)