<p>There are many types of tables in the document images with too much variations and layouts. No matter how many rules you write, there will always appear a table for which your rules will fail. These types of problems are genrally solved using ML(Machine Learning) based solutions. You can find many pre-implemented codes on github for solving the problem of detecting tables in the images using ML or DL (Deep Learning).</p>
<p>Here is my code along with the deep learning models, the model can detect various types of tables as well as the structure cells from the tables: <a href="https://github.com/DevashishPrasad/CascadeTabNet" rel="noreferrer">https://github.com/DevashishPrasad/CascadeTabNet</a></p>
<p>The approach achieves state of the art on various public datasets right now (10th May 2020) as far as the accuracy is concerned</p>
<p>More details : <a href="https://arxiv.org/abs/2004.12629" rel="noreferrer">https://arxiv.org/abs/2004.12629</a></p>
How to find table like structure in image
<p>I have different type of invoice files, I want to find table in each invoice file. In this table position is not constant. So I go for image processing. First I tried to convert my invoice into image, then I found contour based on table borders, Finally I can catch table position. 
For the task I used below code.</p>

<pre><code>with Image(page) as page_image:
    page_image.alpha_channel = False #eliminates transperancy
    img_buffer=np.asarray(bytearray(page_image.make_blob()), dtype=np.uint8)
    img = cv2.imdecode(img_buffer, cv2.IMREAD_UNCHANGED)

    ret, thresh = cv2.threshold(img, 127, 255, 0)
    im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    margin=[]
    for contour in contours:
        # get rectangle bounding contour
        [x, y, w, h] = cv2.boundingRect(contour)
        # Don't plot small false positives that aren't text
        if (w &gt;thresh1 and h&gt; thresh2):
                margin.append([x, y, x + w, y + h])
    #data cleanup on margin to extract required position values.
</code></pre>

<p>In this code <code>thresh1</code>, <code>thresh2</code> i'll update based on the file.</p>

<p>So using this code I can successfully read positions of tables in images, using this position i'll work on my invoice pdf file. For example </p>

<p>Sample 1:</p>

<p><a href="https://i.stack.imgur.com/NJXjD.png" rel="noreferrer"><img src="https://i.stack.imgur.com/NJXjD.png" alt="enter image description here"></a></p>

<p>Sample 2:</p>

<p><a href="https://i.stack.imgur.com/Ntbce.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Ntbce.png" alt="enter image description here"></a></p>

<p>Sample 3:
<a href="https://i.stack.imgur.com/SqpiK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/SqpiK.png" alt="enter image description here"></a></p>

<p>Output:</p>

<p>Sample 1:</p>

<p><a href="https://i.stack.imgur.com/MIaGH.png" rel="noreferrer"><img src="https://i.stack.imgur.com/MIaGH.png" alt="enter image description here"></a></p>

<p>Sample 2:</p>

<p><a href="https://i.stack.imgur.com/CXv8x.png" rel="noreferrer"><img src="https://i.stack.imgur.com/CXv8x.png" alt="enter image description here"></a></p>

<p>Sample 3:</p>

<p><a href="https://i.stack.imgur.com/d76gQ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/d76gQ.png" alt="enter image description here"></a></p>

<p>But, now I have a new format which doesn't have any borders but it's a table. How to solve this? Because my entire operation depends only on borders of the tables. But now I don't have a table borders. How can I achieve this? I don't have any idea to move out from this problem. My question is, Is there any way to find position based on table structure?. </p>

<p>For example My problem input looks like below:</p>

<p><a href="https://i.stack.imgur.com/Fw3Qa.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/Fw3Qa.jpg" alt="enter image description here"></a></p>

<p>I would like to find its position like below:
<a href="https://i.stack.imgur.com/5or8D.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5or8D.png" alt="enter image description here"></a></p>

<p>How can I solve this?
It is really appreciable to give me an idea to solve the problem.</p>

<p>Thanks in advance.</p>