{"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":"code","source":"from PIL import Image\nimport cv2\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-06-14T18:16:24.047295Z","iopub.execute_input":"2021-06-14T18:16:24.047828Z","iopub.status.idle":"2021-06-14T18:16:25.164815Z","shell.execute_reply.started":"2021-06-14T18:16:24.047781Z","shell.execute_reply":"2021-06-14T18:16:25.163818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np","metadata":{"execution":{"iopub.status.busy":"2021-06-14T18:18:48.803699Z","iopub.execute_input":"2021-06-14T18:18:48.80411Z","iopub.status.idle":"2021-06-14T18:18:48.809622Z","shell.execute_reply.started":"2021-06-14T18:18:48.804077Z","shell.execute_reply":"2021-06-14T18:18:48.807975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def crop_image_from_gray(img,tol=7):\n    if img.ndim ==2:\n        mask = img>tol\n        return img[np.ix_(mask.any(1),mask.any(0))]\n    elif img.ndim==3:\n        gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n        mask = gray_img>tol\n        \n        check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0))].shape[0]\n        if (check_shape == 0): # image is too dark so that we crop out everything,\n            return img # return original image\n        else:\n            img1=img[:,:,0][np.ix_(mask.any(1),mask.any(0))]\n            img2=img[:,:,1][np.ix_(mask.any(1),mask.any(0))]\n            img3=img[:,:,2][np.ix_(mask.any(1),mask.any(0))]\n    #         print(img1.shape,img2.shape,img3.shape)\n            img = np.stack([img1,img2,img3],axis=-1)\n    #         print(img.shape)\n        return img\n    \n    \ndef circle_crop(img, sigmaX):   \n    \"\"\"\n    Create circular crop around image centre    \n    \"\"\"    \n    img = crop_image_from_gray(img)    \n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    \n    height, width, depth = img.shape    \n    \n    x = int(width/2)\n    y = int(height/2)\n    r = np.amin((x,y))\n    \n    circle_img = np.zeros((height, width), np.uint8)\n    cv2.circle(circle_img, (x,y), int(r), 1, thickness=-1)\n    img = cv2.bitwise_and(img, img, mask=circle_img)\n    img = crop_image_from_gray(img)\n    img=cv2.addWeighted(img,4, cv2.GaussianBlur( img , (0,0) , sigmaX) ,-4 ,128)\n    return img ","metadata":{"execution":{"iopub.status.busy":"2021-07-02T06:59:48.454054Z","iopub.execute_input":"2021-07-02T06:59:48.454413Z","iopub.status.idle":"2021-07-02T06:59:48.466851Z","shell.execute_reply.started":"2021-07-02T06:59:48.454382Z","shell.execute_reply":"2021-07-02T06:59:48.465862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(\"../input/aptos2019-blindness-detection/train_images/000c1434d8d7.png\")\nimg_t = circle_crop(img,sigmaX = 30)\n\nf, axarr = plt.subplots(1,2,figsize = (11,11))\naxarr[0].imshow(cv2.resize(cv2.cvtColor(img, cv2.COLOR_BGR2RGB),(256,256)))\naxarr[1].imshow(img_t)\nplt.title('After applying Circular Crop and Gaussian Blur')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-14T18:19:27.08962Z","iopub.execute_input":"2021-06-14T18:19:27.090027Z","iopub.status.idle":"2021-06-14T18:19:29.45368Z","shell.execute_reply.started":"2021-06-14T18:19:27.089994Z","shell.execute_reply":"2021-06-14T18:19:29.452298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport tqdm\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2021-07-02T06:56:57.765383Z","iopub.execute_input":"2021-07-02T06:56:57.765745Z","iopub.status.idle":"2021-07-02T06:56:57.770583Z","shell.execute_reply.started":"2021-07-02T06:56:57.765713Z","shell.execute_reply":"2021-07-02T06:56:57.769708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ntest_df = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')\nprint(train_df.shape)\nprint(test_df.shape)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-02T06:55:33.119366Z","iopub.execute_input":"2021-07-02T06:55:33.119702Z","iopub.status.idle":"2021-07-02T06:55:33.165125Z","shell.execute_reply.started":"2021-07-02T06:55:33.119671Z","shell.execute_reply":"2021-07-02T06:55:33.164403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_ben_color(path, sigmaX=10):\n    image = cv2.imread(path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = crop_image_from_gray(image)\n    image = cv2.resize(image, (256, 256))\n    image=cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , sigmaX) ,-4 ,128)\n        \n    return image","metadata":{"execution":{"iopub.status.busy":"2021-07-02T06:55:35.906745Z","iopub.execute_input":"2021-07-02T06:55:35.907061Z","iopub.status.idle":"2021-07-02T06:55:35.914307Z","shell.execute_reply.started":"2021-07-02T06:55:35.907032Z","shell.execute_reply":"2021-07-02T06:55:35.913430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_ht = 256\nimg_wd = 256","metadata":{"execution":{"iopub.status.busy":"2021-07-02T06:56:13.158267Z","iopub.execute_input":"2021-07-02T06:56:13.158621Z","iopub.status.idle":"2021-07-02T06:56:13.164519Z","shell.execute_reply.started":"2021-07-02T06:56:13.158589Z","shell.execute_reply":"2021-07-02T06:56:13.163734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2021-07-02T06:58:04.540837Z","iopub.execute_input":"2021-07-02T06:58:04.541165Z","iopub.status.idle":"2021-07-02T06:58:04.694703Z","shell.execute_reply.started":"2021-07-02T06:58:04.541134Z","shell.execute_reply":"2021-07-02T06:58:04.693838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = train_df.shape[0]\nx_train = np.empty((N, img_ht, img_wd, 3), dtype=np.uint8)\n\ndef path(p):\n    if(p.endswith(\".jpg\")):\n        return f'../input/aptos-balanced-dataset/train_images/train_images/{image_id}'\n    else:\n        return f'../input/aptos-balanced-dataset/train_images/train_images/{image_id}.png'\n\nfor i, image_id in enumerate((train_df['id_code'])):\n    x_train[i, :, :, :] = load_ben_color(f'../input/aptos2019-blindness-detection/train_images/{image_id}.png')\n    print(i)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T07:01:49.127573Z","iopub.execute_input":"2021-07-02T07:01:49.127969Z","iopub.status.idle":"2021-07-02T07:01:51.868455Z","shell.execute_reply.started":"2021-07-02T07:01:49.127931Z","shell.execute_reply":"2021-07-02T07:01:51.866616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = \"../input/aptos2019-blindness-detection/train_images/000c1434d8d7.png\"\nimgr = cv2.imread(\"../input/aptos2019-blindness-detection/train_images/000c1434d8d7.png\")\nimg_t = load_ben_color(img,sigmaX = 30)\n\nf, axarr = plt.subplots(1,2,figsize = (11,11))\naxarr[0].imshow(cv2.resize(cv2.cvtColor(imgr, cv2.COLOR_BGR2RGB),(256,256)))\naxarr[1].imshow(img_t)\nplt.title('After applying load_ben_color')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-14T18:35:32.534766Z","iopub.execute_input":"2021-06-14T18:35:32.535191Z","iopub.status.idle":"2021-06-14T18:35:33.586345Z","shell.execute_reply.started":"2021-06-14T18:35:32.535128Z","shell.execute_reply":"2021-06-14T18:35:33.580127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-06-14T18:23:44.188289Z","iopub.execute_input":"2021-06-14T18:23:44.188734Z","iopub.status.idle":"2021-06-14T18:23:44.195434Z","shell.execute_reply.started":"2021-06-14T18:23:44.188668Z","shell.execute_reply":"2021-06-14T18:23:44.194042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}