{"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":"# 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-04T05:12:40.939492Z","iopub.execute_input":"2022-11-04T05:12:40.940893Z","iopub.status.idle":"2022-11-04T05:12:40.974831Z","shell.execute_reply.started":"2022-11-04T05:12:40.940793Z","shell.execute_reply":"2022-11-04T05:12:40.973423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nbase_dir = '/kaggle/input/carvana-image-masking-challenge'\n\nos.listdir(base_dir)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T05:13:16.943026Z","iopub.execute_input":"2022-11-04T05:13:16.943513Z","iopub.status.idle":"2022-11-04T05:13:16.953027Z","shell.execute_reply.started":"2022-11-04T05:13:16.943477Z","shell.execute_reply":"2022-11-04T05:13:16.952048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-11-04T05:13:19.897123Z","iopub.execute_input":"2022-11-04T05:13:19.897546Z","iopub.status.idle":"2022-11-04T05:13:19.903219Z","shell.execute_reply.started":"2022-11-04T05:13:19.897511Z","shell.execute_reply":"2022-11-04T05:13:19.902021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = plt.imread(os.path.join(base_dir, '29bb3ece3180_11.jpg'))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T05:14:16.373395Z","iopub.execute_input":"2022-11-04T05:14:16.373834Z","iopub.status.idle":"2022-11-04T05:14:17.188901Z","shell.execute_reply.started":"2022-11-04T05:14:16.373798Z","shell.execute_reply":"2022-11-04T05:14:17.188035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip -q ../input/carvana-image-masking-challenge/train.zip -d train/","metadata":{"execution":{"iopub.status.busy":"2022-11-04T05:15:59.623873Z","iopub.execute_input":"2022-11-04T05:15:59.624374Z","iopub.status.idle":"2022-11-04T05:16:09.685815Z","shell.execute_reply.started":"2022-11-04T05:15:59.624337Z","shell.execute_reply":"2022-11-04T05:16:09.684478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip -q ../input/carvana-image-masking-challenge/train_masks.zip -d train_masks/","metadata":{"execution":{"iopub.status.busy":"2022-11-04T05:18:16.198406Z","iopub.execute_input":"2022-11-04T05:18:16.200001Z","iopub.status.idle":"2022-11-04T05:18:17.935472Z","shell.execute_reply.started":"2022-11-04T05:18:16.199886Z","shell.execute_reply":"2022-11-04T05:18:17.933902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs = os.listdir('train/train/')\ntrain_masks_imgs = os.listdir('train_masks/train_masks')","metadata":{"execution":{"iopub.status.busy":"2022-11-04T05:19:27.899937Z","iopub.execute_input":"2022-11-04T05:19:27.900423Z","iopub.status.idle":"2022-11-04T05:19:27.914153Z","shell.execute_reply.started":"2022-11-04T05:19:27.900388Z","shell.execute_reply":"2022-11-04T05:19:27.912769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_imgs)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T05:19:30.190095Z","iopub.execute_input":"2022-11-04T05:19:30.190589Z","iopub.status.idle":"2022-11-04T05:19:30.200479Z","shell.execute_reply.started":"2022-11-04T05:19:30.190554Z","shell.execute_reply":"2022-11-04T05:19:30.198675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_masks_imgs)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T05:19:31.376153Z","iopub.execute_input":"2022-11-04T05:19:31.37711Z","iopub.status.idle":"2022-11-04T05:19:31.386289Z","shell.execute_reply.started":"2022-11-04T05:19:31.377058Z","shell.execute_reply":"2022-11-04T05:19:31.384744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = []\ntrain_mask_images = []\nids1 = []\nids2 = []\n\nfor i in train_imgs:\n    ids1.append(i.split('.')[0])\n    train_images.append('train/train/'+i)\n\nfor i in train_masks_imgs:\n    car_id = i.split(\".\")[0]\n    ids2.append(car_id.split(\"_mask\")[0])\n    train_mask_images.append('train_masks/train_masks/'+i)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-04T05:19:34.110623Z","iopub.execute_input":"2022-11-04T05:19:34.111163Z","iopub.status.idle":"2022-11-04T05:19:34.13024Z","shell.execute_reply.started":"2022-11-04T05:19:34.111124Z","shell.execute_reply":"2022-11-04T05:19:34.128433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(ids1[0])\nprint(ids2[0])\nprint(train_images[0])\nprint(train_mask_images[0])","metadata":{"execution":{"iopub.status.busy":"2022-11-04T05:19:47.479192Z","iopub.execute_input":"2022-11-04T05:19:47.479735Z","iopub.status.idle":"2022-11-04T05:19:47.48783Z","shell.execute_reply.started":"2022-11-04T05:19:47.479697Z","shell.execute_reply":"2022-11-04T05:19:47.48627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nd1 = {'id':ids1, 'img':train_images}\ndf1 = pd.DataFrame(data=d1)\n\nd2 = {'id':ids2, 'mask':train_mask_images}\ndf2 = pd.DataFrame(data=d2)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:12:46.50328Z","iopub.execute_input":"2022-11-03T07:12:46.504061Z","iopub.status.idle":"2022-11-03T07:12:46.522041Z","shell.execute_reply.started":"2022-11-03T07:12:46.504017Z","shell.execute_reply":"2022-11-03T07:12:46.520873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df1)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:12:49.896913Z","iopub.execute_input":"2022-11-03T07:12:49.897315Z","iopub.status.idle":"2022-11-03T07:12:50.066412Z","shell.execute_reply.started":"2022-11-03T07:12:49.897283Z","shell.execute_reply":"2022-11-03T07:12:50.065216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3 = pd.merge(df1, df2, on= 'id')","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:13:24.628572Z","iopub.execute_input":"2022-11-03T07:13:24.628998Z","iopub.status.idle":"2022-11-03T07:13:24.661176Z","shell.execute_reply.started":"2022-11-03T07:13:24.628966Z","shell.execute_reply":"2022-11-03T07:13:24.660132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:13:35.573128Z","iopub.execute_input":"2022-11-03T07:13:35.573859Z","iopub.status.idle":"2022-11-03T07:13:35.591353Z","shell.execute_reply.started":"2022-11-03T07:13:35.573822Z","shell.execute_reply":"2022-11-03T07:13:35.590206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2 \nimg = cv2.imread(df3['img'][0])\nprint(img.shape)\n# plt.imshow(img)\nimg_gray = cv2.imread(df3['img'][2], 0)\nprint(img_gray.shape)\n# img1 = plt.imread(df3['mask'][2])\n# plt.imshow(img1)\nplt.imshow(img_gray, cmap='gray', vmin=0, vmax=255)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:13:39.809891Z","iopub.execute_input":"2022-11-03T07:13:39.810323Z","iopub.status.idle":"2022-11-03T07:13:40.478218Z","shell.execute_reply.started":"2022-11-03T07:13:39.810277Z","shell.execute_reply":"2022-11-03T07:13:40.476922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" **Threshold**","metadata":{}},{"cell_type":"code","source":"res, thresh = cv2.threshold(img_gray, 80,155, cv2.THRESH_BINARY)\nres, thresh1 = cv2.threshold(img_gray, 80,155, cv2.THRESH_BINARY_INV)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:13:46.128234Z","iopub.execute_input":"2022-11-03T07:13:46.128612Z","iopub.status.idle":"2022-11-03T07:13:46.146292Z","shell.execute_reply.started":"2022-11-03T07:13:46.128582Z","shell.execute_reply":"2022-11-03T07:13:46.145239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titles = ['original image','binary', 'binary_inverse']\nimages = [img_gray, thresh, thresh1]","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:14:05.945909Z","iopub.execute_input":"2022-11-03T07:14:05.946357Z","iopub.status.idle":"2022-11-03T07:14:05.953131Z","shell.execute_reply.started":"2022-11-03T07:14:05.946323Z","shell.execute_reply":"2022-11-03T07:14:05.951461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(3):\n    plt.subplot(2,3,i+1), plt.imshow(images[i],'gray')\n    plt.title(titles[i])\n    plt.xticks([]), plt.yticks([])\n    ","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:14:08.741018Z","iopub.execute_input":"2022-11-03T07:14:08.74145Z","iopub.status.idle":"2022-11-03T07:14:09.579704Z","shell.execute_reply.started":"2022-11-03T07:14:08.741415Z","shell.execute_reply":"2022-11-03T07:14:09.578091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Image contour**","metadata":{}},{"cell_type":"code","source":"contours, heirarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T05:01:07.379731Z","iopub.execute_input":"2022-11-03T05:01:07.380177Z","iopub.status.idle":"2022-11-03T05:01:07.393233Z","shell.execute_reply.started":"2022-11-03T05:01:07.380127Z","shell.execute_reply":"2022-11-03T05:01:07.391535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"largest = sorted(contours, key=cv2.contourArea)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T05:01:08.849Z","iopub.execute_input":"2022-11-03T05:01:08.849354Z","iopub.status.idle":"2022-11-03T05:01:08.856679Z","shell.execute_reply.started":"2022-11-03T05:01:08.849328Z","shell.execute_reply":"2022-11-03T05:01:08.855217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = np.zeros(img_gray.shape, np.uint8)\nmask","metadata":{"execution":{"iopub.status.busy":"2022-11-03T05:01:12.476865Z","iopub.execute_input":"2022-11-03T05:01:12.477222Z","iopub.status.idle":"2022-11-03T05:01:12.483913Z","shell.execute_reply.started":"2022-11-03T05:01:12.477196Z","shell.execute_reply":"2022-11-03T05:01:12.482667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_contours = cv2.drawContours(mask,[largest[-1]],0,(255,255,255,255),-1)\nplt.imshow(img_contours)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T05:01:20.104527Z","iopub.execute_input":"2022-11-03T05:01:20.104883Z","iopub.status.idle":"2022-11-03T05:01:20.545296Z","shell.execute_reply.started":"2022-11-03T05:01:20.104855Z","shell.execute_reply":"2022-11-03T05:01:20.543765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titles = ['original image','masked_image']\nimages = [img_gray, img_contours]\nfor i in range(2):\n    plt.subplot(2,3,i+1), plt.imshow(images[i],'gray')\n    plt.title(titles[i])\n    plt.xticks([]), plt.yticks([])\n    ","metadata":{"execution":{"iopub.status.busy":"2022-11-03T05:01:29.336266Z","iopub.execute_input":"2022-11-03T05:01:29.337217Z","iopub.status.idle":"2022-11-03T05:01:29.905015Z","shell.execute_reply.started":"2022-11-03T05:01:29.337148Z","shell.execute_reply":"2022-11-03T05:01:29.903804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"adapt_thresh = cv2.adaptiveThreshold(img_gray,100,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,15,2)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T05:01:34.360305Z","iopub.execute_input":"2022-11-03T05:01:34.360756Z","iopub.status.idle":"2022-11-03T05:01:34.388012Z","shell.execute_reply.started":"2022-11-03T05:01:34.36072Z","shell.execute_reply":"2022-11-03T05:01:34.386931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titles = ['original image','aaptive_masked_image']\nimages = [img_gray, adapt_thresh]\nfor i in range(2):\n    plt.subplot(2,3,i+1), plt.imshow(images[i],'gray')\n    plt.title(titles[i])\n    plt.xticks([]), plt.yticks([])\n    ","metadata":{"execution":{"iopub.status.busy":"2022-11-03T05:01:38.870648Z","iopub.execute_input":"2022-11-03T05:01:38.871266Z","iopub.status.idle":"2022-11-03T05:01:39.380974Z","shell.execute_reply.started":"2022-11-03T05:01:38.871237Z","shell.execute_reply":"2022-11-03T05:01:39.378315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"contours, heirarchy = cv2.findContours(adapt_thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\nlargest = sorted(contours, key=cv2.contourArea)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T05:01:43.411287Z","iopub.execute_input":"2022-11-03T05:01:43.411689Z","iopub.status.idle":"2022-11-03T05:01:43.442171Z","shell.execute_reply.started":"2022-11-03T05:01:43.411657Z","shell.execute_reply":"2022-11-03T05:01:43.440458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = np.zeros(img_gray.shape, np.uint8)\nimg_contours = cv2.drawContours(mask,[largest[-1]],0,(255,255,255,255),-1)\nplt.imshow(img_contours)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T05:02:09.792445Z","iopub.execute_input":"2022-11-03T05:02:09.792834Z","iopub.status.idle":"2022-11-03T05:02:10.279047Z","shell.execute_reply.started":"2022-11-03T05:02:09.792805Z","shell.execute_reply":"2022-11-03T05:02:10.277785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nds = tf.data.Dataset.from_tensor_slices((df3['img'].values, df3['mask'].values))","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:14:26.719173Z","iopub.execute_input":"2022-11-03T07:14:26.719624Z","iopub.status.idle":"2022-11-03T07:14:32.517432Z","shell.execute_reply.started":"2022-11-03T07:14:26.719586Z","shell.execute_reply":"2022-11-03T07:14:32.516254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_img(img_name, mask_name):\n    img = tf.io.read_file(img_name)\n    img = tf.io.decode_image(img)\n    mask = tf.io.read_file(mask_name)\n    mask = tf.io.decode_image(mask)\n    mask = tf.squeeze(mask)\n    mask = mask[:,:,:1]\n    \n    return img, mask","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:14:37.463198Z","iopub.execute_input":"2022-11-03T07:14:37.464042Z","iopub.status.idle":"2022-11-03T07:14:37.471084Z","shell.execute_reply.started":"2022-11-03T07:14:37.464001Z","shell.execute_reply":"2022-11-03T07:14:37.469839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = ds.map(load_img, tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:14:40.004803Z","iopub.execute_input":"2022-11-03T07:14:40.005619Z","iopub.status.idle":"2022-11-03T07:14:40.131621Z","shell.execute_reply.started":"2022-11-03T07:14:40.005574Z","shell.execute_reply":"2022-11-03T07:14:40.130429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\none = ds.take(5)\nfor a, b in one:\n    plt.imshow(a)\n    plt.show()\n    plt.imshow(b)\n    plt.show()\n    print(a.shape)\n    print(b.shape)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:14:42.456271Z","iopub.execute_input":"2022-11-03T07:14:42.456689Z","iopub.status.idle":"2022-11-03T07:14:47.471009Z","shell.execute_reply.started":"2022-11-03T07:14:42.456651Z","shell.execute_reply":"2022-11-03T07:14:47.470102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:15:23.92769Z","iopub.execute_input":"2022-11-03T07:15:23.928148Z","iopub.status.idle":"2022-11-03T07:15:23.93429Z","shell.execute_reply.started":"2022-11-03T07:15:23.92811Z","shell.execute_reply":"2022-11-03T07:15:23.932905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(df3['img'][100])\nimg_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nplt.imshow(img_rgb)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:15:26.714442Z","iopub.execute_input":"2022-11-03T07:15:26.714834Z","iopub.status.idle":"2022-11-03T07:15:27.413574Z","shell.execute_reply.started":"2022-11-03T07:15:26.714803Z","shell.execute_reply":"2022-11-03T07:15:27.412243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rectangle = (0,0,300,380)\n\nmask = np.zeros(img_rgb.shape[:2], np.uint8)\n\nbgdModel = np.zeros((1, 65), np.float64)\nfgdModel = np.zeros((1, 65), np.float64)\n\ncv2.grabCut(img, mask, rectangle, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:15:32.433623Z","iopub.execute_input":"2022-11-03T07:15:32.434024Z","iopub.status.idle":"2022-11-03T07:15:38.725257Z","shell.execute_reply.started":"2022-11-03T07:15:32.433989Z","shell.execute_reply":"2022-11-03T07:15:38.724084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_2 = np.where((mask == 2)| (mask == 0), 0, 1).astype('uint8')","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:15:50.023962Z","iopub.execute_input":"2022-11-03T07:15:50.024533Z","iopub.status.idle":"2022-11-03T07:15:50.036039Z","shell.execute_reply.started":"2022-11-03T07:15:50.024492Z","shell.execute_reply":"2022-11-03T07:15:50.034744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_rgb_nobg = img * mask_2[:,:,np.newaxis]","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:15:57.069394Z","iopub.execute_input":"2022-11-03T07:15:57.069833Z","iopub.status.idle":"2022-11-03T07:15:57.102826Z","shell.execute_reply.started":"2022-11-03T07:15:57.069797Z","shell.execute_reply":"2022-11-03T07:15:57.101388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img_rgb_nobg)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:16:07.826141Z","iopub.execute_input":"2022-11-03T07:16:07.826563Z","iopub.status.idle":"2022-11-03T07:16:08.23796Z","shell.execute_reply.started":"2022-11-03T07:16:07.826531Z","shell.execute_reply":"2022-11-03T07:16:08.236548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport cv2 as cv\nfrom matplotlib import pyplot as plt\nimport sys\nfrom  PIL  import Image","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:35:37.575737Z","iopub.execute_input":"2022-11-03T07:35:37.576201Z","iopub.status.idle":"2022-11-03T07:35:37.582431Z","shell.execute_reply.started":"2022-11-03T07:35:37.576159Z","shell.execute_reply":"2022-11-03T07:35:37.581167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(df3['img'][2])\noriginal = img.copy()\nl = int(max(5, 6))\nu = int(min(6, 6))","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:35:43.560967Z","iopub.execute_input":"2022-11-03T07:35:43.561384Z","iopub.status.idle":"2022-11-03T07:35:43.59623Z","shell.execute_reply.started":"2022-11-03T07:35:43.561352Z","shell.execute_reply":"2022-11-03T07:35:43.595007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ed = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\nedges = cv.GaussianBlur(img, (21, 51), 3)\nedges = cv.cvtColor(edges, cv.COLOR_BGR2GRAY)\nedges = cv.Canny(edges, l, u)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:35:50.043912Z","iopub.execute_input":"2022-11-03T07:35:50.044328Z","iopub.status.idle":"2022-11-03T07:35:50.096861Z","shell.execute_reply.started":"2022-11-03T07:35:50.044295Z","shell.execute_reply":"2022-11-03T07:35:50.095909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, thresh = cv.threshold(edges, 0, 255, cv.THRESH_BINARY  + cv.THRESH_OTSU)\nkernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5, 5))\nmask = cv.morphologyEx(thresh, cv.MORPH_CLOSE, kernel, iterations=4)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:36:48.901287Z","iopub.execute_input":"2022-11-03T07:36:48.90173Z","iopub.status.idle":"2022-11-03T07:36:48.928021Z","shell.execute_reply.started":"2022-11-03T07:36:48.901683Z","shell.execute_reply":"2022-11-03T07:36:48.926592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = mask.tolist()\nsys.setrecursionlimit(10**8)\nfor i in  range(len(data)):\n    for j in  range(len(data[i])):\n        if data[i][j] !=  255:\n            data[i][j] =  -1\n        else:\n            break\n    for j in  range(len(data[i])-1, -1, -1):\n        if data[i][j] !=  255:\n            data[i][j] =  -1\n        else:\n            break","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:36:51.158134Z","iopub.execute_input":"2022-11-03T07:36:51.158561Z","iopub.status.idle":"2022-11-03T07:36:51.596594Z","shell.execute_reply.started":"2022-11-03T07:36:51.158525Z","shell.execute_reply":"2022-11-03T07:36:51.595447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = np.array(data)\nimage[image !=  -1] =  255\nimage[image ==  -1] =  0\n\nmask = np.array(image, np.uint8)\n\nresult = cv.bitwise_and(original, original, mask=mask)\nresult[mask ==  0] =  255\ncv.imwrite('bg.png', result)\n\nimg = Image.open('bg.png')\nimg.convert(\"RGBA\")\ndatas = img.getdata()","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:36:54.068908Z","iopub.execute_input":"2022-11-03T07:36:54.069324Z","iopub.status.idle":"2022-11-03T07:36:54.435332Z","shell.execute_reply.started":"2022-11-03T07:36:54.069291Z","shell.execute_reply":"2022-11-03T07:36:54.434282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newData = []\nfor item in datas:\n    if item[0] ==  255  and item[1] ==  255  and item[2] ==  255:\n        newData.append((255, 255, 255, 0))\n    else:\n        newData.append(item)\n\nimg.putdata(newData)\nimg.save(\"img.png\", \"PNG\")","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:36:58.714646Z","iopub.execute_input":"2022-11-03T07:36:58.715364Z","iopub.status.idle":"2022-11-03T07:37:00.215229Z","shell.execute_reply.started":"2022-11-03T07:36:58.715324Z","shell.execute_reply":"2022-11-03T07:37:00.21419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimg = cv2.imread(df3['img'][10])\n#colors = img.convert('RGB').getcolors() #this converts the mode to RGB","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:39:11.507225Z","iopub.execute_input":"2022-11-03T07:39:11.508256Z","iopub.status.idle":"2022-11-03T07:39:11.539654Z","shell.execute_reply.started":"2022-11-03T07:39:11.508216Z","shell.execute_reply":"2022-11-03T07:39:11.538365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(img)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:39:13.818985Z","iopub.execute_input":"2022-11-03T07:39:13.819391Z","iopub.status.idle":"2022-11-03T07:39:13.825505Z","shell.execute_reply.started":"2022-11-03T07:39:13.819358Z","shell.execute_reply":"2022-11-03T07:39:13.824672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,8))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:39:16.507514Z","iopub.execute_input":"2022-11-03T07:39:16.508471Z","iopub.status.idle":"2022-11-03T07:39:17.268304Z","shell.execute_reply.started":"2022-11-03T07:39:16.508425Z","shell.execute_reply":"2022-11-03T07:39:17.266756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:39:23.830141Z","iopub.execute_input":"2022-11-03T07:39:23.830604Z","iopub.status.idle":"2022-11-03T07:39:23.837248Z","shell.execute_reply.started":"2022-11-03T07:39:23.830568Z","shell.execute_reply":"2022-11-03T07:39:23.836228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,8))\nplt.imshow(img_rgb)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:39:32.478414Z","iopub.execute_input":"2022-11-03T07:39:32.478802Z","iopub.status.idle":"2022-11-03T07:39:33.237507Z","shell.execute_reply.started":"2022-11-03T07:39:32.478771Z","shell.execute_reply":"2022-11-03T07:39:33.23609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_hsv = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2HSV)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:39:36.745148Z","iopub.execute_input":"2022-11-03T07:39:36.74599Z","iopub.status.idle":"2022-11-03T07:39:36.752866Z","shell.execute_reply.started":"2022-11-03T07:39:36.745951Z","shell.execute_reply":"2022-11-03T07:39:36.751521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lower = np.array([0, 0, 50])\nupper = np.array([20, 20, 255])","metadata":{"execution":{"iopub.status.busy":"2022-11-03T08:15:43.827564Z","iopub.execute_input":"2022-11-03T08:15:43.828458Z","iopub.status.idle":"2022-11-03T08:15:43.832879Z","shell.execute_reply.started":"2022-11-03T08:15:43.82842Z","shell.execute_reply":"2022-11-03T08:15:43.83203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = cv2.inRange(grid_hsv, lower, upper)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T08:15:53.946598Z","iopub.execute_input":"2022-11-03T08:15:53.947283Z","iopub.status.idle":"2022-11-03T08:15:53.955478Z","shell.execute_reply.started":"2022-11-03T08:15:53.947241Z","shell.execute_reply":"2022-11-03T08:15:53.954133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,8))\nplt.imshow(mask)\nprint(mask.shape)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T08:58:15.225137Z","iopub.execute_input":"2022-11-03T08:58:15.225581Z","iopub.status.idle":"2022-11-03T08:58:15.820334Z","shell.execute_reply.started":"2022-11-03T08:58:15.225543Z","shell.execute_reply":"2022-11-03T08:58:15.819186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = cv2.bitwise_and(img_rgb, img_rgb, mask=mask)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T08:26:37.735991Z","iopub.execute_input":"2022-11-03T08:26:37.737043Z","iopub.status.idle":"2022-11-03T08:26:37.745883Z","shell.execute_reply.started":"2022-11-03T08:26:37.737002Z","shell.execute_reply":"2022-11-03T08:26:37.744604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,8))\nplt.imshow(res)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T08:26:39.744973Z","iopub.execute_input":"2022-11-03T08:26:39.745587Z","iopub.status.idle":"2022-11-03T08:26:40.451278Z","shell.execute_reply.started":"2022-11-03T08:26:39.745551Z","shell.execute_reply":"2022-11-03T08:26:40.450088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport matplotlib.image as mpimg\n\nfrom PIL import Image\nfrom matplotlib.offsetbox import OffsetImage, AnnotationBbox","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:19:01.060345Z","iopub.execute_input":"2022-11-03T07:19:01.060752Z","iopub.status.idle":"2022-11-03T07:19:01.067455Z","shell.execute_reply.started":"2022-11-03T07:19:01.060721Z","shell.execute_reply":"2022-11-03T07:19:01.065971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install easydev                 \n!pip install colormap                \n!pip install opencv-python          \n!pip install colorgram.py            \n!pip install extcolors ","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:19:03.064642Z","iopub.execute_input":"2022-11-03T07:19:03.065173Z","iopub.status.idle":"2022-11-03T07:20:04.977541Z","shell.execute_reply.started":"2022-11-03T07:19:03.065136Z","shell.execute_reply":"2022-11-03T07:20:04.976356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport extcolors\n\nfrom colormap import rgb2hex","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:23:08.422386Z","iopub.execute_input":"2022-11-03T07:23:08.422892Z","iopub.status.idle":"2022-11-03T07:23:08.668129Z","shell.execute_reply.started":"2022-11-03T07:23:08.42285Z","shell.execute_reply":"2022-11-03T07:23:08.666886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors_x = extcolors.extract_from_path(df3['img'][0], tolerance = 0, limit = 500)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:25:47.1466Z","iopub.execute_input":"2022-11-03T07:25:47.147039Z","iopub.status.idle":"2022-11-03T07:25:49.720624Z","shell.execute_reply.started":"2022-11-03T07:25:47.147001Z","shell.execute_reply":"2022-11-03T07:25:49.719336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(df3['mask'][0])","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:25:49.722673Z","iopub.execute_input":"2022-11-03T07:25:49.723157Z","iopub.status.idle":"2022-11-03T07:25:49.729755Z","shell.execute_reply.started":"2022-11-03T07:25:49.723111Z","shell.execute_reply":"2022-11-03T07:25:49.728727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:25:52.0971Z","iopub.execute_input":"2022-11-03T07:25:52.097495Z","iopub.status.idle":"2022-11-03T07:25:52.312768Z","shell.execute_reply.started":"2022-11-03T07:25:52.097465Z","shell.execute_reply":"2022-11-03T07:25:52.310065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = cv2.imread(df3['img'][0],0)\nim = cv2.imread(df3['mask'][0])\nmask_inv =  255 - mask;\nfinal_im = mask_inv*im","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:25:56.021788Z","iopub.execute_input":"2022-11-03T07:25:56.023562Z","iopub.status.idle":"2022-11-03T07:25:56.064662Z","shell.execute_reply.started":"2022-11-03T07:25:56.023508Z","shell.execute_reply":"2022-11-03T07:25:56.063002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3","metadata":{"execution":{"iopub.status.busy":"2022-11-03T07:25:57.295794Z","iopub.execute_input":"2022-11-03T07:25:57.29662Z","iopub.status.idle":"2022-11-03T07:25:57.314029Z","shell.execute_reply.started":"2022-11-03T07:25:57.296573Z","shell.execute_reply":"2022-11-03T07:25:57.31263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.image as mpimg\nimport imageio\nmasked_img = imageio.imread(df3['mask'][4])\nplt.imshow(masked_img)\nprint(masked_img.shape)\n\nreal_img = cv2.imread(df3['img'][4])\nplt.imshow(real_img)\nprint(real_img.shape)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T11:07:24.733399Z","iopub.execute_input":"2022-11-03T11:07:24.734424Z","iopub.status.idle":"2022-11-03T11:07:25.783581Z","shell.execute_reply.started":"2022-11-03T11:07:24.734382Z","shell.execute_reply":"2022-11-03T11:07:25.782589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(mask)\nprint(mask.shape)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T09:02:57.828896Z","iopub.execute_input":"2022-11-03T09:02:57.829754Z","iopub.status.idle":"2022-11-03T09:02:58.351252Z","shell.execute_reply.started":"2022-11-03T09:02:57.829707Z","shell.execute_reply":"2022-11-03T09:02:58.349748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masked_img = mpimg.imread(df3['mask'][4])  \nplt.imshow(masked_img)\nprint(masked_img.shape)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T12:19:02.808649Z","iopub.execute_input":"2022-11-03T12:19:02.809059Z","iopub.status.idle":"2022-11-03T12:19:03.376063Z","shell.execute_reply.started":"2022-11-03T12:19:02.809025Z","shell.execute_reply":"2022-11-03T12:19:03.375167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from PIL import Image\n# img = Image.open(df3['mask'][4]).convert(\"RGB\")\n#image = cv2.imread(img)\n#plt.imshow(img)\n# print(img.shape())\n# img.save('train_masks/train_masks/d0dab700c896_14_mask.jpg')\n# extension = str(df3['mask'][4]).split('.')[-1]\n\n# if extension == \"jpg\":\n#     bg_image.save(imgName, \"jpg\")\n# else:\n#     if bg_image.mode in (\"RGBA\", \"P\"):\n#         bg_image = bg_image.convert(\"RGB\")\n#     bg_image.save(imgName, \"JPEG\")\nintersection = np.bitwise_and(real_img, masked_img[:,:,:3])\nplt.imshow(intersection)\nprint(intersection.shape)\n# union = np.logical_or(img1, img)\n# iou_score = np.sum(intersection) / np.sum(union)\n# print(\"IoU is %s\" % iou_score)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T12:53:31.759241Z","iopub.execute_input":"2022-11-03T12:53:31.759649Z","iopub.status.idle":"2022-11-03T12:53:32.418198Z","shell.execute_reply.started":"2022-11-03T12:53:31.759616Z","shell.execute_reply":"2022-11-03T12:53:32.415555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Intersection over union is : 0.30355477739154735, which is 30% accurate","metadata":{}},{"cell_type":"code","source":"colors_x = extcolors.extract_from_path(intersection, tolerance = 0, limit = 500)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T12:50:07.128782Z","iopub.execute_input":"2022-11-03T12:50:07.129502Z","iopub.status.idle":"2022-11-03T12:50:07.15524Z","shell.execute_reply.started":"2022-11-03T12:50:07.129455Z","shell.execute_reply":"2022-11-03T12:50:07.153522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}