{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Create Yolo Labels\n\nThis is the poorly written and pretty inefficient code I used to quickly create files for YOLO. The code could definately be improved but oh well\n\n#### ONLY CREATING LABELS FOR THE ONES WITH CHARACTERS the ones without any labels will be skipped"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nfrom tqdm import tqdm_notebook as tqdm\n\nimport os\nprint(os.listdir('../input'))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Read the train set and unicode translations"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/train.csv')\ntranslations_df = pd.read_csv('../input/unicode_translation.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Drop the images with no labels from train_df"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = train_df.dropna()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def unicode_to_num(unicode):\n    '''Translates unicode ID to location from translations'''\n    return translations_df[translations_df['Unicode'] == unicode].index[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def label_to_yolo(label, dim):\n    '''Converts the given \"label\" to proper YOLO format by doing this:\n            - Replacing unicode ID with class\n            - Scaling coordinates/sizes to [0,1] \n    '''\n    height = dim[0]\n    width = dim[1]\n    _label = label.split()\n    for index in range(0, len(_label)):\n        if index % 5 == 0:\n            _label[index] = unicode_to_num(_label[index])\n        elif (index % 5 == 1) | (index % 5 == 3):\n            _label[index] = int(_label[index]) / width\n        elif (index % 5 == 2) | (index % 5 == 4):\n            _label[index] = int(_label[index]) / height\n\n    return(_label)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_img_dimensions(path):\n    '''Returns the image dimensions of an image_id'''\n    img = cv2.imread('../input/train_images/'+path+'.jpg')\n    return img.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if not os.path.exists('yolo_train_labels'):\n    os.mkdir('yolo_train_labels')\ndef yolo_to_txt(yolo, file_name):\n    '''Writes given YOLO label to a text file'''\n    file = open('yolo_train_labels/'+file_name+'.txt', \"w\")\n    for index in range(0, len(yolo), 5):\n        to_write = ' '.join(str(x) for x in yolo[index:index+5])\n        file.write(to_write + '\\n')\n    file.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def write_row(image_id, labels):\n    img_shape = get_img_dimensions(image_id)\n    if type(labels) == str:\n        yolo = label_to_yolo(labels, dim=img_shape)\n        yolo_to_txt(yolo, image_id)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for index, row in tqdm(train_df.iterrows(), total=train_df.shape[0]):\n    write_row(row['image_id'], row['labels'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now to archive the labels in a zip"},{"metadata":{"trusted":true},"cell_type":"code","source":"import shutil\nshutil.make_archive('yolo_labels', 'zip', 'yolo_train_labels')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir('.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm -rf yolo_train_labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir('.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_names = open('classes.names', 'w')\nfor index, row in tqdm(translations_df.iterrows(), total=translations_df.shape[0]):\n    class_names.write(row['Unicode']+'\\n')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}