plot img and polygon

Plot an image and the quadrangles that enclose texts on the image

The data is from ICDAR2015

from PIL import Image

import matplotlib.pyplot as plt

import matplotlib.patches as patches

img_path = r'\ICDAR2015 - Text Localization Dataset\ch4_training_images\img_1.jpg'

gt_path = r'\ICDAR2015 - Text Localization Dataset\ch4_training_localization_transcription_gt\gt_img_1.txt'

f = open(gt_path, encoding='utf-8')

lines = f.readlines()

    

labels = []

vertices = []

for line in lines:

    vertices.append(list(map(int,line.rstrip('\n').lstrip('\ufeff').split(',')[:8])))

    label = 0 if '###' in line else 1

    labels.append(label)

polys = []

for p in vertices:

    points = []

    for i in range(0, 8, 2):

        points.append([p[i], p[i+1]])

    polys.append(points)

        

img = Image.open(img_path)

fig, ax = plt.subplots(1, figsize=(15,15))

ax.imshow(img)

for p in polys:

    polygon = patches.Polygon(p, edgecolor='r',facecolor='none')

    ax.add_patch(polygon)

    print(p)

plt.show()