CoolFace
Apppublic

vishnu23/Text_Detection_and_Layout_Analysis

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py62 linesDownload Raw Back to root
1import streamlit as st2import pytesseract3import cv24import pdf2image5import numpy as np6import layoutparser as lp7from PIL import Image8 9 10def text_detection(img):11  res = Image.open(img)12  res = np.array(res)13  overlay = Image.open(img)14  overlay = np.array(overlay)15  with st.spinner('Processing Text Detection'):16    boxes = pytesseract.image_to_data(res)17    for i,b in enumerate(boxes.splitlines()): 18      if i!=0:19        b = b.split()20        if len(b)==12:21          x,y,w,h = int(b[6]),int(b[7]),int(b[8]),int(b[9])22          cv2.rectangle(overlay,(x-5,y-5),(w+x+5,h+y+5),(185,237,221),-1) 23          #res = cv2.putText(res,b[11],(x,y),cv2.FONT_HERSHEY_COMPLEX,1,(185,237,221),2)24  new = cv2.addWeighted(overlay, 0.4, res, 1 - 0.4, 0)25  st.subheader("Text Detection")26  st.image(new)27 28def parserIMG(path):29  res = Image.open(path)30  with st.spinner('Processing Layout Analysis'):31    model = lp.Detectron2LayoutModel('lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config',32                                  extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.5],33                                  label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"})34    layout_result = model.detect(res)35    res=lp.draw_box(res, layout_result,  box_width=5, box_alpha=0.2, show_element_type=True)36  st.subheader("Layout Analysis")37  st.image(res)38 39def parserPDF(path):40  img = np.asarray(pdf2image.convert_from_bytes(path.read()))41  st.subheader("Layout Analysis")42 43  with st.spinner('Processing Layout Analysis'):44    model = lp.Detectron2LayoutModel('lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config',45                                  extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.5],46                                  label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"})47    for i in img:48      layout_result = model.detect(i)49      i=lp.draw_box(i, layout_result,  box_width=5, box_alpha=0.2, show_element_type=True)50      st.image(i)51 52#title 53st.title('Text Detection & Layout Analysis')54#uploading file55file = st.file_uploader(label='Upload your document here',type=['png','jpg','jpeg','pdf'])56button = st.button('Confirm')57if button and file is not None:58    if file.type=="image/png" or file.type=="image/jpg" or file.type=="image/jpeg":59      parserIMG(file)60      text_detection(file)61    elif file.type == "application/pdf":62      parserPDF(file)