Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import math
|
| 3 |
+
import re
|
| 4 |
+
import ast
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from doctr.io import DocumentFile
|
| 9 |
+
from doctr.models import ocr_predictor
|
| 10 |
+
from PIL import Image, ImageDraw
|
| 11 |
+
path = r""
|
| 12 |
+
|
| 13 |
+
if os.path.exists('tp'):
|
| 14 |
+
img_temp = r"tp"
|
| 15 |
+
else:
|
| 16 |
+
os.mkdir(os.path.join(path, "tp"))
|
| 17 |
+
img_temp = r"tp"
|
| 18 |
+
|
| 19 |
+
if os.path.exists('tp1'):
|
| 20 |
+
sub_img_temp = r"tp1"
|
| 21 |
+
else:
|
| 22 |
+
os.mkdir(os.path.join(path, "tp1"))
|
| 23 |
+
sub_img_temp = r"tp1"
|
| 24 |
+
|
| 25 |
+
def load_model():
|
| 26 |
+
return ocr_predictor(
|
| 27 |
+
# det_arch="db_resnet50_rotation",
|
| 28 |
+
# reco_arch="crnn_mobilenet_v3_large",
|
| 29 |
+
det_arch='linknet_resnet18_rotation',
|
| 30 |
+
reco_arch='crnn_vgg16_bn',
|
| 31 |
+
detect_orientation=True,
|
| 32 |
+
assume_straight_pages=False,
|
| 33 |
+
pretrained=True,
|
| 34 |
+
pretrained_backbone=True,
|
| 35 |
+
export_as_straight_boxes=True,
|
| 36 |
+
preserve_aspect_ratio=True,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
def convert_coordinates(geometry, page_dim, i, j):
|
| 40 |
+
len_x = page_dim[1]
|
| 41 |
+
len_y = page_dim[0]
|
| 42 |
+
(x_min, y_min) = geometry[0]
|
| 43 |
+
(x_max, y_max) = geometry[1]
|
| 44 |
+
x_min = (math.floor(x_min * len_x)) + i*len_x
|
| 45 |
+
x_max = (math.ceil(x_max * len_x)) + i*len_x
|
| 46 |
+
y_min = (math.floor(y_min * len_y)) + j*len_y
|
| 47 |
+
y_max = (math.ceil(y_max * len_y)) + j*len_y
|
| 48 |
+
return [x_min, x_max, y_min, y_max]
|
| 49 |
+
|
| 50 |
+
def get_coordinates(output, x, y):
|
| 51 |
+
page_dim = output['pages'][0]["dimensions"]
|
| 52 |
+
raw_data = []
|
| 53 |
+
for obj1 in output['pages'][0]["blocks"]:
|
| 54 |
+
for obj2 in obj1["lines"]:
|
| 55 |
+
for obj3 in obj2["words"]:
|
| 56 |
+
converted_coordinates = convert_coordinates(obj3["geometry"],page_dim, x, y)
|
| 57 |
+
raw_data.append("{}: {}".format(converted_coordinates,obj3["value"]))
|
| 58 |
+
return raw_data
|
| 59 |
+
|
| 60 |
+
def get_vals(file_path, wh):
|
| 61 |
+
model = load_model()
|
| 62 |
+
Data, counter = [], 1
|
| 63 |
+
for i in range(wh): # split_var is fixed
|
| 64 |
+
for j in range(wh):
|
| 65 |
+
path = f"{file_path}/{counter}.jpg"
|
| 66 |
+
temp_doc = DocumentFile.from_images(path)
|
| 67 |
+
output = model(temp_doc).export()
|
| 68 |
+
data = get_coordinates(output, i, j)
|
| 69 |
+
counter += 1
|
| 70 |
+
Data.extend(data)
|
| 71 |
+
return Data
|
| 72 |
+
|
| 73 |
+
def clean_dir(path):
|
| 74 |
+
files = os.listdir(path=path)
|
| 75 |
+
for i in range(1,len(files)+1):
|
| 76 |
+
os.remove(f"{path}/{i}.jpg")
|
| 77 |
+
|
| 78 |
+
def html_path(img, counter):
|
| 79 |
+
img.save(f"{sub_img_temp}/{counter}.jpg")
|
| 80 |
+
return f"<img src='/file={sub_img_temp}/{counter}.jpg'></img>"
|
| 81 |
+
|
| 82 |
+
def create_box(l): # l represents the bounds of box
|
| 83 |
+
return (l[0], l[2], l[1], l[3])
|
| 84 |
+
|
| 85 |
+
def process(filepath, regex, size=(1656,1170)):
|
| 86 |
+
clean_dir(path=img_temp)
|
| 87 |
+
clean_dir(path=sub_img_temp)
|
| 88 |
+
img = Image.open(filepath)
|
| 89 |
+
(width, height), parts, counter, dimensions, im_, values = img.size, [], 0, [], [], []
|
| 90 |
+
for i in range(0, width, size[0]):
|
| 91 |
+
for j in range(0, height, size[1]):
|
| 92 |
+
counter += 1
|
| 93 |
+
box = (i, j, i+size[0], j+size[1])
|
| 94 |
+
img.crop(box).save(f"{img_temp}/{counter}.jpg")
|
| 95 |
+
parts.append(img.crop(box))
|
| 96 |
+
temp= os.listdir(path=img_temp) # temp represents a temporary variable that contains directory information
|
| 97 |
+
if regex == 'Regex-1':
|
| 98 |
+
pattern = re.compile(r"^\s\b\d+([\.,]\d+)?")
|
| 99 |
+
else:
|
| 100 |
+
pattern = re.compile(r"\d+")
|
| 101 |
+
|
| 102 |
+
data = get_vals(img_temp, wh=math.floor(math.sqrt(len(temp))))
|
| 103 |
+
counter, idx = 1, []
|
| 104 |
+
for d in data:
|
| 105 |
+
dimensions.append(ast.literal_eval(d.split(':')[0]))
|
| 106 |
+
im_.append(html_path(img.crop(create_box(ast.literal_eval(d.split(':')[0]))), counter=counter))
|
| 107 |
+
values.append(d.split(':')[1])
|
| 108 |
+
counter += 1
|
| 109 |
+
metadata = pd.DataFrame(zip(dimensions, im_, values), columns=['Coordinates','Image','Value'])
|
| 110 |
+
df = metadata[metadata['Value'].str.contains(pattern)] #[img.size] moreover df is a chunk taken from metadata which contains the regex pattern.
|
| 111 |
+
|
| 112 |
+
return df#.to_markdown()
|
| 113 |
+
|
| 114 |
+
def main():
|
| 115 |
+
|
| 116 |
+
demo = gr.Interface(
|
| 117 |
+
fn=process,
|
| 118 |
+
inputs=[gr.Image(type="filepath", interactive=True),gr.Dropdown(['Regex-1'])],
|
| 119 |
+
outputs=gr.DataFrame(wrap=True, max_rows=10, overflow_row_behaviour= "paginate", datatype = ["str", "markdown", "str"], interactive=True),
|
| 120 |
+
title="OCR"
|
| 121 |
+
)
|
| 122 |
+
demo.launch(debug=True, show_error=True)
|
| 123 |
+
|
| 124 |
+
if __name__=="__main__":
|
| 125 |
+
main()
|