Views
No views yet
1git clone https://github.com/vikp/surya.git
2cd suryagit checkout f7c6c04pip install -r requirements.txtfrom surya.input.processing import prepare_image_detectionfrom surya.model.detection.segformer import load_processor1from PIL import Image
2image = Image.open("path/to/image")
3images = [prepare_image_detection(img=image, processor=load_processor())]images = torch.stack(images, dim=0).to(model.dtype).to(torch.float32).to(device)prepare_image_detection function, the image is converted into the shape : [1 ,12 , 300 , 300 ] (This is the format(shape) that is expected by Surya Layout Model)1from surya.model.detection.segformer import load_model
2device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model = load_model("ketanmore/surya-ocr-arabic-layout") output = model(images).to(device).to(torch.float32)print(model.config.id2label) # This snippet can be used to print classes(labels) used for segmentaion1{0: 'Blank',
2 1: 'Caption',
3 2: 'Footnote',
4 3: 'Formula',
5 4: 'List-item',
6 5: 'Page-footer',
7 6: 'Page-header',
8 7: 'Picture',
9 8: 'Section-header',
10 9: 'Table',
11 10: 'Text',
12 11: 'Title'}1import torch
2import matplotlib.colors as mcolors
3import matplotlib.pyplot as plt1probabilities = torch.softmax(outputs.logits, dim=1)
2mask = torch.argmax(probabilities, dim=1).squeeze(0)
3mask = mask.cpu().numpy()1class_labels = {
2 0: 'Blank',
3 1: 'Caption',
4 2: 'Footnote',
5 3: 'Formula',
6 4: 'List-item',
7 5: 'Page-footer',
8 6: 'Page-header',
9 7: 'Picture',
10 8: 'Section-header',
11 9: 'Table',
12 10: 'Text',
13 11: 'Title'
14}
15
16# Create a color map, one unique color for each class
17colors = plt.cm.get_cmap('tab20', len(class_labels))1
2def plot_segmentation_mask(mask, class_labels, colors):
3 fig, ax = plt.subplots(figsize=(10, 10))
4 # Display the image
5 ax.imshow(mask, cmap=colors, interpolation='nearest')
6
7 # Add class labels
8 unique_classes = np.unique(mask)
9 for cls in unique_classes:
10 # Find the first occurrence of this class in the mask
11 indices = np.argwhere(mask == cls)
12 if indices.size > 0:
13 representative_index = indices[len(indices)//2] # Pick middle index as representative
14 ax.text(representative_index[1], representative_index[0], class_labels[cls],
15 verticalalignment='center', horizontalalignment='center',
16 color='white', fontsize=12, weight='bold')
17
18 plt.axis('off')
19 plt.show()
20
21# Call the function with the mask, labels, and colors
22plot_segmentation_mask(mask, class_labels, colors)1from surya.layout import parallel_get_regions
2
3def logits_to_bboxes(logits,image) :
4 correct_shape = (300, 300)
5 logits_temp = F.interpolate(logits, size=correct_shape, mode='bilinear', align_corners=False)
6 logits_temp = logits_temp.cpu().detach().numpy().astype(np.float32)
7
8 heatmap_count = logits_temp.shape[1]
9 heatmaps = [logits_temp[i][k] for i in range(logits_temp.shape[0]) for k in range(heatmap_count)]
10 regions = parallel_get_regions(heatmaps=heatmaps, orig_size=image.size, id2label=model.config.id2label)
11
12 final_bboxes = []
13 for i in regions.bboxes :
14 final_bboxes.append(i.bbox)
15 return final_bboxes
16
17
18bb = logits_to_bboxes(outputs.logits,image)
19print(bb)
20
21# Sample Output
22[[41.0, 293.0, 345.0, 517.0],
23 [39.0, 141.0, 343.0, 320.0],
24 [332.0, 136.0, 528.0, 405.0],
25 [332.0, 367.0, 528.0, 714.0],
26 [126.0, 62.0, 437.0, 95.0],
27 [218.0, 106.0, 343.0, 138.0]]1from surya.postprocessing.heatmap import draw_bboxes_on_image
2from PIL import Image
3
4img = draw_bboxes_on_image(bboxes,Image.open(image_path))