Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pytesseract
|
3 |
+
import cv2
|
4 |
+
import pdf2image
|
5 |
+
import numpy as np
|
6 |
+
import layoutparser as lp
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
|
10 |
+
def 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 |
+
|
28 |
+
def 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 |
+
|
39 |
+
def 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
|
53 |
+
st.title('Text Detection & Layout Analysis')
|
54 |
+
#uploading file
|
55 |
+
file = st.file_uploader(label='Upload your document here',type=['png','jpg','jpeg','pdf'])
|
56 |
+
button = st.button('Confirm')
|
57 |
+
if 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)
|