3

There have been already some similar style questions asked before (1, 2) However, none have mentioned the new Yolov5 style annotations.

Is there a simple function that takes in a normalized Yolov5 bounding box like:-

test = [0.436523 0.535156 0.587891 0.484375]

def some_function(test):
    ...
    return pascal_coords

And returns it in Pascal-VOC format?

I have tried plenty of online scripts - like https://dbuscombe-usgs.github.io/MLMONDAYS/blog/2020/08/17/blog-post and https://blog.roboflow.com/how-to-convert-annotations-from-pascal-voc-to-yolo-darknet/

But they aim for full dataset conversion including the xml, and some don't accept normalized boxes

This is the format:-

Yolov5 [<x-center> <y-center> <width> <height>]

|---> Converted to <-----|

Pascal VOC [x-top-left, y-top-left, x-bottom-right, y-bottom-right]

I simply want the converted bounding box :) TIA!

neel g
  • 1,138
  • 1
  • 11
  • 25

3 Answers3

3

After a bit of digging, I found this excellent little package hidden away under piles of google searches https://github.com/tensorturtle/rebox by @tensorturtle. Kudos to the author for providing such a useful and simple-to-use repository!

It provides a way to quickly convert bboxes as well as calculate basic ops like IOU. Really lovely work :)

neel g
  • 1,138
  • 1
  • 11
  • 25
2

There is no direct way to convert the normalized Yolo format to another format like Pascal VOC, because you need to know the size of the image to do the conversion. (Just like you need to know the size of the image to convert it to the normalized yolo format in the first place.) You will also want to provide some mapping to convert the class numbers to class names.

I am working on a Python package to simplify these kinds of conversions called PyLabel. I have a sample notebook that will convert Yolo v5 annotations to VOC format here https://github.com/pylabel-project/samples/blob/main/yolo2voc.ipynb. You can open it in Colab using this link.

The core code will be something like this:

from pylabel import importer
dataset = importer.ImportYoloV5(path=path_to_annotations, path_to_images=path_to_images)
dataset.export.ExportToVoc(output_path=output_path)

Hope that helps. Feel free to contact me if you have feedback or need assistance.

alexheat
  • 479
  • 5
  • 9
  • xmin,ymix,xmax,ymax are not stored as integers using this code. – curious_mind Jan 06 '22 at 01:31
  • Hi @curious_mind. Thanks for reporting that. I can probably make a quick fix for that. Will take a look and report back. – alexheat Jan 06 '22 at 05:43
  • @curious_mind. I fixed that issue and published a new release v0.1.25 that exports as int. Do pip install pylabel>=0.1.25 to make sure you get the latest version. I am curious. What framework are using that used VOC format? I would like to add it to my tests to make sure my output files are working with that framework. Thanks again for reporting it. – alexheat Jan 06 '22 at 06:41
1

Here is a code snippet that I used when I converted Yolo into Pascal_VOC. Yolo uses normalized coordinates so it is important to have the height and width of your image. Otherwise you can't calculate it back.

Here is my snippet:

# Convert Yolo bb to Pascal_voc bb
def yolo_to_pascal_voc(x_center, y_center, w, h,  image_w, image_h):
    w = w * image_w
    h = h * image_h
    x1 = ((2 * x_center * image_w) - w)/2
    y1 = ((2 * y_center * image_h) - h)/2
    x2 = x1 + w
    y2 = y1 + h
    return [x1, y1, x2, y2]

for any other object detection format conversion you can check my blog post on Medium: https://christianbernecker.medium.com/convert-bounding-boxes-from-coco-to-pascal-voc-to-yolo-and-back-660dc6178742

Have fun!