{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**import lib**"},{"metadata":{"trusted":true},"cell_type":"code","source":"import math\nimport os\n\nimport sys\n\nimport numpy as np\nimport pandas as pd\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport cv2\nfrom keras.preprocessing.image import array_to_img\nfrom PIL import Image as im \n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## import data"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data=pd.read_csv(\"../input/img-dim-train/train_img_dim.csv\")\ntrain_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"annots=train_data.copy()\nannots= annots[annots.class_id != 14] #drop NO Finding class\nannots.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(set(annots.image_id))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"listfile=[]\nfiles=os.listdir(\"../input/vinbigdata-chest-xray-abnormalities-detection/train\") \nfor file in files: \n    if os.path.isfile(\"../input/vinbigdata-chest-xray-abnormalities-detection/train/\" + file):\n        listfile.append(file)\nlen(listfile)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## norm bbox to new images"},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_SIZE=512\ndef norm_df(df):\n    df.x_min= ((df.x_min/ df.width)* IMG_SIZE).astype('int32')\n    df.x_max=( (df.x_max/ df.width)* IMG_SIZE).astype('int32')\n    \n    df.y_min= ((df.y_min/ df.height)* IMG_SIZE).astype('int32')\n    df.y_max= ((df.y_max/ df.height)* IMG_SIZE).astype('int32')\n    return df\ndf=norm_df(annots.copy()).reset_index(drop = True)\n\ndf.to_csv('all_data_dim_convert.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## make folder "},{"metadata":{"trusted":true},"cell_type":"code","source":"# if you want\n'''\nDATA_DIR = \"../input/vinbigdata-chest-xray-abnormalities-detection\"\ntrain_dcm_dir = os.path.join(DATA_DIR, \"train\")\ntest_dcm_dir = os.path.join(DATA_DIR, \"test\")\n\nimg_dir = os.path.join(os.getcwd(), \"images\")  # .jpg\ncsv_dir = os.path.join(os.getcwd(), \"df\")  # .csv\n\nfor directory in [img_dir, csv_dir]:\n    if os.path.isdir(directory):\n        continue\n    os.mkdir(directory)\n\n'''    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## All unique picture is 4394\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"\nmylist=[]\nfor image_id in df.image_id.unique():\n    mylist.append(image_id)\nprint('all file is : ' ,len(mylist))\n    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## convert dicom to jpg"},{"metadata":{},"cell_type":"markdown","source":"## Now we convert dicom to JPG or PNG  thanks to [raddar](http://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way)"},{"metadata":{"trusted":true},"cell_type":"code","source":"\ni=0\nfor item in mylist:\n    image_id=item\n    voi_lut = True\n    fix_monochrome = True\n    dicom = pydicom.dcmread('../input/vinbigdata-chest-xray-abnormalities-detection/train/'+image_id+'.dicom')\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n               \n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n        \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n \n    #X_train11 =data_file.pixel_array\n    new_array = cv2.resize(data, (IMG_SIZE,IMG_SIZE))# norm the value dont div on 255 \n    new_array = np.reshape(new_array, (IMG_SIZE, IMG_SIZE,1))\n    new_array = np.asarray(new_array)\n    img = array_to_img(new_array)\n    i=i+1\n    if (i+1)%500==0:\n        print(i+1)\n\n    img.save('/kaggle/working/' + image_id + '.jpg',dtype=np.uint16, quality=100)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### makeclasses csv file"},{"metadata":{"trusted":true},"cell_type":"code","source":"\n'''\nclasses=df.class_name.unique()\nclass_id=df.class_id.unique()\nprint(class_id)\nprint(classes)\n\nwith open('classes.csv', mode='w') as file:\n    for  clss ,i in zip(classes,class_id ):\n        file.write('{},{}\\n'.format(clss, i))\ncass=pd.read_csv('classes.csv')\ncass\n'''\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### add path to images in  csv"},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\n\ndf.image_id= df.image_id.apply(lambda x: '/kaggle/working/images'+ '/' + x + '.jpg')\ndf_path=df.copy()\ndf_path.head()\n\n\n'''\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\n","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}