jacobmp commited on
Commit
f4f66f6
·
verified ·
1 Parent(s): 2739be7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -10
app.py CHANGED
@@ -7,7 +7,7 @@ from ultralytics import YOLO
7
  from PIL import Image
8
  import torch
9
 
10
- def greet(name):
11
  LINE_MODEL_PATH = "Kansallisarkisto/multicentury-textline-detection"
12
  #OCR_MODEL_PATH = "Kansallisarkisto/multicentury-htr-model"
13
  OCR_MODEL_PATH = "microsoft/trocr-large-handwritten"
@@ -17,7 +17,7 @@ def greet(name):
17
  model = VisionEncoderDecoderModel.from_pretrained(OCR_MODEL_PATH)
18
 
19
  # Open an image of handwritten text
20
- image = Image.open("/content/drive/My Dive/ocr/img/hhhhhh-x595.jpeg").convert("RGB")
21
 
22
  try:
23
  # Load the trained line detection model
@@ -34,21 +34,16 @@ def greet(name):
34
  for box in boxes:
35
  #box = box + torch.tensor([-10,0, 10, 0])
36
  box = [tensor.item() for tensor in box]
37
- #print(box)
38
  lineImg = image.crop(tuple(list(box)))
39
- #plt.imshow(lineImg)
40
- #plt.show()
41
 
42
  # Preprocess and predict
43
  pixel_values = processor(lineImg, return_tensors="pt").pixel_values
44
  generated_ids = model.generate(pixel_values)
45
  generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
46
- #print(generated_text)
47
  full_text += generated_text
48
- #print("--------------------------------------------")
49
 
50
  return full_text
51
- #print("--------------------------------------------")
52
 
53
- demo = gr.Interface(fn=greet, inputs="image", outputs="text")
54
- demo.launch()
 
 
7
  from PIL import Image
8
  import torch
9
 
10
+ def process(path):
11
  LINE_MODEL_PATH = "Kansallisarkisto/multicentury-textline-detection"
12
  #OCR_MODEL_PATH = "Kansallisarkisto/multicentury-htr-model"
13
  OCR_MODEL_PATH = "microsoft/trocr-large-handwritten"
 
17
  model = VisionEncoderDecoderModel.from_pretrained(OCR_MODEL_PATH)
18
 
19
  # Open an image of handwritten text
20
+ image = Image.open(path).convert("RGB")
21
 
22
  try:
23
  # Load the trained line detection model
 
34
  for box in boxes:
35
  #box = box + torch.tensor([-10,0, 10, 0])
36
  box = [tensor.item() for tensor in box]
 
37
  lineImg = image.crop(tuple(list(box)))
 
 
38
 
39
  # Preprocess and predict
40
  pixel_values = processor(lineImg, return_tensors="pt").pixel_values
41
  generated_ids = model.generate(pixel_values)
42
  generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
 
43
  full_text += generated_text
 
44
 
45
  return full_text
 
46
 
47
+ if __name__ == "__main__"
48
+ demo = gr.Interface(fn=process, inputs=gr.Image(type="filepath", outputs="text")
49
+ demo.launch()