{"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":"## YOLO approach for RSNA Brain Tumor\n* The YOLO system includes a function to train a classifier to identify the object.\n * https://github.com/ultralytics/yolov5\n* To do it, need to manually create annotation data to train the classifier. \n* Annotation work is conducted manually using png-converted dataset and an annotation tool, LabelMe.\n * https://www.kaggle.com/jonathanbesomi/rsna-miccai-png\n * https://github.com/wkentaro/labelme\n* This notebook shows the process of converting json annotation data to DataFrame.\n* If YOLO is used in this competition, it is required that the trained classifier never produce false positive judgement, because there are dozens of images per patient.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport json\nimport codecs\nfrom pandas.io.json import json_normalize","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.29125,"end_time":"2021-09-12T17:34:27.359975","exception":false,"start_time":"2021-09-12T17:34:27.068725","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:23.829449Z","iopub.execute_input":"2021-09-17T14:18:23.829966Z","iopub.status.idle":"2021-09-17T14:18:24.028999Z","shell.execute_reply.started":"2021-09-17T14:18:23.829875Z","shell.execute_reply":"2021-09-17T14:18:24.027948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files=[]\nfor dirname, _, filenames in os.walk('../input/rsna-miccai-brain-annotation/rsna_biccai_brain_0916'):\n    for filename in filenames:\n        files+=[os.path.join(dirname, filename)]","metadata":{"papermill":{"duration":0.498533,"end_time":"2021-09-12T17:34:27.968678","exception":false,"start_time":"2021-09-12T17:34:27.470145","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:24.031035Z","iopub.execute_input":"2021-09-17T14:18:24.031318Z","iopub.status.idle":"2021-09-17T14:18:24.119161Z","shell.execute_reply.started":"2021-09-17T14:18:24.031289Z","shell.execute_reply":"2021-09-17T14:18:24.118107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgpath=[]\nfor item in files:\n    if item[-4:]=='.png':\n          imgpath+=[item]\nprint(imgpath[0:5])\nprint(len(imgpath))","metadata":{"papermill":{"duration":0.029775,"end_time":"2021-09-12T17:34:28.018186","exception":false,"start_time":"2021-09-12T17:34:27.988411","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:24.120338Z","iopub.execute_input":"2021-09-17T14:18:24.120585Z","iopub.status.idle":"2021-09-17T14:18:24.127436Z","shell.execute_reply.started":"2021-09-17T14:18:24.120557Z","shell.execute_reply":"2021-09-17T14:18:24.126364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"jsonpath=[]\nfor item in files:\n    if item[-4:]=='json':\n          jsonpath+=[item]\nprint(jsonpath[0:5])\nprint(len(jsonpath))","metadata":{"papermill":{"duration":0.030004,"end_time":"2021-09-12T17:34:28.068553","exception":false,"start_time":"2021-09-12T17:34:28.038549","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:24.130027Z","iopub.execute_input":"2021-09-17T14:18:24.130269Z","iopub.status.idle":"2021-09-17T14:18:24.142310Z","shell.execute_reply.started":"2021-09-17T14:18:24.130239Z","shell.execute_reply":"2021-09-17T14:18:24.141121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"box=[]\nwith open('/kaggle/input/rsna-miccai-brain-annotation/rsna_biccai_brain_0916/0000/Image-88.json') as f:\n    d = json.load(f)\n    print(d)","metadata":{"papermill":{"duration":0.035797,"end_time":"2021-09-12T17:34:28.174139","exception":false,"start_time":"2021-09-12T17:34:28.138342","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:24.143893Z","iopub.execute_input":"2021-09-17T14:18:24.144259Z","iopub.status.idle":"2021-09-17T14:18:24.161050Z","shell.execute_reply.started":"2021-09-17T14:18:24.144211Z","shell.execute_reply":"2021-09-17T14:18:24.160108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"jsonpath[0]","metadata":{"papermill":{"duration":0.031435,"end_time":"2021-09-12T17:34:28.226467","exception":false,"start_time":"2021-09-12T17:34:28.195032","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:24.162712Z","iopub.execute_input":"2021-09-17T14:18:24.163287Z","iopub.status.idle":"2021-09-17T14:18:24.173834Z","shell.execute_reply.started":"2021-09-17T14:18:24.163215Z","shell.execute_reply":"2021-09-17T14:18:24.172448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BOX=[]\nfor path in jsonpath:\n    with open(path) as f:\n        idn=path.split('/')[-2]\n        d = json.load(f)\n        labels=d['shapes']\n        height=d['imageHeight']\n        width=d['imageWidth']\n        file=path[0:-5].split('/')[-1]\n        for item in labels:\n            BOX+=[[idn,file,item['label'],item['points'][0][0],item['points'][0][1],item['points'][1][0],item['points'][1][1],height,width]]\nprint(BOX[0:5])    ","metadata":{"papermill":{"duration":1.226241,"end_time":"2021-09-12T17:34:29.47391","exception":false,"start_time":"2021-09-12T17:34:28.247669","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:24.175835Z","iopub.execute_input":"2021-09-17T14:18:24.176189Z","iopub.status.idle":"2021-09-17T14:18:25.030851Z","shell.execute_reply.started":"2021-09-17T14:18:24.176147Z","shell.execute_reply":"2021-09-17T14:18:25.029686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data0=pd.DataFrame(BOX)\ndata0.columns=['idn','file','name','ymin','xmin','ymax','xmax','height','width']\ndata0[0:5]","metadata":{"papermill":{"duration":0.061762,"end_time":"2021-09-12T17:34:29.558328","exception":false,"start_time":"2021-09-12T17:34:29.496566","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:25.032429Z","iopub.execute_input":"2021-09-17T14:18:25.033033Z","iopub.status.idle":"2021-09-17T14:18:25.064536Z","shell.execute_reply.started":"2021-09-17T14:18:25.032992Z","shell.execute_reply":"2021-09-17T14:18:25.063446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data0['label']=data0['name'].map({'non_tumor':0,'tumor':1})\ndata0['Ycent']=(data0['ymin']+data0['ymax'])/(2*data0['height'])\ndata0['Xcent']=(data0['xmin']+data0['xmax'])/(2*data0['width'])\ndata0['boxH']=(data0['ymax']-data0['ymin'])/data0['height']\ndata0['boxW']=(data0['xmax']-data0['xmin'])/data0['width']\ndata0[0:5]","metadata":{"papermill":{"duration":0.079492,"end_time":"2021-09-12T17:34:29.6606","exception":false,"start_time":"2021-09-12T17:34:29.581108","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:25.066258Z","iopub.execute_input":"2021-09-17T14:18:25.066502Z","iopub.status.idle":"2021-09-17T14:18:25.103430Z","shell.execute_reply.started":"2021-09-17T14:18:25.066474Z","shell.execute_reply":"2021-09-17T14:18:25.102069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data0.to_csv('brain_tumor_annotation.csv',index=False)","metadata":{"papermill":{"duration":0.038012,"end_time":"2021-09-12T17:34:29.722781","exception":false,"start_time":"2021-09-12T17:34:29.684769","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:25.106980Z","iopub.execute_input":"2021-09-17T14:18:25.108213Z","iopub.status.idle":"2021-09-17T14:18:25.122072Z","shell.execute_reply.started":"2021-09-17T14:18:25.108129Z","shell.execute_reply":"2021-09-17T14:18:25.120928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data0[data0['file']=='Image-88'][data0['idn']=='0000']","metadata":{"papermill":{"duration":0.050654,"end_time":"2021-09-12T17:34:29.797375","exception":false,"start_time":"2021-09-12T17:34:29.746721","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:25.124704Z","iopub.execute_input":"2021-09-17T14:18:25.125102Z","iopub.status.idle":"2021-09-17T14:18:25.150492Z","shell.execute_reply.started":"2021-09-17T14:18:25.125057Z","shell.execute_reply":"2021-09-17T14:18:25.149246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items=data0[data0['file']=='Image-88'][data0['idn']=='0000'].iloc[:,0:6].reset_index(drop=True)\nprint(len(items))\nprint(items.loc[0])","metadata":{"papermill":{"duration":0.037888,"end_time":"2021-09-12T17:34:29.859333","exception":false,"start_time":"2021-09-12T17:34:29.821445","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:25.152212Z","iopub.execute_input":"2021-09-17T14:18:25.152475Z","iopub.status.idle":"2021-09-17T14:18:25.165728Z","shell.execute_reply.started":"2021-09-17T14:18:25.152447Z","shell.execute_reply":"2021-09-17T14:18:25.165058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgpath[322]","metadata":{"papermill":{"duration":0.033698,"end_time":"2021-09-12T17:34:29.96914","exception":false,"start_time":"2021-09-12T17:34:29.935442","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:25.166680Z","iopub.execute_input":"2021-09-17T14:18:25.166919Z","iopub.status.idle":"2021-09-17T14:18:25.180544Z","shell.execute_reply.started":"2021-09-17T14:18:25.166883Z","shell.execute_reply":"2021-09-17T14:18:25.179706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,item in enumerate(imgpath):\n    if item[0:-4].split('/')[-1]=='Image-88' and item[0:-4].split('/')[-2]=='0000':\n          print(i)","metadata":{"papermill":{"duration":0.035082,"end_time":"2021-09-12T17:34:30.029155","exception":false,"start_time":"2021-09-12T17:34:29.994073","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:25.181823Z","iopub.execute_input":"2021-09-17T14:18:25.182142Z","iopub.status.idle":"2021-09-17T14:18:25.194352Z","shell.execute_reply.started":"2021-09-17T14:18:25.182097Z","shell.execute_reply":"2021-09-17T14:18:25.193130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## cv2.rectangle(image,(ymin,xmin),(ymax,xmax),(0,255,0),2)\n* https://www.geeksforgeeks.org/python-opencv-cv2-rectangle-method/","metadata":{"papermill":{"duration":0.024952,"end_time":"2021-09-12T17:34:30.079193","exception":false,"start_time":"2021-09-12T17:34:30.054241","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def draw_bbox2(path):\n\n    items=data0[data0['file']==path[0:-4].split('/')[-1]][data0['idn']==path[0:-4].split('/')[-2]].iloc[:,0:13].reset_index(drop=True)\n    image=cv2.imread(path)\n    idn=path[0:-4].split('/')[-2]\n    file=path[0:-4].split('/')[-1]    \n    print(idn,file)\n    if len(items)>0:\n        for i in range(len(items)):\n            label=items.loc[i,'label'].astype(int)\n            xmin=items.loc[i,'xmin'].astype(int)\n            ymin=items.loc[i,'ymin'].astype(int)\n            xmax=items.loc[i,'xmax'].astype(int)\n            ymax=items.loc[i,'ymax'].astype(int)\n            H=items.loc[i,'height'].astype(int)\n            W=items.loc[i,'width'].astype(int)            \n            print(label,ymin,xmin,ymax,xmax)\n            if label==0:\n                cv2.rectangle(image,(ymin,xmin),(ymax,xmax),(0,255,0),2)\n            elif label==1:\n                cv2.rectangle(image,(ymin,xmin),(ymax,xmax),(0,0,255),2)         \n            \n    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n    plt.show()       ","metadata":{"papermill":{"duration":0.042183,"end_time":"2021-09-12T17:34:30.146641","exception":false,"start_time":"2021-09-12T17:34:30.104458","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:25.195916Z","iopub.execute_input":"2021-09-17T14:18:25.196236Z","iopub.status.idle":"2021-09-17T14:18:25.212861Z","shell.execute_reply.started":"2021-09-17T14:18:25.196196Z","shell.execute_reply":"2021-09-17T14:18:25.211576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_bbox2(imgpath[322])","metadata":{"papermill":{"duration":0.260934,"end_time":"2021-09-12T17:34:30.433345","exception":false,"start_time":"2021-09-12T17:34:30.172411","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:25.214426Z","iopub.execute_input":"2021-09-17T14:18:25.214727Z","iopub.status.idle":"2021-09-17T14:18:25.526845Z","shell.execute_reply.started":"2021-09-17T14:18:25.214696Z","shell.execute_reply":"2021-09-17T14:18:25.525757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for item in imgpath[0:6]:\n    draw_bbox2(item)","metadata":{"papermill":{"duration":148.487049,"end_time":"2021-09-12T17:36:58.947738","exception":false,"start_time":"2021-09-12T17:34:30.460689","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-17T14:18:25.528231Z","iopub.execute_input":"2021-09-17T14:18:25.528471Z","iopub.status.idle":"2021-09-17T14:18:26.870972Z","shell.execute_reply.started":"2021-09-17T14:18:25.528444Z","shell.execute_reply":"2021-09-17T14:18:26.869765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":1.050164,"end_time":"2021-09-12T17:37:03.206454","exception":false,"start_time":"2021-09-12T17:37:02.15629","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":1.110503,"end_time":"2021-09-12T17:37:05.39124","exception":false,"start_time":"2021-09-12T17:37:04.280737","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":1.044066,"end_time":"2021-09-12T17:37:07.475798","exception":false,"start_time":"2021-09-12T17:37:06.431732","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}