{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":346708,"sourceType":"datasetVersion","datasetId":149846},{"sourceId":1165040,"sourceType":"datasetVersion","datasetId":660021},{"sourceId":1803569,"sourceType":"datasetVersion","datasetId":1071580},{"sourceId":1881640,"sourceType":"datasetVersion","datasetId":1120594}],"dockerImageVersionId":30301,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"\n","metadata":{}},{"cell_type":"markdown","source":"# Dataset for the Background Class","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport os\nimport cv2\nimport shutil\nfrom PIL import Image,ImageOps \nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-07-25T07:35:01.323628Z","iopub.execute_input":"2024-07-25T07:35:01.324762Z","iopub.status.idle":"2024-07-25T07:35:01.527754Z","shell.execute_reply.started":"2024-07-25T07:35:01.324629Z","shell.execute_reply":"2024-07-25T07:35:01.526552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Monochromatic background.","metadata":{}},{"cell_type":"code","source":"folderlocation = './data/'\nif not os.path.exists(folderlocation):\n    os.mkdir(folderlocation)\nfolderlocation = './data/Monochrome/'\nif not os.path.exists(folderlocation):\n    os.mkdir(folderlocation)\n\nimage_iter = np.zeros([50, 50, 3])\n\nfor iter1 in range(8):\n    for iter2 in range(8):\n        for iter3 in range(8):\n            image_iter[:,:,0] = iter1*36\n            image_iter[:,:,1] = iter2*36\n            image_iter[:,:,2] = iter3*36\n            \n            cv2.imwrite(folderlocation + \"Monochrome_\"+str(iter1)+str(iter2)+str(iter3)+\".png\", image_iter)\n            ","metadata":{"execution":{"iopub.status.busy":"2024-07-25T07:35:01.529766Z","iopub.execute_input":"2024-07-25T07:35:01.530120Z","iopub.status.idle":"2024-07-25T07:35:01.599438Z","shell.execute_reply.started":"2024-07-25T07:35:01.530087Z","shell.execute_reply":"2024-07-25T07:35:01.598246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## GrassClover Dataset training set\nGrass. Often pictures contain small flower, high resolution, 261 images in training set. Images\n[0,1,3,4,9,19,26,29] have no flower.","metadata":{}},{"cell_type":"code","source":"folderlocation = './data/Grass/'\nif not os.path.exists(folderlocation):\n    os.mkdir(folderlocation)\n\nPATH = '/kaggle/input/grassclover-dataset/biomass_data/train/images/'\nfilenames = next(os.walk(PATH), (None, None, []))[2]  \nfilenames = sorted(filenames)\n\nimg = cv2.imread(PATH + filenames[1], cv2.IMREAD_UNCHANGED)\nprint('Example Image size: ',img.shape)\n\nplt.imshow(img)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-25T07:35:01.600987Z","iopub.execute_input":"2024-07-25T07:35:01.601381Z","iopub.status.idle":"2024-07-25T07:35:03.139634Z","shell.execute_reply.started":"2024-07-25T07:35:01.601334Z","shell.execute_reply":"2024-07-25T07:35:03.138465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def inverte(imagem, name):\n    imagem = (255-imagem)\n    cv2.imwrite(name, imagem)\n\ndim = (512, 512)\nfor iter1 in [0,1,3,4,9,19,26,29]:  \n    img_count =0\n    img = cv2.imread(PATH + filenames[iter1], cv2.IMREAD_UNCHANGED)\n    \n    # Resize and save\n    resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)\n    cv2.imwrite(folderlocation + str(img_count) +'_'+filenames[iter1], resized)\n    inverte(resized,folderlocation + str(img_count) +'_i_'+filenames[iter1])\n    img_count = img_count + 1\n    \n    # Crop and save\n    for iter2 in [100,500,900,1300,1700]:\n        for iter3 in [100,500,900,1300,1700]:\n            resized = img[iter2:dim[0]+iter2,iter3:dim[0]+iter3,:]\n            cv2.imwrite(folderlocation + str(img_count) +'_'+filenames[iter1], resized)\n            inverte(resized,folderlocation + str(img_count) +'_i_'+filenames[iter1])\n            img_count = img_count + 1","metadata":{"execution":{"iopub.status.busy":"2024-07-25T07:35:03.141987Z","iopub.execute_input":"2024-07-25T07:35:03.142363Z","iopub.status.idle":"2024-07-25T07:35:08.596409Z","shell.execute_reply.started":"2024-07-25T07:35:03.142329Z","shell.execute_reply":"2024-07-25T07:35:08.595278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Satellite Images of Water Bodies","metadata":{}},{"cell_type":"code","source":"folderlocation = './data/space/'\nif not os.path.exists(folderlocation):\n    os.mkdir(folderlocation)\n\nPATH = '/kaggle/input/satellite-images-of-water-bodies/Water Bodies Dataset/Images/'\nfilenames = next(os.walk(PATH), (None, None, []))[2]  \nfilenames = sorted(filenames)\n\nimg = cv2.imread(PATH + filenames[1], cv2.IMREAD_UNCHANGED)\nprint('Example Image size: ',img.shape)\n\nfor iter1 in range(300):\n    shutil.copyfile(PATH + filenames[iter1], folderlocation + 'Water'+ filenames[iter1])\n    img = cv2.imread(PATH + filenames[iter1], cv2.IMREAD_UNCHANGED)\n    inverte(img,folderlocation +'Water_i_'+filenames[iter1])\n\nplt.imshow(img)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-25T07:42:09.878456Z","iopub.execute_input":"2024-07-25T07:42:09.879569Z","iopub.status.idle":"2024-07-25T07:42:16.105727Z","shell.execute_reply.started":"2024-07-25T07:42:09.879516Z","shell.execute_reply":"2024-07-25T07:42:16.104295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Galaxy Zoo 2: Images","metadata":{}},{"cell_type":"code","source":"folderlocation = './data/space/'\n\nPATH = '/kaggle/input/galaxy-zoo-2-images/images_gz2/images/'\nfilenames = next(os.walk(PATH), (None, None, []))[2]  \nfilenames = sorted(filenames)\n\nimg = cv2.imread(PATH + filenames[1], cv2.IMREAD_UNCHANGED)\nprint('Example Image size: ',img.shape)\n\nfor iter1 in range(300):\n    shutil.copyfile(PATH + filenames[iter1], folderlocation + 'Galaxy'+filenames[iter1])\n    img = cv2.imread(PATH + filenames[iter1], cv2.IMREAD_UNCHANGED)\n    inverte(img,folderlocation +'Galaxy_i_'+filenames[iter1])\n    \nplt.imshow(img)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-25T07:42:30.290137Z","iopub.execute_input":"2024-07-25T07:42:30.290635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Wood texture samples\nBackground may contain wooden furniture","metadata":{}},{"cell_type":"code","source":"PATH = '/kaggle/input/wood-samples/images/images/'\nfilenames = next(os.walk(PATH), (None, None, []))[2] \n\nfolderlocation = './data/wood/'\nif not os.path.exists(folderlocation):\n    os.mkdir(folderlocation)\n\nfor iter1 in range(200):\n    shutil.copyfile(PATH + filenames[iter1], folderlocation +filenames[iter1])\n    img = cv2.imread(PATH + filenames[iter1], cv2.IMREAD_UNCHANGED)\n    inverte(img,folderlocation +'_i_'+filenames[iter1])","metadata":{"execution":{"iopub.status.busy":"2024-07-25T07:35:14.127187Z","iopub.status.idle":"2024-07-25T07:35:14.127809Z","shell.execute_reply.started":"2024-07-25T07:35:14.127480Z","shell.execute_reply":"2024-07-25T07:35:14.127507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.make_archive('Background_data', 'zip', './data/')\nshutil.rmtree('./data/')","metadata":{"execution":{"iopub.status.busy":"2024-07-25T07:35:14.129139Z","iopub.status.idle":"2024-07-25T07:35:14.129533Z","shell.execute_reply.started":"2024-07-25T07:35:14.129336Z","shell.execute_reply":"2024-07-25T07:35:14.129354Z"},"trusted":true},"execution_count":null,"outputs":[]}]}