{"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":"import cv2\nimport matplotlib.pyplot as plt\nimport multiprocessing\nfrom multiprocessing.pool import ThreadPool\nimport os\nimport pandas as pd\nimport numpy as np\nimport shutil","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-25T12:05:20.1522Z","iopub.execute_input":"2021-05-25T12:05:20.153175Z","iopub.status.idle":"2021-05-25T12:05:20.332387Z","shell.execute_reply.started":"2021-05-25T12:05:20.153063Z","shell.execute_reply":"2021-05-25T12:05:20.331374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 512","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:05:26.018472Z","iopub.execute_input":"2021-05-25T12:05:26.018894Z","iopub.status.idle":"2021-05-25T12:05:26.024283Z","shell.execute_reply.started":"2021-05-25T12:05:26.018817Z","shell.execute_reply":"2021-05-25T12:05:26.022998Z"},"trusted":true},"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\ndef circle_crop(img, sigmaX = 30):   \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    cv2.resize(img, (512, 512))\n    return img \n\ndef preprocess_image(file):\n    input_filepath ='../input/aptos2019-blindness-detection/train_images/'+file\n    output_filepath = './preprocess/1/'+file\n    \n    img = cv2.imread(input_filepath)\n    img = circle_crop(img) \n    \n    #print(img.shape)\n    #plt.imshow(img)\n    cv2.imwrite(output_filepath, cv2.resize(img, (IMG_SIZE,IMG_SIZE)))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:06:18.102846Z","iopub.execute_input":"2021-05-25T12:06:18.103209Z","iopub.status.idle":"2021-05-25T12:06:18.120103Z","shell.execute_reply.started":"2021-05-25T12:06:18.10318Z","shell.execute_reply":"2021-05-25T12:06:18.118775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''This Function uses Multi processing for faster saving of images into folder'''\n\ndef multiprocess_image_processor(process:int, imgs:list):\n    \"\"\"\n    Inputs:\n        process: (int) number of process to run\n        imgs:(list) list of images\n    \"\"\"\n    print(f'MESSAGE: Running {process} process')\n    results = ThreadPool(process).map(preprocess_image, imgs)\n    return results","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:05:32.471927Z","iopub.execute_input":"2021-05-25T12:05:32.472268Z","iopub.status.idle":"2021-05-25T12:05:32.477509Z","shell.execute_reply.started":"2021-05-25T12:05:32.472239Z","shell.execute_reply":"2021-05-25T12:05:32.476581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:05:35.384718Z","iopub.execute_input":"2021-05-25T12:05:35.385403Z","iopub.status.idle":"2021-05-25T12:05:35.413813Z","shell.execute_reply.started":"2021-05-25T12:05:35.385343Z","shell.execute_reply":"2021-05-25T12:05:35.413003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df.columns[0]]+='.png'","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:05:37.543669Z","iopub.execute_input":"2021-05-25T12:05:37.544223Z","iopub.status.idle":"2021-05-25T12:05:37.573352Z","shell.execute_reply.started":"2021-05-25T12:05:37.544188Z","shell.execute_reply":"2021-05-25T12:05:37.572483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:05:41.011173Z","iopub.execute_input":"2021-05-25T12:05:41.011602Z","iopub.status.idle":"2021-05-25T12:05:41.033578Z","shell.execute_reply.started":"2021-05-25T12:05:41.011565Z","shell.execute_reply":"2021-05-25T12:05:41.032898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('./preprocess')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:06:00.666746Z","iopub.execute_input":"2021-05-25T12:06:00.667347Z","iopub.status.idle":"2021-05-25T12:06:00.671087Z","shell.execute_reply.started":"2021-05-25T12:06:00.667313Z","shell.execute_reply":"2021-05-25T12:06:00.670333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('./preprocess/1')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:08:40.743429Z","iopub.execute_input":"2021-05-25T12:08:40.743911Z","iopub.status.idle":"2021-05-25T12:08:40.796066Z","shell.execute_reply.started":"2021-05-25T12:08:40.743838Z","shell.execute_reply":"2021-05-25T12:08:40.793081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"multiprocess_image_processor(6, df['id_code'].values)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:07:07.49883Z","iopub.execute_input":"2021-05-25T12:07:07.499203Z","iopub.status.idle":"2021-05-25T12:07:12.66161Z","shell.execute_reply.started":"2021-05-25T12:07:07.499173Z","shell.execute_reply":"2021-05-25T12:07:12.659386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nimport os","metadata":{"execution":{"iopub.status.busy":"2021-05-25T13:04:50.099611Z","iopub.execute_input":"2021-05-25T13:04:50.099981Z","iopub.status.idle":"2021-05-25T13:04:50.103586Z","shell.execute_reply.started":"2021-05-25T13:04:50.099949Z","shell.execute_reply":"2021-05-25T13:04:50.102877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.make_archive('wnld', 'zip', './preprocess')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:56:48.086922Z","iopub.execute_input":"2021-05-25T12:56:48.087317Z","iopub.status.idle":"2021-05-25T12:58:22.170530Z","shell.execute_reply.started":"2021-05-25T12:56:48.087287Z","shell.execute_reply":"2021-05-25T12:58:22.169569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('./preprocess/1'))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T13:05:03.771554Z","iopub.execute_input":"2021-05-25T13:05:03.772313Z","iopub.status.idle":"2021-05-25T13:05:03.781414Z","shell.execute_reply.started":"2021-05-25T13:05:03.772270Z","shell.execute_reply":"2021-05-25T13:05:03.780112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}