Skip to content

Instantly share code, notes, and snippets.

@patharanordev
Last active April 18, 2021 05:03
Show Gist options
  • Save patharanordev/625b0f525164748af49dbcbe2730af19 to your computer and use it in GitHub Desktop.
Save patharanordev/625b0f525164748af49dbcbe2730af19 to your computer and use it in GitHub Desktop.

Crop image from YOLOv4+'s label(Darknet annotation)

def get_yolo_image_box(line, img_width, img_height):
    '''
    Ref. https://www.ccoderun.ca/programming/darknet_faq/#darknet_annotations
    '''
    columns = line.split(' ')
    class_id = columns[0]
    center_x = float(columns[1]) * img_width
    center_y = float(columns[2]) * img_height
    obj_half_width = float(columns[3]) * img_width / 2
    obj_half_height = float(columns[4]) * img_height / 2
    x_min = int(center_x - obj_half_width)
    y_min = int(center_y - obj_half_height)
    x_max = int(center_x + obj_half_width)
    y_max = int(center_y + obj_half_height)
    return class_id, x_min, y_min, x_max, y_max
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment