{"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-11T10:49:12.590446Z","iopub.execute_input":"2022-11-11T10:49:12.590827Z","iopub.status.idle":"2022-11-11T10:49:12.599859Z","shell.execute_reply.started":"2022-11-11T10:49:12.590792Z","shell.execute_reply":"2022-11-11T10:49:12.598419Z"},"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-11T10:49:21.332424Z","iopub.execute_input":"2022-11-11T10:49:21.332769Z","iopub.status.idle":"2022-11-11T10:49:21.359174Z","shell.execute_reply.started":"2022-11-11T10:49:21.332739Z","shell.execute_reply":"2022-11-11T10:49:21.358144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:49:22.896415Z","iopub.execute_input":"2022-11-11T10:49:22.896768Z","iopub.status.idle":"2022-11-11T10:49:22.901653Z","shell.execute_reply.started":"2022-11-11T10:49:22.896738Z","shell.execute_reply":"2022-11-11T10:49:22.900551Z"},"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-11T10:49:23.960172Z","iopub.execute_input":"2022-11-11T10:49:23.960556Z","iopub.status.idle":"2022-11-11T10:49:24.73973Z","shell.execute_reply.started":"2022-11-11T10:49:23.960525Z","shell.execute_reply":"2022-11-11T10:49:24.738743Z"},"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-11T10:49:25.227839Z","iopub.execute_input":"2022-11-11T10:49:25.228222Z","iopub.status.idle":"2022-11-11T10:49:36.994269Z","shell.execute_reply.started":"2022-11-11T10:49:25.22819Z","shell.execute_reply":"2022-11-11T10:49:36.992961Z"},"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-11T10:49:36.996512Z","iopub.execute_input":"2022-11-11T10:49:36.997253Z","iopub.status.idle":"2022-11-11T10:49:38.845068Z","shell.execute_reply.started":"2022-11-11T10:49:36.99721Z","shell.execute_reply":"2022-11-11T10:49:38.843731Z"},"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-11T05:35:21.405531Z","iopub.execute_input":"2022-11-11T05:35:21.405969Z","iopub.status.idle":"2022-11-11T05:35:21.418799Z","shell.execute_reply.started":"2022-11-11T05:35:21.405931Z","shell.execute_reply":"2022-11-11T05:35:21.417717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_imgs)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:35:56.607519Z","iopub.execute_input":"2022-11-11T05:35:56.60795Z","iopub.status.idle":"2022-11-11T05:35:56.614368Z","shell.execute_reply.started":"2022-11-11T05:35:56.60792Z","shell.execute_reply":"2022-11-11T05:35:56.613416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_masks_imgs)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:15:50.298175Z","iopub.execute_input":"2022-11-11T05:15:50.298536Z","iopub.status.idle":"2022-11-11T05:15:50.30563Z","shell.execute_reply.started":"2022-11-11T05:15:50.298501Z","shell.execute_reply":"2022-11-11T05:15:50.304685Z"},"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-11T05:38:30.554436Z","iopub.execute_input":"2022-11-11T05:38:30.55536Z","iopub.status.idle":"2022-11-11T05:38:30.570247Z","shell.execute_reply.started":"2022-11-11T05:38:30.555313Z","shell.execute_reply":"2022-11-11T05:38:30.569301Z"},"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-11T05:38:33.279239Z","iopub.execute_input":"2022-11-11T05:38:33.279606Z","iopub.status.idle":"2022-11-11T05:38:33.290088Z","shell.execute_reply.started":"2022-11-11T05:38:33.279574Z","shell.execute_reply":"2022-11-11T05:38:33.289088Z"},"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-11T05:38:42.27121Z","iopub.execute_input":"2022-11-11T05:38:42.271578Z","iopub.status.idle":"2022-11-11T05:38:42.288973Z","shell.execute_reply.started":"2022-11-11T05:38:42.271546Z","shell.execute_reply":"2022-11-11T05:38:42.287922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df1)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:38:43.538379Z","iopub.execute_input":"2022-11-11T05:38:43.539073Z","iopub.status.idle":"2022-11-11T05:38:43.557486Z","shell.execute_reply.started":"2022-11-11T05:38:43.539037Z","shell.execute_reply":"2022-11-11T05:38:43.556479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3_n = pd.merge(df1, df2, on= 'id')","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:38:44.377365Z","iopub.execute_input":"2022-11-11T05:38:44.378347Z","iopub.status.idle":"2022-11-11T05:38:44.418295Z","shell.execute_reply.started":"2022-11-11T05:38:44.3783Z","shell.execute_reply":"2022-11-11T05:38:44.417423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3 = df3_n.iloc[0:100]\ndf3","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:38:45.301572Z","iopub.execute_input":"2022-11-11T05:38:45.302535Z","iopub.status.idle":"2022-11-11T05:38:45.31991Z","shell.execute_reply.started":"2022-11-11T05:38:45.302492Z","shell.execute_reply":"2022-11-11T05:38:45.318897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n!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-11T05:38:46.243802Z","iopub.execute_input":"2022-11-11T05:38:46.244158Z","iopub.status.idle":"2022-11-11T05:39:40.61995Z","shell.execute_reply.started":"2022-11-11T05:38:46.244126Z","shell.execute_reply":"2022-11-11T05:39:40.61874Z"},"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-11T05:39:40.624089Z","iopub.execute_input":"2022-11-11T05:39:40.624439Z","iopub.status.idle":"2022-11-11T05:39:41.272069Z","shell.execute_reply.started":"2022-11-11T05:39:40.624393Z","shell.execute_reply":"2022-11-11T05:39:41.270992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors_x = extcolors.extract_from_path(df3['img'][0], tolerance = 0, limit = 100)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:40:55.736155Z","iopub.execute_input":"2022-11-11T05:40:55.736564Z","iopub.status.idle":"2022-11-11T05:40:58.391339Z","shell.execute_reply.started":"2022-11-11T05:40:55.736528Z","shell.execute_reply":"2022-11-11T05:40:58.390272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(df3['mask'][2])","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:40:59.975805Z","iopub.execute_input":"2022-11-11T05:40:59.976382Z","iopub.status.idle":"2022-11-11T05:40:59.994528Z","shell.execute_reply.started":"2022-11-11T05:40:59.976342Z","shell.execute_reply":"2022-11-11T05:40:59.992997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:41:00.797175Z","iopub.execute_input":"2022-11-11T05:41:00.797577Z","iopub.status.idle":"2022-11-11T05:41:00.80248Z","shell.execute_reply.started":"2022-11-11T05:41:00.797533Z","shell.execute_reply":"2022-11-11T05:41:00.801168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#\nimport matplotlib.image as mpimg\nimport imageio\n\nreal_img = cv2.imread(df3['img'][4])\nplt.imshow(real_img)\nprint(real_img.shape)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:49:02.646055Z","iopub.execute_input":"2022-11-11T10:49:02.646423Z","iopub.status.idle":"2022-11-11T10:49:02.749453Z","shell.execute_reply.started":"2022-11-11T10:49:02.646392Z","shell.execute_reply":"2022-11-11T10:49:02.748059Z"},"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-11T05:41:02.17078Z","iopub.execute_input":"2022-11-11T05:41:02.171095Z","iopub.status.idle":"2022-11-11T05:41:02.705392Z","shell.execute_reply.started":"2022-11-11T05:41:02.171065Z","shell.execute_reply":"2022-11-11T05:41:02.704358Z"},"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-11T05:41:02.775139Z","iopub.execute_input":"2022-11-11T05:41:02.775848Z","iopub.status.idle":"2022-11-11T05:41:03.380361Z","shell.execute_reply.started":"2022-11-11T05:41:02.775805Z","shell.execute_reply":"2022-11-11T05:41:03.379352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# colors_x = extcolors.extract_from_path(df3['masked_img'][3], tolerance = 0, limit = 100)\n# colors_x","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:41:03.451909Z","iopub.execute_input":"2022-11-11T05:41:03.452571Z","iopub.status.idle":"2022-11-11T05:41:03.457807Z","shell.execute_reply.started":"2022-11-11T05:41:03.452527Z","shell.execute_reply":"2022-11-11T05:41:03.456274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n# real_img = cv2.imread(df3['img'][3])\n# masked_img = mpimg.imread(df3['mask'][3])\n# print(real_img.shape, masked_img.shape)\nbitwise_and_image = []\nfor i in range(100):\n    real_img = cv2.imread(df3['img'][i])\n    masked_img = mpimg.imread(df3['mask'][i])\n    intersection = cv2.bitwise_and(real_img, masked_img[:,:,:3])\n    cv2.imwrite('masked_bitwise_img'+str(i)+'.jpg', intersection)\n#     df3['intersection_path'][i] = 'masked_bitwise_img'+str(i)+'.jpg'\n    bitwise_and_image.append('masked_bitwise_img'+str(i)+'.jpg')\nprint('done')","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:41:04.355292Z","iopub.execute_input":"2022-11-11T05:41:04.356017Z","iopub.status.idle":"2022-11-11T05:41:13.989789Z","shell.execute_reply.started":"2022-11-11T05:41:04.35598Z","shell.execute_reply":"2022-11-11T05:41:13.98871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# bitwise_and_image","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:42:45.005257Z","iopub.execute_input":"2022-11-11T05:42:45.00577Z","iopub.status.idle":"2022-11-11T05:42:45.013425Z","shell.execute_reply.started":"2022-11-11T05:42:45.005725Z","shell.execute_reply":"2022-11-11T05:42:45.012313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df3","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:42:49.430219Z","iopub.execute_input":"2022-11-11T05:42:49.430592Z","iopub.status.idle":"2022-11-11T05:42:49.435334Z","shell.execute_reply.started":"2022-11-11T05:42:49.430559Z","shell.execute_reply":"2022-11-11T05:42:49.434369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3['bitwise_and_image'] = bitwise_and_image\n","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:42:50.358983Z","iopub.execute_input":"2022-11-11T05:42:50.359354Z","iopub.status.idle":"2022-11-11T05:42:50.368435Z","shell.execute_reply.started":"2022-11-11T05:42:50.359321Z","shell.execute_reply":"2022-11-11T05:42:50.367114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(df3['bitwise_and_image'][3])\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:42:50.913852Z","iopub.execute_input":"2022-11-11T05:42:50.915003Z","iopub.status.idle":"2022-11-11T05:42:51.528386Z","shell.execute_reply.started":"2022-11-11T05:42:50.91496Z","shell.execute_reply":"2022-11-11T05:42:51.527337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors_x = extcolors.extract_from_path(df3['bitwise_and_image'][0], tolerance = 0, limit = 100)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:42:51.921963Z","iopub.execute_input":"2022-11-11T05:42:51.922906Z","iopub.status.idle":"2022-11-11T05:42:53.961665Z","shell.execute_reply.started":"2022-11-11T05:42:51.922861Z","shell.execute_reply":"2022-11-11T05:42:53.960658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"white_black = 0 \nfor t in colors_x[0]:\n    if t[0] == (255,255,255) or t[0] == (0,0,0):\n        white_black += t[1]\n# Reconstruct the colors_x tuple\ncolors_x = ([t for t in colors_x[0] if t[0] != (255,255,255) and t[0] != (0,0,0)], colors_x[1] - white_black)\nprint(colors_x)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:42:53.963723Z","iopub.execute_input":"2022-11-11T05:42:53.964081Z","iopub.status.idle":"2022-11-11T05:42:53.971385Z","shell.execute_reply.started":"2022-11-11T05:42:53.964047Z","shell.execute_reply":"2022-11-11T05:42:53.970179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"per_list=[]\ndef percentage(i,j):\n    per = 100 * i / colors_x[1]\n    print(\"percentage of \",i)\n    print(\"---------------------\",per)\n    per_list.append(per)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:42:55.663023Z","iopub.execute_input":"2022-11-11T05:42:55.664137Z","iopub.status.idle":"2022-11-11T05:42:55.670391Z","shell.execute_reply.started":"2022-11-11T05:42:55.664077Z","shell.execute_reply":"2022-11-11T05:42:55.669049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"value_list = []\nfor i in range(99):\n    value = colors_x[0][i][1]\n    RGB=colors_x[0][i][0]\n    value_list.append(RGB)\n    percentage(value,colors_x[0][i][1])\n    print(value,'done')\nvalue_list","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:17.57826Z","iopub.execute_input":"2022-11-11T05:43:17.579226Z","iopub.status.idle":"2022-11-11T05:43:17.602808Z","shell.execute_reply.started":"2022-11-11T05:43:17.579174Z","shell.execute_reply":"2022-11-11T05:43:17.601895Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colorsbackground=pd.DataFrame(list(zip(value_list, per_list)),columns=['RGB', 'Foreground'])","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:27.113718Z","iopub.execute_input":"2022-11-11T05:43:27.114083Z","iopub.status.idle":"2022-11-11T05:43:27.119733Z","shell.execute_reply.started":"2022-11-11T05:43:27.114052Z","shell.execute_reply":"2022-11-11T05:43:27.118684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colorsbackground","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:29.055963Z","iopub.execute_input":"2022-11-11T05:43:29.056339Z","iopub.status.idle":"2022-11-11T05:43:29.074553Z","shell.execute_reply.started":"2022-11-11T05:43:29.056305Z","shell.execute_reply":"2022-11-11T05:43:29.073479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# colors_x[0][1][0]","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:32.792166Z","iopub.execute_input":"2022-11-11T05:43:32.792574Z","iopub.status.idle":"2022-11-11T05:43:32.797079Z","shell.execute_reply.started":"2022-11-11T05:43:32.79254Z","shell.execute_reply":"2022-11-11T05:43:32.796048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors_x_str = list(colors_x)\ntype(colors_x_str)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:33.725253Z","iopub.execute_input":"2022-11-11T05:43:33.726505Z","iopub.status.idle":"2022-11-11T05:43:33.733527Z","shell.execute_reply.started":"2022-11-11T05:43:33.726458Z","shell.execute_reply":"2022-11-11T05:43:33.732581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow([[colors_x[0][i][0] for i in range (99)]])\nplt.axis(\"off\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:34.697096Z","iopub.execute_input":"2022-11-11T05:43:34.697479Z","iopub.status.idle":"2022-11-11T05:43:34.767047Z","shell.execute_reply.started":"2022-11-11T05:43:34.697445Z","shell.execute_reply":"2022-11-11T05:43:34.765684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors_X_value=[]\ncolors_Y_value=[]\n\nfor i in range (99):\n    colors_X_value = colors_x[0][i][0]\n    print(colors_X_value)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:35.33194Z","iopub.execute_input":"2022-11-11T05:43:35.332287Z","iopub.status.idle":"2022-11-11T05:43:35.339182Z","shell.execute_reply.started":"2022-11-11T05:43:35.332256Z","shell.execute_reply":"2022-11-11T05:43:35.338073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for j in range (99):\n    X=colors_x[0][j][1]\n    colors_Y_value.append(X)\nprint(colors_Y_value)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:36.381281Z","iopub.execute_input":"2022-11-11T05:43:36.382001Z","iopub.status.idle":"2022-11-11T05:43:36.387935Z","shell.execute_reply.started":"2022-11-11T05:43:36.381962Z","shell.execute_reply":"2022-11-11T05:43:36.386741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# for not opraction\n# for only background","metadata":{}},{"cell_type":"code","source":"bitwise_not_and_image = []\nfor i in range(100):\n    image_not_masked = mpimg.imread(df3['mask'][i])\n#     masked_img = mpimg.imread(df3['mask'][i])\n    bitwiseNot = cv2.bitwise_not(image_not_masked)\n    real_img = cv2.imread(df3['img'][i])\n\n    intersection = cv2.bitwise_and(real_img, bitwiseNot[:,:,:3])\n    cv2.imwrite('bitwise_not_and_image'+str(i)+'.jpg', intersection)\n#     df3['intersection_path'][i] = 'masked_bitwise_img'+str(i)+'.jpg'\n    bitwise_not_and_image.append('bitwise_not_and_image'+str(i)+'.jpg')\nprint('done')","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:40.904364Z","iopub.execute_input":"2022-11-11T05:43:40.905123Z","iopub.status.idle":"2022-11-11T05:43:50.564477Z","shell.execute_reply.started":"2022-11-11T05:43:40.905082Z","shell.execute_reply":"2022-11-11T05:43:50.563271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3['bitwise_not_and_image'] = bitwise_not_and_image","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:50.566645Z","iopub.execute_input":"2022-11-11T05:43:50.567036Z","iopub.status.idle":"2022-11-11T05:43:50.573188Z","shell.execute_reply.started":"2022-11-11T05:43:50.566998Z","shell.execute_reply":"2022-11-11T05:43:50.571933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = mpimg.imread(df3['bitwise_not_and_image'][0])\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:51.2113Z","iopub.execute_input":"2022-11-11T05:43:51.212041Z","iopub.status.idle":"2022-11-11T05:43:51.866497Z","shell.execute_reply.started":"2022-11-11T05:43:51.212002Z","shell.execute_reply":"2022-11-11T05:43:51.863686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors_obj_only = extcolors.extract_from_path(df3['bitwise_not_and_image'][0], tolerance = 0, limit = 100)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:52.67372Z","iopub.execute_input":"2022-11-11T05:43:52.674077Z","iopub.status.idle":"2022-11-11T05:43:54.548597Z","shell.execute_reply.started":"2022-11-11T05:43:52.674048Z","shell.execute_reply":"2022-11-11T05:43:54.547479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"white_black = 0 \nfor t in colors_obj_only[0]:\n    if t[0] == (255,255,255) or t[0] == (0,0,0):\n        white_black += t[1]\n# Reconstruct the colors_x tuple\ncolors_obj_only = ([t for t in colors_obj_only[0] if t[0] != (255,255,255) and t[0] != (0,0,0)], colors_obj_only[1] - white_black)\nprint(colors_obj_only)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:54.550703Z","iopub.execute_input":"2022-11-11T05:43:54.55109Z","iopub.status.idle":"2022-11-11T05:43:54.558243Z","shell.execute_reply.started":"2022-11-11T05:43:54.551053Z","shell.execute_reply":"2022-11-11T05:43:54.557184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors_obj_only","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:55.897479Z","iopub.execute_input":"2022-11-11T05:43:55.89791Z","iopub.status.idle":"2022-11-11T05:43:55.923952Z","shell.execute_reply.started":"2022-11-11T05:43:55.897873Z","shell.execute_reply":"2022-11-11T05:43:55.923129Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"per_list=[]\ndef percentage(i,j):\n    per = 100 * i / colors_obj_only[1]\n    print(\"percentage of \",i)\n    print(\"---------------------\",per)\n    per_list.append(per)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:43:56.726693Z","iopub.execute_input":"2022-11-11T05:43:56.727046Z","iopub.status.idle":"2022-11-11T05:43:56.733514Z","shell.execute_reply.started":"2022-11-11T05:43:56.727015Z","shell.execute_reply":"2022-11-11T05:43:56.732451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"value_list = []\nfor i in range(99):\n    value = colors_obj_only[0][i][1]\n    RGB=colors_obj_only[0][i][0]\n    value_list.append(RGB)\n    percentage(value,colors_obj_only[0][i][1])\n    print(value,'done')\nvalue_list","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:44:06.848362Z","iopub.execute_input":"2022-11-11T05:44:06.848847Z","iopub.status.idle":"2022-11-11T05:44:06.888021Z","shell.execute_reply.started":"2022-11-11T05:44:06.848803Z","shell.execute_reply":"2022-11-11T05:44:06.886452Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"value_list","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:44:07.86344Z","iopub.execute_input":"2022-11-11T05:44:07.864593Z","iopub.status.idle":"2022-11-11T05:44:07.878101Z","shell.execute_reply.started":"2022-11-11T05:44:07.864548Z","shell.execute_reply":"2022-11-11T05:44:07.877015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors_obj_only[1]","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:44:08.725298Z","iopub.execute_input":"2022-11-11T05:44:08.72593Z","iopub.status.idle":"2022-11-11T05:44:08.733438Z","shell.execute_reply.started":"2022-11-11T05:44:08.725894Z","shell.execute_reply":"2022-11-11T05:44:08.732325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colorsobjonly=pd.DataFrame(list(zip(value_list, per_list)),columns=['RGB', 'background'])","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:44:09.509308Z","iopub.execute_input":"2022-11-11T05:44:09.51017Z","iopub.status.idle":"2022-11-11T05:44:09.515987Z","shell.execute_reply.started":"2022-11-11T05:44:09.510115Z","shell.execute_reply":"2022-11-11T05:44:09.514871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colorsobjonly","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:44:10.529299Z","iopub.execute_input":"2022-11-11T05:44:10.531946Z","iopub.status.idle":"2022-11-11T05:44:10.553008Z","shell.execute_reply.started":"2022-11-11T05:44:10.531859Z","shell.execute_reply":"2022-11-11T05:44:10.551549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colorsbackground","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:44:11.475165Z","iopub.execute_input":"2022-11-11T05:44:11.475593Z","iopub.status.idle":"2022-11-11T05:44:11.493178Z","shell.execute_reply.started":"2022-11-11T05:44:11.475558Z","shell.execute_reply":"2022-11-11T05:44:11.492088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RGB_compare= pd.merge(colorsbackground, colorsobjonly, on='RGB',how=\"left\")","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:44:12.255236Z","iopub.execute_input":"2022-11-11T05:44:12.255657Z","iopub.status.idle":"2022-11-11T05:44:12.270067Z","shell.execute_reply.started":"2022-11-11T05:44:12.255619Z","shell.execute_reply":"2022-11-11T05:44:12.268858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RGB_compare\nprint(RGB_compare.to_string())\n","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:44:13.260345Z","iopub.execute_input":"2022-11-11T05:44:13.261156Z","iopub.status.idle":"2022-11-11T05:44:13.287667Z","shell.execute_reply.started":"2022-11-11T05:44:13.26111Z","shell.execute_reply":"2022-11-11T05:44:13.286479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum(RGB_compare['Foreground'])","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:44:14.321201Z","iopub.execute_input":"2022-11-11T05:44:14.32223Z","iopub.status.idle":"2022-11-11T05:44:14.328928Z","shell.execute_reply.started":"2022-11-11T05:44:14.322177Z","shell.execute_reply":"2022-11-11T05:44:14.327943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum(colorsobjonly['background'])","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:44:15.760119Z","iopub.execute_input":"2022-11-11T05:44:15.760559Z","iopub.status.idle":"2022-11-11T05:44:15.767684Z","shell.execute_reply.started":"2022-11-11T05:44:15.760525Z","shell.execute_reply":"2022-11-11T05:44:15.766632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img);","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:45:09.811989Z","iopub.execute_input":"2022-11-11T05:45:09.812622Z","iopub.status.idle":"2022-11-11T05:45:10.410105Z","shell.execute_reply.started":"2022-11-11T05:45:09.812582Z","shell.execute_reply":"2022-11-11T05:45:10.409125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pix_img=img[151:300,151:300]\n# print(pix_img)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T07:22:16.367283Z","iopub.execute_input":"2022-11-11T07:22:16.368057Z","iopub.status.idle":"2022-11-11T07:22:16.372501Z","shell.execute_reply.started":"2022-11-11T07:22:16.36802Z","shell.execute_reply":"2022-11-11T07:22:16.371412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(pix_img)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T07:22:17.636083Z","iopub.execute_input":"2022-11-11T07:22:17.636568Z","iopub.status.idle":"2022-11-11T07:22:17.85634Z","shell.execute_reply.started":"2022-11-11T07:22:17.636527Z","shell.execute_reply":"2022-11-11T07:22:17.855319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy\n# myimg = cv2.imread('image.jpg')\navg_color_per_row = numpy.average(img, axis=1)\navg_color = numpy.median(avg_color_per_row, axis=0)\nprint(avg_color)\nplt.imshow([avg_color])\nplt.show(avg_color)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T07:39:55.962302Z","iopub.execute_input":"2022-11-11T07:39:55.962692Z","iopub.status.idle":"2022-11-11T07:39:56.3276Z","shell.execute_reply.started":"2022-11-11T07:39:55.962659Z","shell.execute_reply":"2022-11-11T07:39:56.326394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow([(255, 0, 0)])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-11T07:22:20.764331Z","iopub.execute_input":"2022-11-11T07:22:20.767018Z","iopub.status.idle":"2022-11-11T07:22:20.947088Z","shell.execute_reply.started":"2022-11-11T07:22:20.766979Z","shell.execute_reply":"2022-11-11T07:22:20.94602Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\nplt.imshow(hsv);","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:45:10.712907Z","iopub.execute_input":"2022-11-11T05:45:10.71367Z","iopub.status.idle":"2022-11-11T05:45:11.33035Z","shell.execute_reply.started":"2022-11-11T05:45:10.713622Z","shell.execute_reply":"2022-11-11T05:45:11.329357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"low_orange = np.array([0, 0, 50])\nhigh_orange = np.array([20, 20, 255])\nmasking = cv2.inRange(hsv,low_orange, high_orange)\nplt.imshow(masking);\n","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:45:11.980654Z","iopub.execute_input":"2022-11-11T05:45:11.982718Z","iopub.status.idle":"2022-11-11T05:45:12.505295Z","shell.execute_reply.started":"2022-11-11T05:45:11.98267Z","shell.execute_reply":"2022-11-11T05:45:12.504388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"low_oranges = np.array([2, 2, 90])\nhigh_oranges = np.array([89,255,255])\nmaskings = cv2.inRange(hsv,low_oranges, high_oranges)\nplt.imshow(maskings);","metadata":{"execution":{"iopub.status.busy":"2022-11-11T05:45:12.908101Z","iopub.execute_input":"2022-11-11T05:45:12.908597Z","iopub.status.idle":"2022-11-11T05:45:13.54905Z","shell.execute_reply.started":"2022-11-11T05:45:12.908556Z","shell.execute_reply":"2022-11-11T05:45:13.548055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:50:14.758567Z","iopub.execute_input":"2022-11-11T10:50:14.759007Z","iopub.status.idle":"2022-11-11T10:50:14.780874Z","shell.execute_reply.started":"2022-11-11T10:50:14.758967Z","shell.execute_reply":"2022-11-11T10:50:14.779785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import binascii\nimport struct\nfrom PIL import Image\nimport numpy as np\nimport scipy\nimport scipy.misc\nimport scipy.cluster\n\nNUM_CLUSTERS = 5\n\nprint('reading image')\nim = Image.open(img)\nim = im.resize((150, 150))      # optional, to reduce time\nar = np.asarray(im)\nshape = ar.shape\nar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float)\n\nprint('finding clusters')\ncodes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)\nprint('cluster centres:\\n', codes)\n\nvecs, dist = scipy.cluster.vq.vq(ar, codes)         # assign codes\ncounts, bins = scipy.histogram(vecs, len(codes))    # count occurrences\n\nindex_max = scipy.argmax(counts)                    # find most frequent\npeak = codes[index_max]\ncolour = binascii.hexlify(bytearray(int(c) for c in peak)).decode('ascii')\nprint('most frequent is %s (#%s)' % (peak, colour))","metadata":{"execution":{"iopub.status.busy":"2022-11-11T11:24:37.940409Z","iopub.execute_input":"2022-11-11T11:24:37.940785Z","iopub.status.idle":"2022-11-11T11:24:37.965474Z","shell.execute_reply.started":"2022-11-11T11:24:37.940753Z","shell.execute_reply":"2022-11-11T11:24:37.963861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import imageio\nc = ar.copy()\nfor i, code in enumerate(codes):\n    c[scipy.r_[scipy.where(vecs==i)],:] = code\nimageio.imwrite('clusters.png', c.reshape(*shape).astype(np.uint8))\nprint('saved clustered image')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob","metadata":{"execution":{"iopub.status.busy":"2022-11-11T11:42:03.370276Z","iopub.execute_input":"2022-11-11T11:42:03.370649Z","iopub.status.idle":"2022-11-11T11:42:03.376782Z","shell.execute_reply.started":"2022-11-11T11:42:03.37061Z","shell.execute_reply":"2022-11-11T11:42:03.375463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_get(path_image=\"\"):\n    path_image = glob.glob(path_image)\n    for file in path_image:\n        imag = cv2.imread(file)\n        imag = cv2.cvtColor(imag, cv2.COLOR_BGR2RGB)\n        cv2.imshow(\"Images\", imag)\n        cv2.waitKey(0)\n        cv2.destroyAllWindows()\n    return imag\ndef cluster_segmentation(img=None, clt=None):\n    clt = KMeans(n_clusters=clt)\n    clt.fit(img.reshape(-1, 3))\n    width = 300\n    palette = np.zeros((50, width, 3), np.uint8)\n    steps = width/clt.cluster_centers_.shape[0]\n    for idx, centers in enumerate(clt.cluster_centers_):\n        palette[:, int(idx*steps):(int((idx+1)*steps)), :] = centers\n        # print(palette)\n    rgb = clt.cluster_centers_.astype(int)\n    rgb = tuple(rgb.reshape(1, -1)[0])\n    return rgb\ndef predication_formating(rgb=None):\n    rgb_tuple_value = []\n    res = tuple(rgb[x:x + 3] for x in range(0, len(rgb), 3))\n    # printing result\n    rgb_tuple_value.append(str(res))\n    print(\"This is rgb_tuple******\", rgb_tuple_value)\n    rgb_tuple_value = tuple(rgb_tuple_value)\n    return rgb_tuple_value\ndef get_hexcode(rgb_tuple_value):\n    look_common_hash = []\n    hex_code = []\n    for i in rgb_tuple_value:\n        look_common_hash.extend(eval(i))\n        look_common_hash\n    for i in look_common_hash:\n        print(i)\n    print(\"#\"\"%02x%02x%02x\" % i)\n    create_hex = (\"#\"\"%02x%02x%02x\" % i)\n    hex_code.append(create_hex)\n    return hex_code\n# def get_main_hexvalue():\n# imag = image_get(path_image=\"/home/fxdata/Downloads/insta/vscode for insta/model for color detection/selected bg removed imgs/14name.jpg\")\nimag = image_get(img)\nrgb = cluster_segmentation(img=imag,clt=5)\nrgb_tuple_value = predication_formating(rgb=rgb)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T11:42:04.582376Z","iopub.execute_input":"2022-11-11T11:42:04.583088Z","iopub.status.idle":"2022-11-11T11:42:04.616562Z","shell.execute_reply.started":"2022-11-11T11:42:04.583047Z","shell.execute_reply":"2022-11-11T11:42:04.614955Z"},"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_count":null,"outputs":[]}]}