atlanhq / camelot

Camelot: PDF Table Extraction for Humans
https://camelot-py.readthedocs.io
Other
3.66k stars 360 forks source link

How to convert x1,y1,x2,y2 to x1,y1, w, h? #421

Open gabrieldevopsai opened 4 years ago

gabrieldevopsai commented 4 years ago

I need to know how to convert x1,y1,x2,y2 to x1,y1,w,h...

kushalj001 commented 4 years ago

You first need to get the dimensions of your PDF document's page. This can be done as follows:

from camelot.utils import get_page_layout
 _, dim = get_page_layout(pdf_path)

Then you need to make sure that your original co-ordinates are of the same scale as that of your PDF doc given by camelot. If not its generally some multiple of those returned by the function above. Get the resize factor and rescale your co-ordinates to that of camelot scale. Then subtract your y-coordinates from the page height. I wrote this helper function for my use-case where I get the co-ordinates from bounding boxes from the XML doc generated by the LabelImg tool. Might help you.

def convert_to_pdfspace(pdf_path, xml_data, xml_height):
    '''
    Converts labelled template space to PDF space.
    '''
    _, dim = get_page_layout(pdf_path)
    pdf_width, pdf_height = round(dim[0]), round(dim[1])
    resize_factor = xml_height / pdf_height
    for k,v in xml_data.items():
        vstr = ''
        for i in range(len(v)):
            v[i] = round(v[i]/resize_factor)

        v[1] = pdf_height - v[1]
        v[3] = pdf_height - v[3]
        vstr += str(v[0]) +','+ str(v[1]) +','+ str(v[2]) +','+str(v[3])
        xml_data[k] = vstr

    return xml_data
AnandKumar1989 commented 3 years ago

def xyxyToxywh(x):

Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] , input x will be [x1, y1, x2, y2]

y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
y[:, 0] = (x[:, 0] + x[:, 2]) / 2  # x center
y[:, 1] = (x[:, 1] + x[:, 3]) / 2  # y center
y[:, 2] = x[:, 2] - x[:, 0]  # width
y[:, 3] = x[:, 3] - x[:, 1]  # height
return y # output will be [x, y, w, h]