Steven C commited on
Commit
58bc514
1 Parent(s): 276d36c

Accept image with blob format

Browse files
Files changed (1) hide show
  1. app.py +10 -8
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import sys
2
  import torch
3
  import onnx
@@ -8,12 +10,7 @@ from PIL import Image
8
 
9
 
10
  class DocumentParserModel:
11
- def __init__(
12
- self,
13
- model_path,
14
- img_size,
15
- charset
16
- ):
17
  self.charset = charset
18
  self.tokenizer_base = Tokenizer(self.charset)
19
  self.transform = self.create_transform_pipeline(img_size)
@@ -32,10 +29,15 @@ class DocumentParserModel:
32
  onnx.checker.check_model(onnx_model)
33
  return rt.InferenceSession(model_path)
34
 
35
- # TODO: test with image blob
 
 
 
 
 
36
  def predict_text(self, image_path):
37
  try:
38
- with Image.open(image_path) as img_org:
39
  x = self.transform(img_org.convert("RGB")).unsqueeze(0)
40
  ort_inputs = {self.ort_session.get_inputs()[0].name: x.cpu().numpy()}
41
  logits = self.ort_session.run(None, ort_inputs)[0]
 
1
+ import base64
2
+ import io
3
  import sys
4
  import torch
5
  import onnx
 
10
 
11
 
12
  class DocumentParserModel:
13
+ def __init__(self, model_path, img_size, charset):
 
 
 
 
 
14
  self.charset = charset
15
  self.tokenizer_base = Tokenizer(self.charset)
16
  self.transform = self.create_transform_pipeline(img_size)
 
29
  onnx.checker.check_model(onnx_model)
30
  return rt.InferenceSession(model_path)
31
 
32
+ def load_image_from_base64(self, base64_string):
33
+ img_data = base64.b64decode(base64_string)
34
+ image_buffer = io.BytesIO(img_data)
35
+ image = Image.open(image_buffer)
36
+ return image
37
+
38
  def predict_text(self, image_path):
39
  try:
40
+ with self.load_image_from_base64(image_path) as img_org:
41
  x = self.transform(img_org.convert("RGB")).unsqueeze(0)
42
  ort_inputs = {self.ort_session.get_inputs()[0].name: x.cpu().numpy()}
43
  logits = self.ort_session.run(None, ort_inputs)[0]