{"cells":[{"metadata":{},"cell_type":"markdown","source":"# This is a simplified version of pablOberhauser's notebook 'Creating Label Files for use in YOLOv4'","execution_count":null},{"metadata":{"id":"08ATK1f8P0yB","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport os\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"id":"M97dz7ZRRF7s","trusted":true},"cell_type":"code","source":"train=pd.read_csv(\"../input/global-wheat-detection/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"id":"Tu2wvGBST28T","trusted":true},"cell_type":"code","source":"def convert_to_yolo(bbox, c=1024.0):\n  bbox=np.fromstring(bbox[1:-1],sep=',')\n  x=(bbox[0]+bbox[2]/2.0)/c\n  y=(bbox[1]+bbox[3]/2.0)/c\n  w= bbox[2]/c\n  h=bbox[3]/c\n  yolo_box=[x,y,w,h]\n  return(yolo_box)\n","execution_count":null,"outputs":[]},{"metadata":{"id":"h4GQ59QPXGOC","trusted":true},"cell_type":"code","source":"train['yolo_box'] = train.bbox.apply(convert_to_yolo)","execution_count":null,"outputs":[]},{"metadata":{"id":"rlznSk0Sa3Bh","trusted":true},"cell_type":"code","source":"unique=train.image_id.unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if not os.path.exists(\"../yolo\"):\n    os.makedirs(\"../yolo\")","execution_count":null,"outputs":[]},{"metadata":{"id":"rSKj-vBUEEJI","trusted":true},"cell_type":"code","source":"for i in unique:\n  file= \"%s.txt\" %i\n  a='../yolo'\n  b=file\n  path=os.path.join(a,b)\n  os.mknod(path)\n  file_data= train.query('image_id == \"%s\"' %i)\n  boxes = file_data.yolo_box.values\n  with open(path, 'a') as file:\n    for j in boxes:\n      s = \"0 %s %s %s %s \\n\"\n      new_line = (s % tuple(j))\n      file.write(new_line)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Test with a sample .txt file if it is created properly.","execution_count":null},{"metadata":{"id":"oHytY0KZabdr","trusted":true},"cell_type":"code","source":"f=open('../yolo/b53afdf5c.txt')","execution_count":null,"outputs":[]},{"metadata":{"id":"y0kGx2EqadM7","trusted":true,"collapsed":true},"cell_type":"code","source":"print(f.read())","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}