File size: 2,382 Bytes
8069dfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import streamlit as st
import pytesseract
import cv2
import pdf2image
import numpy as np
import layoutparser as lp
from PIL import Image


def text_detection(img):
  res = Image.open(img)
  res = np.array(res)
  overlay = Image.open(img)
  overlay = np.array(overlay)
  with st.spinner('Processing Text Detection'):
    boxes = pytesseract.image_to_data(res)
    for i,b in enumerate(boxes.splitlines()): 
      if i!=0:
        b = b.split()
        if len(b)==12:
          x,y,w,h = int(b[6]),int(b[7]),int(b[8]),int(b[9])
          cv2.rectangle(overlay,(x-5,y-5),(w+x+5,h+y+5),(185,237,221),-1) 
          #res = cv2.putText(res,b[11],(x,y),cv2.FONT_HERSHEY_COMPLEX,1,(185,237,221),2)
  new = cv2.addWeighted(overlay, 0.4, res, 1 - 0.4, 0)
  st.subheader("Text Detection")
  st.image(new)

def parserIMG(path):
  res = Image.open(path)
  with st.spinner('Processing Layout Analysis'):
    model = lp.Detectron2LayoutModel('lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config',
                                  extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.5],
                                  label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"})
    layout_result = model.detect(res)
    res=lp.draw_box(res, layout_result,  box_width=5, box_alpha=0.2, show_element_type=True)
  st.subheader("Layout Analysis")
  st.image(res)

def parserPDF(path):
  img = np.asarray(pdf2image.convert_from_bytes(path.read()))
  st.subheader("Layout Analysis")

  with st.spinner('Processing Layout Analysis'):
    model = lp.Detectron2LayoutModel('lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config',
                                  extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.5],
                                  label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"})
    for i in img:
      layout_result = model.detect(i)
      i=lp.draw_box(i, layout_result,  box_width=5, box_alpha=0.2, show_element_type=True)
      st.image(i)

#title 
st.title('Text Detection & Layout Analysis')
#uploading file
file = st.file_uploader(label='Upload your document here',type=['png','jpg','jpeg','pdf'])
button = st.button('Confirm')
if button and file is not None:
    if file.type=="image/png" or file.type=="image/jpg" or file.type=="image/jpeg":
      parserIMG(file)
      text_detection(file)
    elif file.type == "application/pdf":
      parserPDF(file)