Last active
September 14, 2018 11:01
-
-
Save galaktyk/8fbf80c2c66fcbde8e641c2d5f9bf939 to your computer and use it in GitHub Desktop.
create yolo annotations from downloaded images ()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import os | |
import csv | |
import numpy as np | |
import shutil | |
import pandas as pd | |
## EDIT HERE | |
# Desir classes(from class-descriptions-boxable.csv) and class number in your classes list(classes.txt) | |
classes = {'/m/04yx4':0,'/m/01bl7v':0,'/m/03bt1vf':1,'/m/05r655':1,'/m/01c648':2,'/m/050k8':3,'/m/0bh9flk':4} | |
# Box Optional Arguments 1 or 0 or 'any' | |
tags = {'IsOccluded':'any','IsTruncated':'any','IsGroupOf':0,'IsDepiction':0,'IsInside':'any'} | |
tags = list(tags.values()) | |
# images path | |
images_path = glob.iglob('train/**/*.jpg', recursive=True) | |
images_name = [os.path.basename(x).replace('.jpg','') for x in images_path] | |
chunksize = 10**7 | |
####################################################################### | |
if os.path.exists('train/annotations'): | |
shutil.rmtree('train/annotations') | |
os.makedirs('train/annotations') | |
print('[ INFO ] Number of images : '+str(len(images_name))) | |
for reader in pd.read_csv('../csv_folder/train-annotations-bbox.csv', chunksize=chunksize): | |
reader=reader.values | |
for i in range(0,reader.shape[0]): | |
if (reader[i][2] in classes) and all([ True if b=='any' else a==b for a,b in zip(reader[i][8:13],tags) ]) and (reader[i][0] in images_name): | |
print('Matched : '+reader[i][0]) | |
wi = float(reader[i][5]) - float(reader[i][4]) | |
hi = float(reader[i][7]) - float(reader[i][6]) | |
x_cent = float(reader[i][4]) + wi/2 | |
y_cent = float(reader[i][6]) + hi/2 | |
with open('train/annotations/'+reader[i][0]+'.txt', 'a') as txt_file: | |
txt_file.write(str(classes[reader[i][2]])+' '+str(x_cent)+' '+str(y_cent)+' '+str(wi)+' '+str(hi)) | |
txt_file.write("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
now read .csv in chunk, so it wont crash my potato PC