Spaces:
Build error
Build error
Fix app.py
Browse files
app.py
CHANGED
|
@@ -40,16 +40,25 @@ structure_class_thresholds = {
|
|
| 40 |
}
|
| 41 |
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def table_structure(filename):
|
| 44 |
-
|
|
|
|
| 45 |
pred = structure_model(image, size=imgsz)
|
| 46 |
-
pred = pred.xywhn[0]
|
| 47 |
result = pred.cpu().numpy()
|
| 48 |
return result
|
| 49 |
|
| 50 |
|
| 51 |
def ocr(filename):
|
| 52 |
-
doc = DocumentFile.from_images(filename)
|
| 53 |
result = ocr_predictor(doc).export()
|
| 54 |
result = result['pages'][0]
|
| 55 |
H, W = result['dimensions']
|
|
@@ -67,7 +76,9 @@ def ocr(filename):
|
|
| 67 |
|
| 68 |
|
| 69 |
def convert_stucture(page_tokens, filename, structure_result):
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
width = image.shape[1]
|
| 72 |
height = image.shape[0]
|
| 73 |
# print(width, height)
|
|
@@ -119,7 +130,8 @@ def convert_stucture(page_tokens, filename, structure_result):
|
|
| 119 |
|
| 120 |
|
| 121 |
def visualize_cells(filename, cells, ax):
|
| 122 |
-
|
|
|
|
| 123 |
for i, cell in enumerate(cells):
|
| 124 |
bbox = cell['bbox']
|
| 125 |
x1 = int(bbox[0])
|
|
@@ -127,7 +139,7 @@ def visualize_cells(filename, cells, ax):
|
|
| 127 |
x2 = int(bbox[2])
|
| 128 |
y2 = int(bbox[3])
|
| 129 |
cv2.rectangle(image, (x1, y1), (x2, y2), color=(0, 255, 0))
|
| 130 |
-
ax.image(image)
|
| 131 |
|
| 132 |
|
| 133 |
def pytess(cell_pil_img):
|
|
@@ -234,7 +246,7 @@ def main():
|
|
| 234 |
else:
|
| 235 |
print(filename)
|
| 236 |
|
| 237 |
-
cols[0].image(
|
| 238 |
|
| 239 |
ocr_res = ocr(filename)
|
| 240 |
structure_result = table_structure(filename)
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
|
| 43 |
+
def PIL_to_cv(pil_img):
|
| 44 |
+
return cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def cv_to_PIL(cv_img):
|
| 48 |
+
return PIL.Image.fromarray(cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB))
|
| 49 |
+
|
| 50 |
+
|
| 51 |
def table_structure(filename):
|
| 52 |
+
pil_img = PIL.Image.open(filename)
|
| 53 |
+
image = PIL_to_cv(pil_img)
|
| 54 |
pred = structure_model(image, size=imgsz)
|
| 55 |
+
pred = pred.xywhn[0]
|
| 56 |
result = pred.cpu().numpy()
|
| 57 |
return result
|
| 58 |
|
| 59 |
|
| 60 |
def ocr(filename):
|
| 61 |
+
doc = DocumentFile.from_images(filename.read())
|
| 62 |
result = ocr_predictor(doc).export()
|
| 63 |
result = result['pages'][0]
|
| 64 |
H, W = result['dimensions']
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
def convert_stucture(page_tokens, filename, structure_result):
|
| 79 |
+
pil_img = PIL.Image.open(filename)
|
| 80 |
+
image = PIL_to_cv(pil_img)
|
| 81 |
+
|
| 82 |
width = image.shape[1]
|
| 83 |
height = image.shape[0]
|
| 84 |
# print(width, height)
|
|
|
|
| 130 |
|
| 131 |
|
| 132 |
def visualize_cells(filename, cells, ax):
|
| 133 |
+
pil_img = PIL.Image.open(filename)
|
| 134 |
+
image = PIL_to_cv(pil_img)
|
| 135 |
for i, cell in enumerate(cells):
|
| 136 |
bbox = cell['bbox']
|
| 137 |
x1 = int(bbox[0])
|
|
|
|
| 139 |
x2 = int(bbox[2])
|
| 140 |
y2 = int(bbox[3])
|
| 141 |
cv2.rectangle(image, (x1, y1), (x2, y2), color=(0, 255, 0))
|
| 142 |
+
ax.image(cv_to_PIL(image))
|
| 143 |
|
| 144 |
|
| 145 |
def pytess(cell_pil_img):
|
|
|
|
| 246 |
else:
|
| 247 |
print(filename)
|
| 248 |
|
| 249 |
+
cols[0].image(filename)
|
| 250 |
|
| 251 |
ocr_res = ocr(filename)
|
| 252 |
structure_result = table_structure(filename)
|