"pascal voc to yolo" Code Answer's

You're definitely familiar with the best coding language Pascal that developers use to develop their projects and they get all their queries like "pascal voc to yolo" answered properly. Developers are finding an appropriate answer about pascal voc to yolo related to the Pascal coding language. By visiting this online portal developers get answers concerning Pascal codes question like pascal voc to yolo. Enter your desired code related query in the search bar and get every piece of information about Pascal code related question on pascal voc to yolo. 

pascal voc to yolo

By Xerothermic XenomorphXerothermic Xenomorph on Dec 30, 2020
import glob
import os
import pickle
import xml.etree.ElementTree as ET
from os import listdir, getcwd
from os.path import join

dirs = ['train', 'val']
classes = ['person', 'car']

def getImagesInDir(dir_path):
    image_list = []
    for filename in glob.glob(dir_path + '/*.jpg'):
        image_list.append(filename)

    return image_list

def convert(size, box):
    dw = 1./(size[0])
    dh = 1./(size[1])
    x = (box[0] + box[1])/2.0 - 1
    y = (box[2] + box[3])/2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)

def convert_annotation(dir_path, output_path, image_path):
    basename = os.path.basename(image_path)
    basename_no_ext = os.path.splitext(basename)[0]

    in_file = open(dir_path + '/' + basename_no_ext + '.xml')
    out_file = open(output_path + basename_no_ext + '.txt', 'w')
    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)

    for obj in root.iter('object'):
        difficult = obj.find('difficult').text
        cls = obj.find('name').text
        if cls not in classes or int(difficult)==1:
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
        bb = convert((w,h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')

cwd = getcwd()

for dir_path in dirs:
    full_dir_path = cwd + '/' + dir_path
    output_path = full_dir_path +'/yolo/'

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    image_paths = getImagesInDir(full_dir_path)
    list_file = open(full_dir_path + '.txt', 'w')

    for image_path in image_paths:
        list_file.write(image_path + '\n')
        convert_annotation(full_dir_path, output_path, image_path)
    list_file.close()

    print("Finished processing: " + dir_path)

Source: gist.github.com

Add Comment

1

All those coders who are working on the Pascal based application and are stuck on pascal voc to yolo can get a collection of related answers to their query. Programmers need to enter their query on pascal voc to yolo related to Pascal code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about pascal voc to yolo for the programmers working on Pascal code while coding their module. Coders are also allowed to rectify already present answers of pascal voc to yolo while working on the Pascal language code. Developers can add up suggestions if they deem fit any other answer relating to "pascal voc to yolo". Visit this developer's friendly online web community, CodeProZone, and get your queries like pascal voc to yolo resolved professionally and stay updated to the latest Pascal updates. 

Pascal answers related to "pascal voc to yolo"

View All Pascal queries

Pascal queries related to "pascal voc to yolo"

Browse Other Code Languages

CodeProZone