layoutdetection / app.py
Theivaprakasham's picture
Update app.py
1c6252b verified
raw
history blame
1.85 kB
import os
import gradio as gr
import numpy as np
import layoutparser as lp
from PIL import Image
import PIL
#os.system('pip install "git+https://github.com/facebookresearch/[email protected]#egg=detectron2" ')
#os.system("pip install opencv-python-headless== 4.5.5.62")
model = lp.AutoLayoutModel("lp://efficientdet/PubLayNet", label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"})
article="References<br>[1] Z. Shen, R. Zhang, M. Dell, B. C. G. Lee, J. Carlson, and W. Li, “LayoutParser: A Unified Toolkit for Deep Learning Based Document Image Analysis,” arXiv Prepr. arXiv2103.15348, 2021."
description = "Layout Detection/Parsing is one of the important tasks of converting unstructured data into structured data. This task helps to automate, digitize and organize the data in a usable format. In this project, we utilize LayoutParser library (https://github.com/Layout-Parser/layout-parser) to perform Layout Detection using pre-trained Faster_rcnn_R_50_FPN model that can classify the layout based on Text, Title, List, Table and Figure. Upload an image of a document or click an example image to check this out."
def show_preds(input_image):
img = PIL.Image.fromarray(input_image, 'RGB')
basewidth = 900
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
image_array=np.array(img)
layout = model.detect(image_array)
return lp.draw_box(image_array, layout, show_element_type=True)
#outputs = gr.outputs.Image(type="pil")
examples = [['example1.png'], ['example2.png']]
gr_interface = gr.Interface(fn=show_preds, inputs="image", outputs="image", title='Document Layout Detector/Parser', article=article, description=description, examples=examples, analytics_enabled = True)
gr_interface.launch()