{"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 numpy as np\nimport cv2\nimport os\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom tqdm.notebook import tqdm\nimport tensorflow as tf\nfrom sklearn import preprocessing","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:07.419278Z","iopub.execute_input":"2022-03-02T11:05:07.419893Z","iopub.status.idle":"2022-03-02T11:05:13.043287Z","shell.execute_reply.started":"2022-03-02T11:05:07.419800Z","shell.execute_reply":"2022-03-02T11:05:13.042546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cvtImgToSquare(image ,color = \"b\"):\n    # 先複製原圖\n    copy_image = image.copy()\n    \n    # shape\n    x,y,z = copy_image.shape\n    \n    # 補X方向\n    if x < y:\n        # 計算出x, y 差的一半\n        helf_diff1 = (y - x) // 2\n        helf_diff2 = y - x - helf_diff1\n        if color == \"b\":\n            # 做出兩塊用於補齊正方形的黑色\n            zeros_matrix1 = np.zeros((helf_diff1, y, z),dtype=\"uint8\")\n            zeros_matrix2 = np.zeros((helf_diff2, y, z),dtype=\"uint8\")\n        elif color == \"w\":\n            # 做出兩塊用於補齊正方形的白色\n            zeros_matrix1 = np.ones((helf_diff1, y, z),dtype=\"uint8\")*255\n            zeros_matrix2 = np.ones((helf_diff2, y, z),dtype=\"uint8\")*255\n        # 將黑色1補在image 最後補黑色2\n        concate_img = np.concatenate((zeros_matrix1, copy_image),axis = 0)\n        concate_img = np.concatenate((concate_img, zeros_matrix2),axis = 0)\n\n    # 補Y方向\n    elif x > y:\n        # 計算出x, y 差的一半\n        helf_diff1 = (x - y) // 2\n        helf_diff2 = x - y - helf_diff1\n        \n        if color == \"b\":\n            # 做出兩塊用於補齊正方形的黑色\n            zeros_matrix1 = np.zeros((x, helf_diff1, z),dtype=\"uint8\")\n            zeros_matrix2 = np.zeros((x, helf_diff2, z),dtype=\"uint8\")\n        elif color == \"w\":\n            # 做出兩塊用於補齊正方形的白色\n            zeros_matrix1 = np.ones((x, helf_diff1, z),dtype=\"uint8\")*255\n            zeros_matrix2 = np.ones((x, helf_diff2, z),dtype=\"uint8\")*255\n        \n        # 將黑色1補在image 最後補黑色2\n        concate_img = np.concatenate((zeros_matrix1,copy_image),axis = 1)\n        concate_img = np.concatenate((concate_img,zeros_matrix2),axis = 1)\n    # 回傳正方形圖片\n    else:\n        return copy_image\n    return concate_img","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:13.044806Z","iopub.execute_input":"2022-03-02T11:05:13.045030Z","iopub.status.idle":"2022-03-02T11:05:13.058802Z","shell.execute_reply.started":"2022-03-02T11:05:13.044997Z","shell.execute_reply":"2022-03-02T11:05:13.057992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def squareImgResize(image,resize_to):\n    # 圖片大小\n    x,y,z = image.shape\n    # 判斷縮還是放\n    if x > resize_to and y > resize_to:\n        inter = cv2.INTER_AREA\n    elif x < resize_to and y < resize_to:\n        inter = cv2.INTER_CUBIC\n    # 縮放影像 依照縮放選擇內插法\n    else: #同樣大小不縮放\n        return image\n    resize_image = cv2.resize(image, (resize_to, resize_to), interpolation=inter)\n    # 回傳指定大小\n    return resize_image.reshape(resize_to, resize_to,z)","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:13.060267Z","iopub.execute_input":"2022-03-02T11:05:13.060539Z","iopub.status.idle":"2022-03-02T11:05:13.076650Z","shell.execute_reply.started":"2022-03-02T11:05:13.060490Z","shell.execute_reply":"2022-03-02T11:05:13.075883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv('/kaggle/input/happy-whale-and-dolphin/train.csv')\ntrain_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:13.078571Z","iopub.execute_input":"2022-03-02T11:05:13.078928Z","iopub.status.idle":"2022-03-02T11:05:13.186157Z","shell.execute_reply.started":"2022-03-02T11:05:13.078854Z","shell.execute_reply":"2022-03-02T11:05:13.185404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['image'][0]","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:13.187408Z","iopub.execute_input":"2022-03-02T11:05:13.187786Z","iopub.status.idle":"2022-03-02T11:05:13.197425Z","shell.execute_reply.started":"2022-03-02T11:05:13.187748Z","shell.execute_reply":"2022-03-02T11:05:13.196359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_csv)","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:13.199411Z","iopub.execute_input":"2022-03-02T11:05:13.199814Z","iopub.status.idle":"2022-03-02T11:05:13.208485Z","shell.execute_reply.started":"2022-03-02T11:05:13.199773Z","shell.execute_reply":"2022-03-02T11:05:13.207679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['image'][:5]","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-03-02T11:05:13.209883Z","iopub.execute_input":"2022-03-02T11:05:13.210549Z","iopub.status.idle":"2022-03-02T11:05:13.219893Z","shell.execute_reply.started":"2022-03-02T11:05:13.210475Z","shell.execute_reply":"2022-03-02T11:05:13.218958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array([],dtype = \"uint8\")","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:13.221849Z","iopub.execute_input":"2022-03-02T11:05:13.222172Z","iopub.status.idle":"2022-03-02T11:05:13.230874Z","shell.execute_reply.started":"2022-03-02T11:05:13.222115Z","shell.execute_reply":"2022-03-02T11:05:13.230072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 圖片數量\ndata_numders = 100\n# 圖片資料夾\nimg_dir = \"/kaggle/input/happy-whale-and-dolphin/train_images/\"\nnp_dataset = \"/kaggle/working\"\n# 初始化 y\ny = []\n\nfor i in tqdm(range(data_numders)):\n    # img path\n    img_path = img_dir + train_csv['image'][i]\n    \n    \n    # 前處理\n    gray = cv2.imread(img_path, 0) # 讀灰色圖\n    blurred = cv2.medianBlur(gray, 21) # 去雜訊\n    threshold = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\\\n                                  cv2.THRESH_BINARY, 9, 2)\n    h, w = threshold.shape\n    threshold = threshold.reshape(h, w, 1)\n    \n    # 統一格式\n    image = cvtImgToSquare(threshold, \"w\") # 轉換成方形 \n    image = squareImgResize(image, 720) # 縮放到 720 * 720\n    image = np.expand_dims(image, axis=0) # 增加一維\n    # append to x\n    if (i % 100) == 0:\n        if i != 0:\n            np.save(np_dataset + str(i),x)\n        x = image #初始化 x\n    else:\n        x = np.append(x,image,axis = 0)\n    # id\n    # individual_id = train_csv[\"individual_id\"][i]\n    \n    \n    # append to y\n    # y.append(individual_id)\nnp.save(np_dataset + str(i+1),x) # 最後一圈x檔案\ny = train_csv[\"individual_id\"][:data_numders]\ny = np.array(y)\nnp.save('save_y.npy',y)","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:13.232395Z","iopub.execute_input":"2022-03-02T11:05:13.232760Z","iopub.status.idle":"2022-03-02T11:05:33.145877Z","shell.execute_reply.started":"2022-03-02T11:05:13.232720Z","shell.execute_reply":"2022-03-02T11:05:33.145193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img = x[3]\nplt.figure(figsize=(25,25))\nplt.imshow(test_img, cmap='gray', vmin = 0, vmax = 255,interpolation='none')\nplt.show()\nprint(y[2])","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:33.148802Z","iopub.execute_input":"2022-03-02T11:05:33.149073Z","iopub.status.idle":"2022-03-02T11:05:33.631766Z","shell.execute_reply.started":"2022-03-02T11:05:33.149038Z","shell.execute_reply":"2022-03-02T11:05:33.628485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img = x[0]\nRGB = np.concatenate([test_img,test_img,test_img],axis = 2)\nplt.figure(figsize=(25,25))\nplt.imshow(RGB)\nplt.show()\nprint(y[2])","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:33.632652Z","iopub.execute_input":"2022-03-02T11:05:33.632917Z","iopub.status.idle":"2022-03-02T11:05:34.475082Z","shell.execute_reply.started":"2022-03-02T11:05:33.632877Z","shell.execute_reply":"2022-03-02T11:05:34.474474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img = x[0]\nnp.concatenate([test_img,test_img,test_img],axis = 2).shape","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:34.476270Z","iopub.execute_input":"2022-03-02T11:05:34.477678Z","iopub.status.idle":"2022-03-02T11:05:34.486101Z","shell.execute_reply.started":"2022-03-02T11:05:34.477624Z","shell.execute_reply":"2022-03-02T11:05:34.485293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"圖片形狀 :\",image.shape)\nprint(\"x形狀 :\",x.shape)\nprint(\"y形狀 :\",y.shape)\nLE = preprocessing.LabelEncoder()\nLE.fit(y)\nnum_y = LE.transform(y)\ncategorical_y = tf.keras.utils.to_categorical(LE.transform(y))\nprint(\"種類 :\",len(np.unique(tf.keras.utils.to_categorical(LE.transform(y)),axis = 0))) # 種類\nprint(\"y = \" ,y)\nprint(\"num_y = \",num_y)\nprint(\"categorical_y = \",categorical_y)","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:34.487560Z","iopub.execute_input":"2022-03-02T11:05:34.488000Z","iopub.status.idle":"2022-03-02T11:05:35.350759Z","shell.execute_reply.started":"2022-03-02T11:05:34.487961Z","shell.execute_reply":"2022-03-02T11:05:35.349977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimg = cv2.imread(img_path, 0)\nimg = cv2.medianBlur(img, 5)\n\nret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)\nth2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,\\\n    cv2.THRESH_BINARY, 13, 2)\nth3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\\\n    cv2.THRESH_BINARY, 13, 2)\n\ntitles = ['Original Image', 'Global Thresholding (v = 127)',\n            'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']\nimages = [img, th1, th2, th3]\n\nfor i in range(4):\n    plt.subplot(2, 2, i+1), plt.imshow(images[i], 'gray')\n    plt.title(titles[i])\n    plt.xticks([]), plt.yticks([])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:35.352072Z","iopub.execute_input":"2022-03-02T11:05:35.352545Z","iopub.status.idle":"2022-03-02T11:05:35.832257Z","shell.execute_reply.started":"2022-03-02T11:05:35.352474Z","shell.execute_reply":"2022-03-02T11:05:35.831563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = 9\nb = 2\nfor i in range(4):\n    img_path = img_dir + train_csv['image'][i+100]\n    img = cv2.imread(img_path, 0)\n    img = cv2.medianBlur(img, 21)\n    th3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\\\n        cv2.THRESH_BINARY, a, b)\n    plt.figure(figsize=(20,20))\n    plt.subplot(2, 2, i+1), plt.imshow(th3, 'gray')\n    plt.xticks([]), plt.yticks([])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:35.833522Z","iopub.execute_input":"2022-03-02T11:05:35.833883Z","iopub.status.idle":"2022-03-02T11:05:38.934006Z","shell.execute_reply.started":"2022-03-02T11:05:35.833849Z","shell.execute_reply":"2022-03-02T11:05:38.933331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(train_csv[\"species\"])","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-03-02T11:05:38.935214Z","iopub.execute_input":"2022-03-02T11:05:38.935810Z","iopub.status.idle":"2022-03-02T11:05:38.985178Z","shell.execute_reply.started":"2022-03-02T11:05:38.935773Z","shell.execute_reply":"2022-03-02T11:05:38.984440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(np.unique(train_csv[\"species\"]))","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:38.986542Z","iopub.execute_input":"2022-03-02T11:05:38.987662Z","iopub.status.idle":"2022-03-02T11:05:39.039353Z","shell.execute_reply.started":"2022-03-02T11:05:38.987614Z","shell.execute_reply":"2022-03-02T11:05:39.038456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(np.unique(train_csv[\"individual_id\"]))","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:39.040727Z","iopub.execute_input":"2022-03-02T11:05:39.040984Z","iopub.status.idle":"2022-03-02T11:05:39.102820Z","shell.execute_reply.started":"2022-03-02T11:05:39.040950Z","shell.execute_reply":"2022-03-02T11:05:39.101847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"threshold.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:39.104771Z","iopub.execute_input":"2022-03-02T11:05:39.105252Z","iopub.status.idle":"2022-03-02T11:05:39.114033Z","shell.execute_reply.started":"2022-03-02T11:05:39.105215Z","shell.execute_reply":"2022-03-02T11:05:39.113302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-02T11:05:39.115547Z","iopub.execute_input":"2022-03-02T11:05:39.116051Z","iopub.status.idle":"2022-03-02T11:05:39.122586Z","shell.execute_reply.started":"2022-03-02T11:05:39.116014Z","shell.execute_reply":"2022-03-02T11:05:39.121759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}