ValueError: All bounding boxes should have positive height and width. Found invalid box

Lane: <p>Hi, I’m working with Mask RCNN neural network and with PyTorch I need to load dataset, and I followed this tutorial: <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">TorchVision Object Detection Finetuning Tutorial — PyTorch Tutorials 2.2.0+cu121 documentation</a> I have coordinates of polygon bounding box of every word on image wrote in json file. So now, I don’t know how to compute rectangle bounding box of that polygon coordinates.<br>
I’m using <code>train_labels.json</code> from ArT (ICDAR2019) dataset</p>
<pre><code class="lang-auto">f = open('train_labels.json')
data = json.load(f)
boxes = []
        for i in data.keys():
            for j in data[i]:
                mask = j['points'] # --&gt; polygonBB coordinates
                obj_ids = np.unique(mask)
                obj_ids = obj_ids[1:]

                num_objs = len(obj_ids)
</code></pre>
<p>Variable <code>mask</code> has polygon bb coordinates.</p>
<p>When I use:</p>
<pre><code class="lang-auto">left, right = min(mask, key=lambda p: p[0]), max(mask, key=lambda p: p[0])
 top, bottom = min(mask, key=lambda p: p[1]), max(mask, key=lambda p: p[1])    

 boxes.append([left[0],top[1],right[0],bottom[1]])
</code></pre>
<p>then the output is<br>
<code>ValueError: All bounding boxes should have positive height and width. Found invalid box [24.994787216186523, 472.875732421875, 46.65693664550781, 472.875732421875] for target at index 0.</code></p>
<p>For some reason, the second and the last coordinate is the same but why?</p>
<p>But if I use the code from PyTorch tutorial:</p>
<pre><code class="lang-auto">for z in range(num_objs):
                    pos = np.where(masks[z])
                    xmin = np.min(pos[1])
                    xmax = np.max(pos[1])
                    ymin = np.min(pos[0])
                    ymax = np.max(pos[0])
                    #print(xmin, xmax,ymin,ymax)
                    boxes.append([xmin, ymin, xmax, ymax])

boxes = torch.as_tensor(boxes, dtype=torch.float32)
target = {}
        target["boxes"] = boxes
</code></pre>
<p>Output is:<br>
<code>ValueError: All bounding boxes should have positive height and width. Found invalid box [0.0, 0.0, 0.0, 0.0] for target at index 0.</code></p>
<p>My annotation for word “red” is:</p>
<pre><code class="lang-auto"> {
                "transcription": "red",
                "language": "Latin",
                "illegibility": false,
                "points": [
                    [
                        21.51514273595292,
                        96.10111281287746
                    ],
                    [
                        34.41549485283264,
                        97.283756254127
                    ],
                    [
                        36.42017833821299,
                        98.51023290725634
                    ],
                    [
                        48.266909960703956,
                        99.59706074137782
                    ],
                    [
                        51.456108522323206,
                        92.58403315228024
                    ],
                    [
                        63.3249196091766,
                        93.6674491203431
                    ],
                    [
                        60.960257769931985,
                        110.16062344395255
                    ],
                    [
                        49.094275907517556,
                        109.06503983039966
                    ],
                    [
                        46.79115225196064,
                        109.89584392001012
                    ],
                    [
                        34.94618448639528,
                        108.80142524223687
                    ],
                    [
                        32.794444829218214,
                        108.60261473324032
                    ],
                    [
                        19.896204045991865,
                        107.41087863881067
                    ],
                    [
                        21.51514273595292,
                        96.10111281287746
                    ]
                ]
            }
</code></pre>
<p>This is just one word of I don’t know how many, I have 80k images with random generated words. I’m also sending a image with some other text but all text instances are equal annotated.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/a/caf7df3e45092991966c304d379f767f2bf8f531.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/caf7df3e45092991966c304d379f767f2bf8f531" title="moze4.PNG"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/a/caf7df3e45092991966c304d379f767f2bf8f531.jpeg" alt="moze4.PNG" data-base62-sha1="sXxvLNJP6u5jU4grl9pjkTkq7Fn" width="667" height="500" data-dominant-color="7E513F"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">moze4.PNG</span><span class="informations">985×738 83.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Also I have and this code and the output is:</p>
<pre><code class="lang-auto">if len(mask) == 0:
                raise ValueError("Can't compute bounding box of empty list")
                minx, miny = float("inf"), float("inf")
                maxx, maxy = float("-inf"), float("-inf")
                for x, y in mask:
                    # Set min coords
                    if x &lt;minx:
                        minx = x
                    if y &lt;miny:
                        miny = y
                    # Set max coords
                    if x &gt;maxx:
                        maxx = x
                    elif y &gt; maxy:
                        maxy = y
               boxes.append([minx,miny,maxx,maxy])
</code></pre>
<p><code>ValueError: All bounding boxes should have positive height and width. Found invalid box [594.2897338867188, 533.2000122070312, 366.35723876953125, 468.6328125] for target at index 0.</code></p>