Spaces:
Build error
Build error
debu das
commited on
Commit
·
6135263
1
Parent(s):
c169a19
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,8 @@ import numpy as np
|
|
| 16 |
# from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 17 |
# from cv2 import dnn_superres
|
| 18 |
from transformers import DetrFeatureExtractor
|
| 19 |
-
from transformers import DetrForObjectDetection
|
|
|
|
| 20 |
import torch
|
| 21 |
import asyncio
|
| 22 |
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
|
|
@@ -36,11 +37,8 @@ def cv_to_PIL(cv_img):
|
|
| 36 |
|
| 37 |
|
| 38 |
async def pytess(cell_pil_img):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
print(pytess_output)
|
| 42 |
-
print("pytess_output@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
|
| 43 |
-
return pytess_output
|
| 44 |
|
| 45 |
# def super_res(pil_img):
|
| 46 |
# '''
|
|
@@ -142,7 +140,7 @@ def table_detector(image, THRESHOLD_PROBA):
|
|
| 142 |
feature_extractor = DetrFeatureExtractor(do_resize=True, size=800, max_size=800)
|
| 143 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 144 |
|
| 145 |
-
model =
|
| 146 |
|
| 147 |
with torch.no_grad():
|
| 148 |
outputs = model(**encoding)
|
|
@@ -165,7 +163,7 @@ def table_struct_recog(image, THRESHOLD_PROBA):
|
|
| 165 |
feature_extractor = DetrFeatureExtractor(do_resize=True, size=1000, max_size=1000)
|
| 166 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 167 |
|
| 168 |
-
model =
|
| 169 |
with torch.no_grad():
|
| 170 |
outputs = model(**encoding)
|
| 171 |
|
|
@@ -432,11 +430,9 @@ class TableExtractionPipeline():
|
|
| 432 |
'''
|
| 433 |
image = Image.open(image_path).convert("RGB")
|
| 434 |
model, probas, bboxes_scaled = table_detector(image, THRESHOLD_PROBA=TD_THRESHOLD)
|
| 435 |
-
|
| 436 |
-
st.image(image, caption='input')
|
| 437 |
-
st.text(bboxes_scaled.nelement())
|
| 438 |
if bboxes_scaled.nelement() == 0:
|
| 439 |
-
|
| 440 |
return ''
|
| 441 |
|
| 442 |
# try:
|
|
@@ -489,17 +485,21 @@ class TableExtractionPipeline():
|
|
| 489 |
if __name__ == "__main__":
|
| 490 |
|
| 491 |
img_name = st.file_uploader("Upload an image with table(s)")
|
|
|
|
|
|
|
|
|
|
| 492 |
|
| 493 |
-
|
| 494 |
-
padd_left = st.slider('Padding left', 0, 200, 20)
|
| 495 |
-
padd_right = st.slider('Padding right', 0, 200, 20)
|
| 496 |
-
padd_bottom = st.slider('Padding bottom', 0, 200, 20)
|
| 497 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 498 |
|
| 499 |
te = TableExtractionPipeline()
|
| 500 |
# for img in image_list:
|
| 501 |
if img_name is not None:
|
| 502 |
-
asyncio.run(te.start_process(img_name, TD_THRESHOLD=
|
| 503 |
|
| 504 |
|
| 505 |
|
|
|
|
| 16 |
# from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 17 |
# from cv2 import dnn_superres
|
| 18 |
from transformers import DetrFeatureExtractor
|
| 19 |
+
#from transformers import DetrForObjectDetection
|
| 20 |
+
from transformers import TableTransformerForObjectDetection
|
| 21 |
import torch
|
| 22 |
import asyncio
|
| 23 |
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
async def pytess(cell_pil_img):
|
| 40 |
+
return ' '.join(pytesseract.image_to_data(cell_pil_img, output_type=Output.DICT, config='-c tessedit_char_blacklist=œ˜â€œï¬â™Ã©œ¢!|”?«“¥ --psm 6 preserve_interword_spaces')['text']).strip()
|
| 41 |
+
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# def super_res(pil_img):
|
| 44 |
# '''
|
|
|
|
| 140 |
feature_extractor = DetrFeatureExtractor(do_resize=True, size=800, max_size=800)
|
| 141 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 142 |
|
| 143 |
+
model = TableTransformerForObjectDetection.from_pretrained("microsoft/table-transformer-detection")
|
| 144 |
|
| 145 |
with torch.no_grad():
|
| 146 |
outputs = model(**encoding)
|
|
|
|
| 163 |
feature_extractor = DetrFeatureExtractor(do_resize=True, size=1000, max_size=1000)
|
| 164 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 165 |
|
| 166 |
+
model = TableTransformerForObjectDetection.from_pretrained("microsoft/table-transformer-structure-recognition")
|
| 167 |
with torch.no_grad():
|
| 168 |
outputs = model(**encoding)
|
| 169 |
|
|
|
|
| 430 |
'''
|
| 431 |
image = Image.open(image_path).convert("RGB")
|
| 432 |
model, probas, bboxes_scaled = table_detector(image, THRESHOLD_PROBA=TD_THRESHOLD)
|
| 433 |
+
|
|
|
|
|
|
|
| 434 |
if bboxes_scaled.nelement() == 0:
|
| 435 |
+
st.write('No table found in the pdf-page image')
|
| 436 |
return ''
|
| 437 |
|
| 438 |
# try:
|
|
|
|
| 485 |
if __name__ == "__main__":
|
| 486 |
|
| 487 |
img_name = st.file_uploader("Upload an image with table(s)")
|
| 488 |
+
st1, st2 = st.columns((1,1))
|
| 489 |
+
TD_th = st1.slider('Table detection threshold', 0.0, 1.0, 0.6)
|
| 490 |
+
TSR_th = st2.slider('Table structure recognition threshold', 0.0, 1.0, 0.8)
|
| 491 |
|
| 492 |
+
st1, st2, st3, st4 = st.columns((1,1,1,1))
|
|
|
|
|
|
|
|
|
|
| 493 |
|
| 494 |
+
padd_top = st1.slider('Padding top', 0, 200, 20)
|
| 495 |
+
padd_left = st2.slider('Padding left', 0, 200, 20)
|
| 496 |
+
padd_right = st3.slider('Padding right', 0, 200, 20)
|
| 497 |
+
padd_bottom = st4.slider('Padding bottom', 0, 200, 20)
|
| 498 |
|
| 499 |
te = TableExtractionPipeline()
|
| 500 |
# for img in image_list:
|
| 501 |
if img_name is not None:
|
| 502 |
+
asyncio.run(te.start_process(img_name, TD_THRESHOLD=TD_th , TSR_THRESHOLD=TSR_th , padd_top=padd_top, padd_left=padd_left, padd_bottom=padd_bottom, padd_right=padd_right, delta_xmin=0, delta_ymin=0, delta_xmax=0, delta_ymax=0, expand_rowcol_bbox_top=0, expand_rowcol_bbox_bottom=0))
|
| 503 |
|
| 504 |
|
| 505 |
|