Update README.md
Browse files
README.md
CHANGED
|
@@ -1,5 +1,93 @@
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
-
|
| 4 |
-
-
|
| 5 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
+
tags:
|
| 4 |
+
- object-detection
|
| 5 |
+
- document-layout-analysis
|
| 6 |
+
- historical-documents
|
| 7 |
+
- layoutparser
|
| 8 |
+
- detectron2
|
| 9 |
+
- mask-rcnn
|
| 10 |
+
language:
|
| 11 |
+
- sv
|
| 12 |
+
pipeline_tag: object-detection
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# Historical Document Layout Detection Model
|
| 16 |
+
|
| 17 |
+
A fine-tuned Mask R-CNN model (via LayoutParser/Detectron2) for detecting layout
|
| 18 |
+
elements in historical Swedish Medical journal pages.
|
| 19 |
+
|
| 20 |
+
## Model Details
|
| 21 |
+
|
| 22 |
+
- **Model type:** Mask R-CNN (ResNet backbone)
|
| 23 |
+
- **Framework:** Detectron2 / LayoutParser
|
| 24 |
+
- **Fine-tuned for:** Historical document layout analysis
|
| 25 |
+
- **Language of source documents:** Swedish
|
| 26 |
+
|
| 27 |
+
## Label Map
|
| 28 |
+
|
| 29 |
+
| ID | Label |
|
| 30 |
+
|----|-------------------|
|
| 31 |
+
| 0 | Advertisement |
|
| 32 |
+
| 1 | Author |
|
| 33 |
+
| 2 | Header or Footer |
|
| 34 |
+
| 3 | Image |
|
| 35 |
+
| 4 | List |
|
| 36 |
+
| 5 | Page Number |
|
| 37 |
+
| 6 | Table |
|
| 38 |
+
| 7 | Text |
|
| 39 |
+
| 8 | Title |
|
| 40 |
+
|
| 41 |
+
## Usage
|
| 42 |
+
|
| 43 |
+
### Installation
|
| 44 |
+
|
| 45 |
+
Follow instructions at: https://detectron2.readthedocs.io/en/latest/tutorials/install.html
|
| 46 |
+
|
| 47 |
+
### Inference
|
| 48 |
+
|
| 49 |
+
import cv2
|
| 50 |
+
import layoutparser as lp
|
| 51 |
+
|
| 52 |
+
# Configuration
|
| 53 |
+
model_config_path = "config_mask_rcnn_resized.yaml"
|
| 54 |
+
model_path = "model_final_LP.pth"
|
| 55 |
+
label_map = {
|
| 56 |
+
0: "advertisement",
|
| 57 |
+
1: "author",
|
| 58 |
+
2: "header_or_footer",
|
| 59 |
+
3: "image",
|
| 60 |
+
4: "list",
|
| 61 |
+
5: "page_no",
|
| 62 |
+
6: "table",
|
| 63 |
+
7: "text",
|
| 64 |
+
8: "title",
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
# Load model
|
| 68 |
+
model = lp.models.Detectron2LayoutModel(
|
| 69 |
+
config_path=model_config_path,
|
| 70 |
+
model_path=model_path,
|
| 71 |
+
extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8],
|
| 72 |
+
label_map=label_map,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# Load and process image
|
| 76 |
+
image = cv2.imread("<path_to_image>")
|
| 77 |
+
image = image[..., ::-1] # BGR to RGB
|
| 78 |
+
|
| 79 |
+
# Detect layout
|
| 80 |
+
layout = model.detect(image)
|
| 81 |
+
|
| 82 |
+
# Print detected elements
|
| 83 |
+
for block in layout:
|
| 84 |
+
print(f"Type: {block.type}, Score: {block.score:.3f}, Box: {block.coordinates}")
|
| 85 |
+
|
| 86 |
+
# Visualize
|
| 87 |
+
import matplotlib.pyplot as plt
|
| 88 |
+
|
| 89 |
+
viz = lp.draw_box(image, layout, box_width=3, show_element_type=True)
|
| 90 |
+
plt.figure(figsize=(12, 16))
|
| 91 |
+
plt.imshow(viz)
|
| 92 |
+
plt.axis("off")
|
| 93 |
+
plt.show()
|