{"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":"markdown","source":"## Random Flortists - K Means Clustering","metadata":{}},{"cell_type":"code","source":"from kaggle_datasets import KaggleDatasets\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-27T19:16:28.575006Z","iopub.execute_input":"2021-11-27T19:16:28.575448Z","iopub.status.idle":"2021-11-27T19:16:28.579213Z","shell.execute_reply.started":"2021-11-27T19:16:28.575418Z","shell.execute_reply":"2021-11-27T19:16:28.578322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"../input/code-ml-2021-challenge-6-pierre-papier-ciseaux/training/training/*\"\ntest_path = \"../input/code-ml-2021-challenge-6-pierre-papier-ciseaux/testing/testing/*\"","metadata":{"execution":{"iopub.status.busy":"2021-11-27T19:18:18.90219Z","iopub.execute_input":"2021-11-27T19:18:18.902847Z","iopub.status.idle":"2021-11-27T19:18:18.906567Z","shell.execute_reply.started":"2021-11-27T19:18:18.902803Z","shell.execute_reply":"2021-11-27T19:18:18.905791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from glob import glob","metadata":{"execution":{"iopub.status.busy":"2021-11-27T19:18:20.543078Z","iopub.execute_input":"2021-11-27T19:18:20.543792Z","iopub.status.idle":"2021-11-27T19:18:20.546971Z","shell.execute_reply.started":"2021-11-27T19:18:20.543756Z","shell.execute_reply":"2021-11-27T19:18:20.5464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_files = glob(train_path)\ntest_image_files = glob(test_path)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T19:19:45.781366Z","iopub.execute_input":"2021-11-27T19:19:45.781664Z","iopub.status.idle":"2021-11-27T19:19:45.802204Z","shell.execute_reply.started":"2021-11-27T19:19:45.781633Z","shell.execute_reply":"2021-11-27T19:19:45.801353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_image_files)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T19:19:47.514696Z","iopub.execute_input":"2021-11-27T19:19:47.515662Z","iopub.status.idle":"2021-11-27T19:19:47.522085Z","shell.execute_reply.started":"2021-11-27T19:19:47.515612Z","shell.execute_reply":"2021-11-27T19:19:47.521193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_image_files)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T19:19:47.936652Z","iopub.execute_input":"2021-11-27T19:19:47.937438Z","iopub.status.idle":"2021-11-27T19:19:47.942352Z","shell.execute_reply.started":"2021-11-27T19:19:47.937392Z","shell.execute_reply":"2021-11-27T19:19:47.94169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_images(paths):\n    tmp = []\n    for path in paths:\n        tmp.append(imread(path))\n    return tmp","metadata":{"execution":{"iopub.status.busy":"2021-11-27T19:20:12.656358Z","iopub.execute_input":"2021-11-27T19:20:12.657011Z","iopub.status.idle":"2021-11-27T19:20:12.662874Z","shell.execute_reply.started":"2021-11-27T19:20:12.656969Z","shell.execute_reply":"2021-11-27T19:20:12.661967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## The training images\ntraining_images = load_images(train_image_files)\n## The testing images\ntesting_images = load_images(train_image_files)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T19:40:41.772793Z","iopub.execute_input":"2021-11-27T19:40:41.7732Z","iopub.status.idle":"2021-11-27T19:41:03.547406Z","shell.execute_reply.started":"2021-11-27T19:40:41.773163Z","shell.execute_reply":"2021-11-27T19:41:03.546579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from numpy import array\nfrom PIL import Image\nimport numpy as np\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2021-11-27T20:00:56.945634Z","iopub.execute_input":"2021-11-27T20:00:56.945928Z","iopub.status.idle":"2021-11-27T20:00:56.94986Z","shell.execute_reply.started":"2021-11-27T20:00:56.945895Z","shell.execute_reply":"2021-11-27T20:00:56.949054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def img2NP (image):\n    img= Image.open(image)\n    np_img = np.array(img)\n    return np_img","metadata":{"execution":{"iopub.status.busy":"2021-11-27T20:00:15.246658Z","iopub.execute_input":"2021-11-27T20:00:15.246954Z","iopub.status.idle":"2021-11-27T20:00:15.252413Z","shell.execute_reply.started":"2021-11-27T20:00:15.246921Z","shell.execute_reply":"2021-11-27T20:00:15.251482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def flattenGrayImage(image):\n    npImage = img2NP(image)\n    img = np.frombuffer(npImage, dtype=np.uint8).reshape((300, 300, 4))\n    img_gray = cv2.cvtColor(img, cv2.COLOR_RGBA2GRAY)\n    img_gray_flat = img_gray.flatten()\n    return img_gray_flat","metadata":{"execution":{"iopub.status.busy":"2021-11-27T20:00:30.176234Z","iopub.execute_input":"2021-11-27T20:00:30.177188Z","iopub.status.idle":"2021-11-27T20:00:30.183349Z","shell.execute_reply.started":"2021-11-27T20:00:30.177131Z","shell.execute_reply":"2021-11-27T20:00:30.182598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def readyImages(imageFiles):\n    readyImgArray = []\n    for image in imageFiles:\n        readyImgArray.append(flattenGrayImage(image))\n    return readyImgArray","metadata":{"execution":{"iopub.status.busy":"2021-11-27T20:00:30.742147Z","iopub.execute_input":"2021-11-27T20:00:30.74267Z","iopub.status.idle":"2021-11-27T20:00:30.747632Z","shell.execute_reply.started":"2021-11-27T20:00:30.742635Z","shell.execute_reply":"2021-11-27T20:00:30.746649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## The training images flattened and in grayscale\ntrainReadyImages = readyImages(train_images)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T20:00:31.272306Z","iopub.execute_input":"2021-11-27T20:00:31.272645Z","iopub.status.idle":"2021-11-27T20:00:40.373862Z","shell.execute_reply.started":"2021-11-27T20:00:31.272615Z","shell.execute_reply":"2021-11-27T20:00:40.373003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## The testing images flattened and in grayscale\ntestReadyImages = readyImages(testing_images)","metadata":{},"execution_count":null,"outputs":[]}]}