Transforming bounding box from object detection ML to shapefile

I'm working on a Python script to build a point shapefile I can open on QGIS from a list of bounding box (X and Y coordinates) returned by an Object Detection algo.

I know the CRS/EPSG and bounds of the TIFF file I ran the prediction on.

However I'm not sure how to handle the list of X and Y Bounding box coordinates: should I convert each coordinates into the original CRS while creating the point shape object ?

Or I just need to append each object with it's X and Y coordinate and save the shapefile with the correct bounds and CRS?

• Are the bounding box coordinates in different coordinate systems? i.e. TIFF 'A' is in EPSG 4326 and TIFF 'B' is in 3785? If so then you will need to standardise them all before putting into a single shapefile. If they are the same you should be okay to append each.
– EnE_
Jul 9 '20 at 4:08
• there is only 1 TIFF file I convert in JPG to run the prediction At the end of my process, I have a list of N bounding box coordinates in X and Y. I say X and Y because they are relative to the size of the full tiff in pixel My question concerns the many polygon I want to create : should I convert each polygon points (4 * (x , y )) for a rectangle into the EPSG of my original TIFF, or I can just copy the x and y and update the shapefile bounds and EPSG and it will work like that ?
– Gil
Jul 9 '20 at 16:01

I had a similar problem. I did instance segmentation and then I should have restored the coordinates of the completed mask for GIS.
I converted the points of each mask to original coordinates and created a polygon using shapely. I think you just need to convert each point in your bbox to original coordinates and create polygons.
I used rasterio, and I think you can take this too.

1. Converts the result of each object detection you cut into an integrated pixel coordinate.
2. Rasterio calls the original TIFF file.
3. You can get the coordinate values for the pixel coordinates of the original file.

For example.

import rasterio

#you can get x, y coords from original TIFF cell coords.
origin=rasterio.open("STH.TIFF")

#you make bbox list or xy pixel coods list sth..
bbox_list = [[xmax,xmin,ymax,ymin],....]

#get coords from pixel coords.
origin.xy(1, 100)

#return coords based on original TIFF crs.
(340578.786, 4155398.1545)

#all pixel coords convert to original coords.
for bbox in bbox_list:
origin.xy(bbox, bbox)
.....
• sorry i still not understand, why origin.xy(1,100) ? how to know (1,100)? Aug 24 '21 at 2:20
• @ichsan It's just an example. The result of object detection is usually obtained by converting the origin to 0,0. If your AI result is a triangle of (1,1)(2,2)(10,10), to know the coordinates of this shape from tiff, origin.xy(1,1), origin.xy(2,2), origin.xy(10,10). Rather than that, there is no need to use rasterio.xy. You can also calculate the coordinates based on the origin of the tiff and the CRS. (orgin_x + coords of AI * CRS unit, origin_y - coords of AI * CRS unit). That's more convenient. Aug 24 '21 at 5:19

I am also doing object detection where I am detecting several tree species in a locality. I need to convert the output boxes for each image into shapefile. Could anyone provide me with a bit of guidance on how to do? The object detection model does not contains the CRS data as I had to convert my .tiff to .png file and the coordinate was lost. Thank you.

• If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review Sep 12 '21 at 4:35