Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from ultralytics import YOLO
|
|
| 7 |
from PIL import Image
|
| 8 |
import torch
|
| 9 |
|
| 10 |
-
def
|
| 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(
|
| 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 |
-
|
| 54 |
-
demo.
|
|
|
|
|
|
| 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()
|