Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,7 +58,7 @@ def parse_alto_xml(xml_file_path):
|
|
| 58 |
except (ValueError, TypeError) as e:
|
| 59 |
print(f"Warning: Could not parse coordinates for '{text}': {e}")
|
| 60 |
ocr_data.append({
|
| 61 |
-
'text': text, 'x': 0, 'y': 0, 'w': 10, 'h': 10
|
| 62 |
})
|
| 63 |
if line_text_parts:
|
| 64 |
full_text_lines.append(" ".join(line_text_parts))
|
|
@@ -81,10 +81,15 @@ def draw_ocr_on_image(image_pil, ocr_data):
|
|
| 81 |
draw = ImageDraw.Draw(image_pil)
|
| 82 |
|
| 83 |
try:
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
font_size = max(8, int(avg_height * 0.6))
|
| 86 |
font = ImageFont.truetype("arial.ttf", font_size)
|
| 87 |
-
except (IOError, ZeroDivisionError):
|
| 88 |
font = ImageFont.load_default()
|
| 89 |
font_size = 10
|
| 90 |
print("Arial font not found or issue with height calculation, using default font.")
|
|
@@ -93,8 +98,12 @@ def draw_ocr_on_image(image_pil, ocr_data):
|
|
| 93 |
x, y, w, h = item['x'], item['y'], item['w'], item['h']
|
| 94 |
text = item['text']
|
| 95 |
draw.rectangle([(x, y), (x + w, y + h)], outline="red", width=2)
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
return image_pil
|
| 100 |
|
|
@@ -105,33 +114,33 @@ def process_image_and_xml(image_path, xml_path, show_overlay):
|
|
| 105 |
Main function for the Gradio interface.
|
| 106 |
image_path and xml_path are now file paths (strings).
|
| 107 |
"""
|
| 108 |
-
if image_path is None:
|
| 109 |
return None, "Please upload an image.", None
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
img_pil_orig = Image.open(image_path).convert("RGB")
|
| 113 |
-
except Exception as e:
|
| 114 |
-
return None, f"Error loading image: {e}. Also, please upload an OCR XML file.", None
|
| 115 |
-
return img_pil_orig, "Please upload an OCR XML file.", None
|
| 116 |
-
|
| 117 |
try:
|
| 118 |
img_pil = Image.open(image_path).convert("RGB")
|
| 119 |
except Exception as e:
|
| 120 |
return None, f"Error loading image: {e}", None
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
extracted_text, ocr_box_data = parse_alto_xml(xml_path)
|
| 123 |
|
| 124 |
overlay_image_pil = None
|
| 125 |
-
if show_overlay
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
return img_pil, extracted_text, overlay_image_pil
|
| 136 |
|
| 137 |
|
|
@@ -163,8 +172,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 163 |
def update_interface(image_filepath, xml_filepath, show_overlay_val):
|
| 164 |
if image_filepath is None and xml_filepath is None:
|
| 165 |
return None, "Please upload an image and an XML file.", None
|
| 166 |
-
|
| 167 |
-
return None, "Please upload an image file.", None
|
| 168 |
|
| 169 |
img, text, overlay_img = process_image_and_xml(image_filepath, xml_filepath, show_overlay_val)
|
| 170 |
|
|
@@ -185,8 +193,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 185 |
gr.Markdown("---")
|
| 186 |
gr.Markdown("### Example ALTO XML Snippet (for `String` element extraction):")
|
| 187 |
gr.Code(
|
| 188 |
-
# Corrected:
|
| 189 |
-
language="text", # Or language=None
|
| 190 |
value="""
|
| 191 |
<alto xmlns="http://www.loc.gov/standards/alto/v3/alto.xsd">
|
| 192 |
<Description>...</Description>
|
|
@@ -219,20 +226,13 @@ if __name__ == "__main__":
|
|
| 219 |
img.save("dummy_image.png")
|
| 220 |
print("Created dummy_image.png for testing.")
|
| 221 |
|
| 222 |
-
example_xml_filename = "189819724.34.xml"
|
| 223 |
if not os.path.exists(example_xml_filename):
|
| 224 |
print(f"WARNING: Example XML '{example_xml_filename}' not found. Please create it or upload your own.")
|
| 225 |
-
# Example of writing the provided XML if it's missing for easier testing
|
| 226 |
-
# Note: The provided XML is partial, so this would be a placeholder
|
| 227 |
-
# If you have the full XML, you can paste it here.
|
| 228 |
-
# For now, just a warning.
|
| 229 |
-
# with open(example_xml_filename, "w", encoding="utf-8") as f:
|
| 230 |
-
# f.write('<?xml version="1.0" encoding="UTF-8"?>\n<alto xmlns="http://www.loc.gov/standards/alto/v3/alto.xsd"><Description><MeasurementUnit>pixel</MeasurementUnit><sourceImageInformation><fileName>./data/pdfs/189742210.pdf</fileName></sourceImageInformation><OCRProcessing ID="IdOcr"><ocrProcessingStep><processingDateTime>Wed Nov 20 19:03:43 2019\n</processingDateTime><processingSoftware><softwareCreator>CONTRIBUTORS</softwareCreator><softwareName>pdfalto</softwareName><softwareVersion>0.1</softwareVersion></processingSoftware></ocrProcessingStep></OCRProcessing></Description><Styles><TextStyle ID="font0" FONTFAMILY="helvetica" FONTSIZE="21.000" FONTTYPE="sans-serif" FONTWIDTH="proportional" FONTCOLOR="#WWWWWW" FONTSTYLE="italics"/><TextStyle ID="font1" FONTFAMILY="helvetica" FONTSIZE="10.000" FONTTYPE="sans-serif" FONTWIDTH="proportional" FONTCOLOR="#WWWWWW" FONTSTYLE="italics"/><TextStyle ID="font2" FONTFAMILY="helvetica" FONTSIZE="10.000" FONTTYPE="sans-serif" FONTWIDTH="proportional" FONTCOLOR="#WWWWWW" FONTSTYLE=""/></Styles><Layout><Page ID="Page13" PHYSICAL_IMG_NR="13" WIDTH="2394" HEIGHT="3612"><PrintSpace><TextLine WIDTH="684" HEIGHT="108" ID="p13_t1" HPOS="465" VPOS="196"><String ID="p13_w1" CONTENT="Introduction" HPOS="465" VPOS="196" WIDTH="684" HEIGHT="108" STYLEREFS="font0"/></TextLine></PrintSpace></Page></Layout></alto>') # Shortened for brevity
|
| 231 |
-
# print(f"Created a placeholder '{example_xml_filename}'. Please replace with full XML if needed.")
|
| 232 |
|
| 233 |
except ImportError:
|
| 234 |
print("Pillow not installed, can't create dummy image.")
|
| 235 |
except Exception as e:
|
| 236 |
-
print(f"Error
|
| 237 |
|
| 238 |
demo.launch()
|
|
|
|
| 58 |
except (ValueError, TypeError) as e:
|
| 59 |
print(f"Warning: Could not parse coordinates for '{text}': {e}")
|
| 60 |
ocr_data.append({
|
| 61 |
+
'text': text, 'x': 0, 'y': 0, 'w': 10, 'h': 10 # Placeholder
|
| 62 |
})
|
| 63 |
if line_text_parts:
|
| 64 |
full_text_lines.append(" ".join(line_text_parts))
|
|
|
|
| 81 |
draw = ImageDraw.Draw(image_pil)
|
| 82 |
|
| 83 |
try:
|
| 84 |
+
# Filter for items with positive height before calculating average
|
| 85 |
+
valid_heights = [d['h'] for d in ocr_data if d['h'] > 0]
|
| 86 |
+
if valid_heights:
|
| 87 |
+
avg_height = sum(valid_heights) / len(valid_heights)
|
| 88 |
+
else:
|
| 89 |
+
avg_height = 10 # Default if no valid heights
|
| 90 |
font_size = max(8, int(avg_height * 0.6))
|
| 91 |
font = ImageFont.truetype("arial.ttf", font_size)
|
| 92 |
+
except (IOError, ZeroDivisionError): # ZeroDivisionError should be caught by the check above
|
| 93 |
font = ImageFont.load_default()
|
| 94 |
font_size = 10
|
| 95 |
print("Arial font not found or issue with height calculation, using default font.")
|
|
|
|
| 98 |
x, y, w, h = item['x'], item['y'], item['w'], item['h']
|
| 99 |
text = item['text']
|
| 100 |
draw.rectangle([(x, y), (x + w, y + h)], outline="red", width=2)
|
| 101 |
+
# Adjust text position to be slightly above the box, or below if no space above
|
| 102 |
+
text_y_position = y - font_size - 2
|
| 103 |
+
if text_y_position < 0: # If text would go off the top of the image
|
| 104 |
+
text_y_position = y + h + 2 # Place below the box
|
| 105 |
+
|
| 106 |
+
draw.text((x + 2, text_y_position), text, fill="green", font=font)
|
| 107 |
|
| 108 |
return image_pil
|
| 109 |
|
|
|
|
| 114 |
Main function for the Gradio interface.
|
| 115 |
image_path and xml_path are now file paths (strings).
|
| 116 |
"""
|
| 117 |
+
if image_path is None: # If no image is uploaded at all
|
| 118 |
return None, "Please upload an image.", None
|
| 119 |
+
|
| 120 |
+
# Try to open the image first, as it's needed for both outputs if XML is missing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
try:
|
| 122 |
img_pil = Image.open(image_path).convert("RGB")
|
| 123 |
except Exception as e:
|
| 124 |
return None, f"Error loading image: {e}", None
|
| 125 |
|
| 126 |
+
if xml_path is None: # If XML is missing, but image is present
|
| 127 |
+
return img_pil, "Please upload an OCR XML file.", None
|
| 128 |
+
|
| 129 |
+
# Both image and XML are presumably present
|
| 130 |
extracted_text, ocr_box_data = parse_alto_xml(xml_path)
|
| 131 |
|
| 132 |
overlay_image_pil = None
|
| 133 |
+
if show_overlay:
|
| 134 |
+
if ocr_box_data:
|
| 135 |
+
img_for_overlay = img_pil.copy()
|
| 136 |
+
overlay_image_pil = draw_ocr_on_image(img_for_overlay, ocr_box_data)
|
| 137 |
+
elif not (isinstance(extracted_text, str) and extracted_text.startswith("Error")):
|
| 138 |
+
# Append message if overlay is checked but no boxes, and no major XML parse error
|
| 139 |
+
if isinstance(extracted_text, str):
|
| 140 |
+
extracted_text += "\n(No bounding box data found or parsed for overlay)"
|
| 141 |
+
else: # Should ideally not happen based on parse_alto_xml's return
|
| 142 |
+
extracted_text = "(No bounding box data found or parsed for overlay)"
|
| 143 |
+
|
| 144 |
return img_pil, extracted_text, overlay_image_pil
|
| 145 |
|
| 146 |
|
|
|
|
| 172 |
def update_interface(image_filepath, xml_filepath, show_overlay_val):
|
| 173 |
if image_filepath is None and xml_filepath is None:
|
| 174 |
return None, "Please upload an image and an XML file.", None
|
| 175 |
+
# `process_image_and_xml` now handles cases where one is None
|
|
|
|
| 176 |
|
| 177 |
img, text, overlay_img = process_image_and_xml(image_filepath, xml_filepath, show_overlay_val)
|
| 178 |
|
|
|
|
| 193 |
gr.Markdown("---")
|
| 194 |
gr.Markdown("### Example ALTO XML Snippet (for `String` element extraction):")
|
| 195 |
gr.Code(
|
| 196 |
+
# Corrected: Omitted language parameter
|
|
|
|
| 197 |
value="""
|
| 198 |
<alto xmlns="http://www.loc.gov/standards/alto/v3/alto.xsd">
|
| 199 |
<Description>...</Description>
|
|
|
|
| 226 |
img.save("dummy_image.png")
|
| 227 |
print("Created dummy_image.png for testing.")
|
| 228 |
|
| 229 |
+
example_xml_filename = "189819724.34.xml" # Make sure this file exists with your XML content
|
| 230 |
if not os.path.exists(example_xml_filename):
|
| 231 |
print(f"WARNING: Example XML '{example_xml_filename}' not found. Please create it or upload your own.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
except ImportError:
|
| 234 |
print("Pillow not installed, can't create dummy image.")
|
| 235 |
except Exception as e:
|
| 236 |
+
print(f"Error during setup: {e}")
|
| 237 |
|
| 238 |
demo.launch()
|