text_html_output_1.png text_plain_output_1.png
from PIL import Image, ImageDraw, ImageFont

import pandas as pd

df1 = pd.read_csv('../input/newspaper-articles-csv-ocr/ocr/df_valid.csv')
df1['imgdir'] = '../input/newspaper-articles/articles/validation'
df2 = pd.read_csv('../input/newspaper-articles-csv-ocr/ocr/df_train1.csv')
df2['imgdir'] = '../input/newspaper-articles/articles/training'
df3 = pd.read_csv('../input/newspaper-articles-csv-ocr/ocr/df_train2.csv')
df3['imgdir'] = '../input/newspaper-articles/articles/training'
df4 = pd.read_csv('../input/newspaper-articles-csv-ocr/ocr/df_train3.csv')
df4['imgdir'] = '../input/newspaper-articles/articles/training'
df = pd.concat([df1, df2, df3, df4])

df = df[df.text.apply(lambda x: len(str(x)) > 1)]
df = df.reset_index(drop=True)

df.groupby(['label']).size()

label2color = {'title': 'Red', 'text': 'Green'}
def Draw_BBox(image_path, _bbox_labeled: pd.DataFrame):
    actual_boxes = []
    for idx, row in _bbox_labeled.iterrows():
        x0 = row['left']
        y0 = row['top']
        x1 = row['width'] + row['left']
        y1 = row['height'] + row['top']
        label = row['label']
        conf = row['conf']
        color = label2color[label]
        actual_box = [x0, y0, x1, y1]
        draw = ImageDraw.Draw(image, 'RGB')
        draw.rectangle(actual_box, outline=color, width=1)
        draw.text((actual_box[0] + 10, actual_box[1] - 10), text=f'{label}', fill=color)
    return image
sample_filename = df['image_id'].iloc[0]
sample_dir = df['imgdir'].iloc[0]
from PIL import Image, ImageDraw, ImageFont
image = Image.open(f'{sample_dir}/{sample_filename}')
image = image.convert('RGB').resize((1000, 1000))
sample_image_df = df[df['image_id'] == sample_filename]
sample_image_df = sample_image_df
Draw_BBox(image, sample_image_df)