<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ria on Medium]]></title>
        <description><![CDATA[Stories by Ria on Medium]]></description>
        <link>https://medium.com/@riasharma.indian?source=rss-243ddd4b6f1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*HRvKtzkCFJ1Urrok</url>
            <title>Stories by Ria on Medium</title>
            <link>https://medium.com/@riasharma.indian?source=rss-243ddd4b6f1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 20:04:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@riasharma.indian/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Efficient Bounding Box Sorting and Automated Bounding Box Intersection Adjustment]]></title>
            <link>https://medium.com/@riasharma.indian/efficient-bounding-box-sorting-and-automated-bounding-box-intersection-adjustment-952eeab86e6?source=rss-243ddd4b6f1------2</link>
            <guid isPermaLink="false">https://medium.com/p/952eeab86e6</guid>
            <category><![CDATA[iou]]></category>
            <category><![CDATA[object-detection]]></category>
            <category><![CDATA[bounding-box]]></category>
            <category><![CDATA[sorting-algorithms]]></category>
            <category><![CDATA[intersect]]></category>
            <dc:creator><![CDATA[Ria]]></dc:creator>
            <pubDate>Fri, 31 Mar 2023 17:44:09 GMT</pubDate>
            <atom:updated>2023-04-28T05:41:22.750Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’re working on an object detection project, you’re probably familiar with bounding boxes. Bounding boxes are rectangular boxes that surround objects in an image and indicate their location. They are a crucial part of object detection algorithms, and their accuracy can significantly affect the performance of the algorithm.</p><p>However, sometimes the bounding boxes generated by the algorithm may not be accurate enough. For example, two boxes may overlap or intersect, or some boxes may be too small or too large. In such cases, we need to adjust and sort the bounding boxes to improve their accuracy.</p><p>In this blog post, we will discuss how to adjust and sort bounding boxes using Python and the OpenCV and Pandas libraries.</p><p>In the above image, you can clearly see that some boxes slightly intersect each other, and some more than 90% overlap each other also not in sorting.</p><p>First, let’s load an image:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1df782c21f6b85ea5a6c33895ff9b3a/href">https://medium.com/media/c1df782c21f6b85ea5a6c33895ff9b3a/href</a></iframe><p>Next, we convert the bounding boxes to the [x, y, w, h] format and create a Pandas DataFrame from them:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5a84ad47d1ba6b998f621894a2b1dea/href">https://medium.com/media/e5a84ad47d1ba6b998f621894a2b1dea/href</a></iframe><p>We then sort the bounding boxes by their x-coordinate, y-coordinate, and x2-coordinate:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49f7b8db62eb888c8bcace8884ed7aec/href">https://medium.com/media/49f7b8db62eb888c8bcace8884ed7aec/href</a></iframe><p>Now have to reorder a set of bounding boxes based on their positions using Python and pandas.</p><p>The code snippet below demonstrates how to perform this task using a nested loop structure:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/325cc70370ebe9ec225a5d575fc55f81/href">https://medium.com/media/325cc70370ebe9ec225a5d575fc55f81/href</a></iframe><p>The outer loop iterates four times, which suggests that there are four bounding boxes to be organized. The inner loop iterates over the pandas dataframe that contains the bounding boxes.</p><p>The condition inside the inner loop checks if the position of the bounding box at the current index is greater than the position of the bounding box at the next index. If this condition is true, it means that the two bounding boxes are out of order, so the code swaps their positions.</p><p>The swap is performed using the pandas “.iloc” method, which accesses the data in the dataframe by row and column indexes. The “.copy()” method creates a copy of the row so that the original data is not lost during the swap.</p><p>By the end of the code, the data frame will contain the bounding boxes organized in the desired order. This can be useful for various applications, such as visualizing the objects in a certain sequence or performing further analysis based on their positions.</p><p>Now, we need to adjust the bounding boxes to remove any overlaps or intersections. To achieve this we need the original format of each box to be [x1, y1, x2, y2]</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd422b6deb3c9e7777394bb28b1b41c9/href">https://medium.com/media/dd422b6deb3c9e7777394bb28b1b41c9/href</a></iframe><p>We define a function adjust_boxes() that checks if two boxes intersect, and another function intersect() that adjusts the boxes to remove any overlaps or intersections:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5959b304a78f0733f0a4940a5c8a3af/href">https://medium.com/media/c5959b304a78f0733f0a4940a5c8a3af/href</a></iframe><p>The given code adjust_boxes takes in a list of bounding box coordinates (boxes) and returns a modified list of bounding box coordinates after performing some adjustments. The adjustments aim to resolve any overlapping boxes and make them fit better around their respective objects.</p><p>The function first defines another function named intersect which checks whether two bounding boxes intersect or not. It does this by checking if the rightmost point of the first box is to the right of the leftmost point of the second box, the leftmost point of the first box is to the left of the rightmost point of the second box, the bottom point of the first box is below the topmost point of the second box, and the topmost point of the first box is above the bottom point of the second box. If all these conditions are true, the function returns True, indicating that the boxes intersect.</p><p>The main function adjust_boxes then loops through each pair of boxes in the boxes list using a nested for loop. For each pair, it checks if the boxes intersect using the intersect function defined earlier. If the boxes intersect, it calculates the overlap between the two boxes in terms of their width (dx) and height (dy).</p><p>It then calculates the Intersection over Union (IoU) of the first box with the second box (iou1) and the second box with the first box (iou2). IoU is a measure of the extent of overlap between two boxes and is defined as the ratio of the area of their intersection to the area of their union.</p><p>Depending on the values of iou1 and iou2, the function either adjusts one of the boxes to resolve the overlap or merges the two boxes into a single box. If both iou1 and iou2 are greater than or equal to 0.8, it merges the two boxes into a single box. If iou1 is less than 0.8, it adjusts the first box to reduce its overlap with the second box. If iou2 is less than 0.8, it adjusts the second box to reduce its overlap with the first box.</p><p>If the function adjusts a box, it determines whether to adjust the width or the height of the box based on whether the overlap in width (dx) or height (dy) is greater. It then adjusts the corresponding dimension by half the overlap. If the function merges two boxes, it creates a new box that encompasses both the original boxes and removes the second box from the list.</p><p>Finally, the function returns the modified boxes list.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4f8eaab3412be19f77dace24409262d/href">https://medium.com/media/f4f8eaab3412be19f77dace24409262d/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=952eeab86e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Training Tesseract-OCR with Custom Data and Font]]></title>
            <link>https://medium.com/@riasharma.indian/training-tesseract-ocr-with-custom-data-and-font-cf9e94429b4a?source=rss-243ddd4b6f1------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf9e94429b4a</guid>
            <dc:creator><![CDATA[Ria]]></dc:creator>
            <pubDate>Sun, 06 Nov 2022 13:02:29 GMT</pubDate>
            <atom:updated>2022-11-06T13:02:29.804Z</atom:updated>
            <content:encoded><![CDATA[<p>While trying to develop an OCR project for low-resolution images, I realized the shortcomings of the pre-trained tesseract models. For this reason, I decided to train it using my own data. Unfortunately, resources on how to do this on the internet are few and far between. This is a detailed guide on how to set up the image files and train a custom tesseract model.</p><h4>Steps involved:</h4><ul><li>Preparing Dataset</li><li>Preparing Box files.</li><li>Annotating Box files.</li><li>Training Tesseract.</li><li>Testing Model</li></ul><h4><strong>Preparing Dataset:</strong></h4><p>In this article we will generate data for learning purpose only. so first of all we need to crate a .txt file in which have to put all the chars which we want to train. Have a look below….</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*JG24HAi4ObCnt-vqe3PE5Q.png" /><figcaption>cursive.txt</figcaption></figure><h4>Preparing Box files:</h4><p>We can use a program like <a href="https://github.com/nguyenq/jTessBoxEditor/releases/tag/Release-2.3.1">jTessBoxEditor</a> for box generation, editor and training.</p><blockquote>NOTE: Download <a href="https://www.java.com/download/ie_manual.jsp">java </a>and install and then restart your PC.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*Q8EtLct1VIXqgDdP1BL-UQ.png" /><figcaption>jTessBoxEditor</figcaption></figure><p>Now open jTessBoxEditor.jar</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ir_3woeGWO0XITTLWT7vtQ.png" /></figure><ol><li>Click on Box Generator</li><li>Choose the .txt file by input box</li><li>Select the output folder</li><li>Select the font and size</li><li>Give te name to your model</li><li>Final generate</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iScf9rQLksWid9CEUXBfng.png" /></figure><p>Go to Box editor select your .tif file. we can see that tesseract generate box files for the text in the images. Tesseract will try its best to draw boxes around the characters and to identify them. This obviously won’t be 100% accurate because otherwise, we wouldn’t need to train tesseract. This will produce a list of box files for each image file with the file format.</p><p><strong><em>[language name].[font name].exp[number].box</em></strong></p><p>These box files will contain boxes around the characters in the image and what each character is. Now comes the manual work of annotating the box files. Check the bounding boxes that tesseract has made. We can correct the boxes and the character in them. We can also add new boxes around characters that tesseract has missed.</p><p>This is the most important task as any mistakes will reduce the accuracy of the trained models.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JBw2DUO0iCSUD7lhzMt-Xw.png" /></figure><p>Save and go to the Trainer tab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gbsGDAz0R0sdbIvoXKBI3Q.png" /></figure><p>Check the training data path, language name and choose the option Training with existing boxes.</p><blockquote>NOTE: A box editor and trainer for Tesseract OCR, providing editing of box data of both Tesseract 2.0x and 3.0x formats and full automation of Tesseract training.</blockquote><blockquote>*LSTM Training for Tesseract 4.0x is not supported.</blockquote><p>Now you can run and train.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/308/1*Ir1IkDZu9l2tdzcpAXCmVQ.png" /></figure><p>In tessdata folder you can see .trainingdata file.</p><p>For testing you can paste this file to below location and use in your code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*V5J48zxHVUNoCmsU7E_7vA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*ma6yNN6Hi2DFxL9hy-nR9w.png" /><figcaption>test.png</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*pni0j-8JqZ4XFrOcwAZ8Qg.png" /><figcaption>Output</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf9e94429b4a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>