Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -149,15 +149,84 @@ def recognize_table(image):
|
|
| 149 |
for cell in cells:
|
| 150 |
draw.rectangle(cell["bbox"], outline="red")
|
| 151 |
|
| 152 |
-
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
|
| 155 |
def process_pdf(image):
|
| 156 |
cropped_table = detect_and_crop_table(image)
|
| 157 |
|
| 158 |
-
image = recognize_table(cropped_table)
|
| 159 |
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
|
| 163 |
title = "Demo: table detection with Table Transformer"
|
|
@@ -166,7 +235,7 @@ examples =[['image.png']]
|
|
| 166 |
|
| 167 |
app = gr.Interface(fn=process_pdf,
|
| 168 |
inputs=gr.Image(type="pil"),
|
| 169 |
-
outputs=gr.Image(type="pil", label="Detected table"),
|
| 170 |
title=title,
|
| 171 |
description=description,
|
| 172 |
examples=examples)
|
|
|
|
| 149 |
for cell in cells:
|
| 150 |
draw.rectangle(cell["bbox"], outline="red")
|
| 151 |
|
| 152 |
+
return image, cells
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def get_cell_coordinates_by_row(table_data):
|
| 156 |
+
# Extract rows and columns
|
| 157 |
+
rows = [entry for entry in table_data if entry['label'] == 'table row']
|
| 158 |
+
columns = [entry for entry in table_data if entry['label'] == 'table column']
|
| 159 |
+
|
| 160 |
+
# Sort rows and columns by their Y and X coordinates, respectively
|
| 161 |
+
rows.sort(key=lambda x: x['bbox'][1])
|
| 162 |
+
columns.sort(key=lambda x: x['bbox'][0])
|
| 163 |
+
|
| 164 |
+
# Function to find cell coordinates
|
| 165 |
+
def find_cell_coordinates(row, column):
|
| 166 |
+
cell_bbox = [column['bbox'][0], row['bbox'][1], column['bbox'][2], row['bbox'][3]]
|
| 167 |
+
return cell_bbox
|
| 168 |
+
|
| 169 |
+
# Generate cell coordinates and count cells in each row
|
| 170 |
+
cell_coordinates = []
|
| 171 |
+
|
| 172 |
+
for row in rows:
|
| 173 |
+
row_cells = []
|
| 174 |
+
for column in columns:
|
| 175 |
+
cell_bbox = find_cell_coordinates(row, column)
|
| 176 |
+
row_cells.append({'column': column['bbox'], 'cell': cell_bbox})
|
| 177 |
+
|
| 178 |
+
# Sort cells in the row by X coordinate
|
| 179 |
+
row_cells.sort(key=lambda x: x['column'][0])
|
| 180 |
+
|
| 181 |
+
# Append row information to cell_coordinates
|
| 182 |
+
cell_coordinates.append({'row': row['bbox'], 'cells': row_cells, 'cell_count': len(row_cells)})
|
| 183 |
+
|
| 184 |
+
# Sort rows from top to bottom
|
| 185 |
+
cell_coordinates.sort(key=lambda x: x['row'][1])
|
| 186 |
+
|
| 187 |
+
return cell_coordinates
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def apply_ocr(cell_coordinates):
|
| 191 |
+
# let's OCR row by row
|
| 192 |
+
data = dict()
|
| 193 |
+
max_num_columns = 0
|
| 194 |
+
for idx, row in enumerate(cell_coordinates):
|
| 195 |
+
row_text = []
|
| 196 |
+
for cell in row["cells"]:
|
| 197 |
+
# crop cell out of image
|
| 198 |
+
cell_image = np.array(cropped_table.crop(cell["cell"]))
|
| 199 |
+
# apply OCR
|
| 200 |
+
result = reader.readtext(np.array(cell_image))
|
| 201 |
+
if len(result) > 0:
|
| 202 |
+
text = " ".join([x[1] for x in result])
|
| 203 |
+
row_text.append(text)
|
| 204 |
+
|
| 205 |
+
if len(row_text) > max_num_columns:
|
| 206 |
+
max_num_columns = len(row_text)
|
| 207 |
+
|
| 208 |
+
data[idx] = row_text
|
| 209 |
+
|
| 210 |
+
# pad rows which don't have max_num_columns elements
|
| 211 |
+
# to make sure all rows have the same number of columns
|
| 212 |
+
for row, row_data in data.copy().items():
|
| 213 |
+
if len(row_data) != max_num_columns:
|
| 214 |
+
row_data = row_data + ["" for _ in range(max_num_columns - len(row_data))]
|
| 215 |
+
data[row] = row_data
|
| 216 |
+
|
| 217 |
+
return data
|
| 218 |
|
| 219 |
|
| 220 |
def process_pdf(image):
|
| 221 |
cropped_table = detect_and_crop_table(image)
|
| 222 |
|
| 223 |
+
image, cells = recognize_table(cropped_table)
|
| 224 |
|
| 225 |
+
cell_coordinates = get_cell_coordinates_by_row(cells)
|
| 226 |
+
|
| 227 |
+
data = apply_ocr(cell_coordinates)
|
| 228 |
+
|
| 229 |
+
return image, data
|
| 230 |
|
| 231 |
|
| 232 |
title = "Demo: table detection with Table Transformer"
|
|
|
|
| 235 |
|
| 236 |
app = gr.Interface(fn=process_pdf,
|
| 237 |
inputs=gr.Image(type="pil"),
|
| 238 |
+
outputs=[gr.Image(type="pil", label="Detected table"), "json"],
|
| 239 |
title=title,
|
| 240 |
description=description,
|
| 241 |
examples=examples)
|