Quantcast
Viewing all articles
Browse latest Browse all 14069

How to convert Polygon format to YOLO forma

POLYGON ((799 1776, 799 2016, 490 2016, 490 1776, 799 1776))This is the bounding box in POLYGONi want this in YOLO v5 format

import loggingfrom pathlib import Pathimport pandas as pdfrom shapely.wkt import loadsyolo_output_dir = Path("my_yolo")yolo_output_dir.mkdir(parents=True, exist_ok=True)df = (pd      .read_csv('images_bboxes.csv')      .fillna(value={'geometry': '2'}))worm_types = {    y: x for (x, y) in enumerate(df['worm_type'].unique())}logging.critical(worm_types)for (i, g) in df.groupby('image_id', sort=False):    dst = yolo_output_dir.joinpath(i).with_suffix('.txt')    logging.warning(dst)    with dst.open('w') as fp:        for i in g.itertuples(index=False):            if i.geometry:                worm = worm_types[i.worm_type]                geometry = loads(i.geometry)                (minx, miny, maxx, maxy) = geometry.bounds                (w, h) = (maxx - minx, maxy - miny)                print(worm, minx, miny, w, h, file=fp)

This is the code i tried,, but it is giving wrong coordinates..

POLYGON ((799 1776, 799 2016, 490 2016, 490 1776, 799 1776))is converted to0 389.0 1552.0 160.0 165.0which is wrong


Viewing all articles
Browse latest Browse all 14069

Trending Articles