{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Here we want to convert datasets labels to Yolov5 format.","metadata":{}},{"cell_type":"code","source":"import cv2\nimport pandas as pd\nimport ast","metadata":{"execution":{"iopub.status.busy":"2022-01-03T11:22:56.065102Z","iopub.execute_input":"2022-01-03T11:22:56.066216Z","iopub.status.idle":"2022-01-03T11:22:56.529964Z","shell.execute_reply.started":"2022-01-03T11:22:56.066171Z","shell.execute_reply":"2022-01-03T11:22:56.529325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path2='../input/tensorflow-great-barrier-reef/train_images/video_2/'\nimg = cv2.imread(path2+'1.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-01-03T11:22:56.531321Z","iopub.execute_input":"2022-01-03T11:22:56.531676Z","iopub.status.idle":"2022-01-03T11:22:56.604714Z","shell.execute_reply.started":"2022-01-03T11:22:56.531647Z","shell.execute_reply":"2022-01-03T11:22:56.603812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Trying to figure out shape of images","metadata":{}},{"cell_type":"code","source":"height, width, channels = img.shape\nprint(height, width)","metadata":{"execution":{"iopub.status.busy":"2022-01-03T11:22:56.60618Z","iopub.execute_input":"2022-01-03T11:22:56.606856Z","iopub.status.idle":"2022-01-03T11:22:56.611954Z","shell.execute_reply.started":"2022-01-03T11:22:56.606815Z","shell.execute_reply":"2022-01-03T11:22:56.611062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Checking format of values in one row","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('../input/tensorflow-great-barrier-reef/train.csv')\nprint(df.iloc[341])","metadata":{"execution":{"iopub.status.busy":"2022-01-03T11:22:56.613121Z","iopub.execute_input":"2022-01-03T11:22:56.613371Z","iopub.status.idle":"2022-01-03T11:22:56.680093Z","shell.execute_reply.started":"2022-01-03T11:22:56.613334Z","shell.execute_reply":"2022-01-03T11:22:56.679219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We dose not have permission for \"../input\" folder so we create a folder for writing \".txt\" files in it","metadata":{}},{"cell_type":"code","source":"!mkdir -p ../yolov5fromat/labels","metadata":{"execution":{"iopub.status.busy":"2022-01-03T11:22:56.683585Z","iopub.execute_input":"2022-01-03T11:22:56.683808Z","iopub.status.idle":"2022-01-03T11:22:57.475154Z","shell.execute_reply.started":"2022-01-03T11:22:56.683779Z","shell.execute_reply":"2022-01-03T11:22:57.474149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As you can see x and y is pixel format so we need to convert it to Yolov5 annotations format. For this matter we use a library named \"ast\" that read the annotation in dictionary foramt. then use dictionary values to create labels.","metadata":{}},{"cell_type":"code","source":"for j in range(len(df)):\n    list_anno = ast.literal_eval(df.iloc[j][5])\n    for anno in list_anno:\n        with open('../yolov5fromat/labels/' + str(df.iloc[j][0]) + '_' + str(df.iloc[j][2]) + '.txt', 'w') as file:\n            file.write(str(0) + ' ' + str(anno['x']/width) + ' ' + str(anno['y']/height) + ' ' + str(anno['width']) + ' ' + str(anno['height']) + '\\n')","metadata":{"execution":{"iopub.status.busy":"2022-01-03T11:22:57.476797Z","iopub.execute_input":"2022-01-03T11:22:57.47717Z","iopub.status.idle":"2022-01-03T11:23:05.210092Z","shell.execute_reply.started":"2022-01-03T11:22:57.477136Z","shell.execute_reply":"2022-01-03T11:23:05.209286Z"},"trusted":true},"execution_count":null,"outputs":[]}]}