Views
No views yet
| ID | Label |
|---|---|
| 0 | Advertisement |
| 1 | Author |
| 2 | Header or Footer |
| 3 | Image |
| 4 | List |
| 5 | Page Number |
| 6 | Table |
| 7 | Text |
| 8 | Title |
| AP | AP50 | AP75 | APs | APm | APl |
|---|---|---|---|---|---|
| 64.325 | 88.948 | 69.214 | 40.350 | 55.117 | 67.543 |
1import cv2
2import layoutparser as lp
3import matplotlib.pyplot as plt
4
5# Configuration
6model_config_path = "config_mask_rcnn_resized.yaml"
7model_path = "SweMPer-layout-lite.pth"
8
9label_map = {
10 0: "advertisement",
11 1: "author",
12 2: "header_or_footer",
13 3: "image",
14 4: "list",
15 5: "page_no",
16 6: "table",
17 7: "text",
18 8: "title",
19}
20
21# Load model
22model = lp.models.Detectron2LayoutModel(
23 config_path=model_config_path,
24 model_path=model_path,
25 extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8],
26 label_map=label_map,
27)
28
29# Load and process image
30image = cv2.imread("<path_to_image>")
31image = image[..., ::-1] # BGR to RGB
32
33# Detect layout
34layout = model.detect(image)
35
36# Print detected elements
37for block in layout:
38 print(f"Type: {block.type}, Score: {block.score:.3f}, Box: {block.coordinates}")
39
40# Visualize results
41viz = lp.draw_box(image, layout, box_width=3, show_element_type=True)
42plt.figure(figsize=(12, 16))
43plt.imshow(viz)
44plt.axis("off")
45plt.show()