{"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-06-20T06:39:36.448225Z","iopub.execute_input":"2021-06-20T06:39:36.448591Z","iopub.status.idle":"2021-06-20T06:39:36.453414Z","shell.execute_reply.started":"2021-06-20T06:39:36.44856Z","shell.execute_reply":"2021-06-20T06:39:36.452155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 224","metadata":{"execution":{"iopub.status.busy":"2021-06-20T06:43:32.833935Z","iopub.execute_input":"2021-06-20T06:43:32.834607Z","iopub.status.idle":"2021-06-20T06:43:32.838445Z","shell.execute_reply.started":"2021-06-20T06:43:32.834569Z","shell.execute_reply":"2021-06-20T06:43:32.837321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 필요없는 검정색 배경 부분 삭제 함수\ndef 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) # 원형으로 사진 cutting 해주는 함수\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    #img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n    #plt.imshow(img)\n    cv2.imwrite(output_filepath, cv2.resize(img, (IMG_SIZE,IMG_SIZE)))","metadata":{"execution":{"iopub.status.busy":"2021-06-20T06:43:52.023765Z","iopub.execute_input":"2021-06-20T06:43:52.024165Z","iopub.status.idle":"2021-06-20T06:43:52.04052Z","shell.execute_reply.started":"2021-06-20T06:43:52.024128Z","shell.execute_reply":"2021-06-20T06:43:52.039414Z"},"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-30T13:08:30.763149Z","iopub.execute_input":"2021-05-30T13:08:30.763711Z","iopub.status.idle":"2021-05-30T13:08:30.769007Z","shell.execute_reply.started":"2021-05-30T13:08:30.763662Z","shell.execute_reply":"2021-05-30T13:08:30.767786Z"},"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-06-20T05:55:40.754359Z","iopub.execute_input":"2021-06-20T05:55:40.754662Z","iopub.status.idle":"2021-06-20T05:55:40.770831Z","shell.execute_reply.started":"2021-06-20T05:55:40.754634Z","shell.execute_reply":"2021-06-20T05:55:40.769667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df.columns[0]]+='.png'","metadata":{"execution":{"iopub.status.busy":"2021-06-20T05:55:40.772343Z","iopub.execute_input":"2021-06-20T05:55:40.772623Z","iopub.status.idle":"2021-06-20T05:55:40.801025Z","shell.execute_reply.started":"2021-06-20T05:55:40.772596Z","shell.execute_reply":"2021-06-20T05:55:40.799789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-30T13:08:36.290906Z","iopub.execute_input":"2021-05-30T13:08:36.291264Z","iopub.status.idle":"2021-05-30T13:08:36.312975Z","shell.execute_reply.started":"2021-05-30T13:08:36.291235Z","shell.execute_reply":"2021-05-30T13:08:36.311837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('./preprocess')","metadata":{"execution":{"iopub.status.busy":"2021-05-30T13:08:40.001137Z","iopub.execute_input":"2021-05-30T13:08:40.001591Z","iopub.status.idle":"2021-05-30T13:08:40.006792Z","shell.execute_reply.started":"2021-05-30T13:08:40.001553Z","shell.execute_reply":"2021-05-30T13:08:40.005345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('./preprocess/1')","metadata":{"execution":{"iopub.status.busy":"2021-05-30T13:08:44.245628Z","iopub.execute_input":"2021-05-30T13:08:44.245999Z","iopub.status.idle":"2021-05-30T13:08:44.250251Z","shell.execute_reply.started":"2021-05-30T13:08:44.245969Z","shell.execute_reply":"2021-05-30T13:08:44.249145Z"},"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-30T13:08:46.278052Z","iopub.execute_input":"2021-05-30T13:08:46.27865Z","iopub.status.idle":"2021-05-30T13:10:34.069398Z","shell.execute_reply.started":"2021-05-30T13:08:46.278595Z","shell.execute_reply":"2021-05-30T13:10:34.065237Z"},"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.77227Z","shell.execute_reply":"2021-05-25T13:05:03.780112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}