{"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":"### Use this notebook for\n\n- Removing background \n- Extracting digits (for object recognition)\n- Extracting digit bounding boxes (for object detection)\n\n### There are two issues with the current approach.\n\n- 1. blots because of image processing algorithm during image generation process of organizers. These will be extracted as digits. \n    - soulution: no need to worry about them ;)\n- BG removal will not work if the part of digit's size covers more than (1000*1000/2) pixels.\n    - Solution: use surrounding tiles\n\n![image.png](attachment:35257e8c-dca8-4898-9ea1-c5440caff1e3.png)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-09T22:26:20.858502Z","iopub.execute_input":"2022-03-09T22:26:20.858794Z","iopub.status.idle":"2022-03-09T22:26:20.865808Z","shell.execute_reply.started":"2022-03-09T22:26:20.858761Z","shell.execute_reply":"2022-03-09T22:26:20.864527Z"}}},{"cell_type":"code","source":"import os\nfrom PIL import Image\nfrom pathlib import Path\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import style\nfrom skimage import measure\nfrom skimage import filters\nfrom tqdm import tqdm\nfrom sklearn.cluster import DBSCAN\n\nstyle.use('fivethirtyeight')","metadata":{"execution":{"iopub.status.busy":"2022-03-09T22:28:47.621731Z","iopub.execute_input":"2022-03-09T22:28:47.622036Z","iopub.status.idle":"2022-03-09T22:28:47.62925Z","shell.execute_reply.started":"2022-03-09T22:28:47.622004Z","shell.execute_reply":"2022-03-09T22:28:47.628069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindir = '../input/ultra-mnist/train'\ndf = pd.read_csv('../input/ultra-mnist/train.csv')\nimages = df.id.apply(lambda x: Path(traindir)/f\"{x}.jpeg\").values\nlabels = df.digit_sum.values","metadata":{"execution":{"iopub.status.busy":"2022-03-09T22:28:49.679876Z","iopub.execute_input":"2022-03-09T22:28:49.680231Z","iopub.status.idle":"2022-03-09T22:28:50.233597Z","shell.execute_reply.started":"2022-03-09T22:28:49.680191Z","shell.execute_reply":"2022-03-09T22:28:50.232699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# OPERATIONS ON FIRST INDEX. (later goes into multiproc script)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T22:26:21.650517Z","iopub.execute_input":"2022-03-09T22:26:21.650871Z","iopub.status.idle":"2022-03-09T22:26:21.656572Z","shell.execute_reply.started":"2022-03-09T22:26:21.650827Z","shell.execute_reply":"2022-03-09T22:26:21.655537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):\n\n    lab = labels[i]\n    im = cv2.imread(str(images[i]),  cv2.IMREAD_GRAYSCALE)//255\n    print(\"LABEL:\", lab)\n    ## separate out bg and fg\n    w, h = im.shape\n    new_im = np.zeros((w, h))\n    tysz, txsz = w//4, h//4\n    for ty in range(0, w, tysz):\n        for tx in range(0, h, txsz):\n            tile = im[tx:tx+txsz, ty:ty+tysz]\n            vals, cnts = np.unique(tile, return_counts=True)\n            idx = np.argmax(cnts)\n            rmv = vals[idx]\n            msk = (tile==rmv)\n            tile[msk]=0\n            tile[~msk]=1\n            vals, cnts = np.unique(tile, return_counts=True)\n            new_im[tx:tx+txsz, ty:ty+tysz]=tile\n    new_im = new_im.astype(np.uint8)\n    plt.imshow(new_im)\n    plt.show()\n    cv2.imwrite('bg-rm.png', new_im*255)\n    ## individual images\n    X = np.array(np.where(new_im==1)).T\n    cluster = DBSCAN(eps=28/2, min_samples=28).fit(X)\n    all_coordinates = [] \n    for ulab in np.unique(cluster.labels_):\n        ### extract\n        idxs = (cluster.labels_==ulab)\n        coords = X[idxs]\n        xs = coords.T[0]\n        ys = coords.T[1]\n        bb = [xs.min(), xs.max(), ys.min(), ys.max()]\n        all_coordinates.append(np.array(bb))\n        # paste on a black bg.\n        # later give some padding by tw+padlen and th+padlen and xs-xs.min()+(padlen//2)\n        tw = xs.max()-xs.min()+1\n        th = ys.max()-ys.min()+1\n        print(tw, th, \"bb:\", bb)\n        tl = np.zeros((tw,th)) \n        txs = xs-xs.min()\n        tys = ys-ys.min()\n\n        tl[txs, tys] = 1\n        plt.imshow(tl)\n        plt.show()\n\n        print(\"---\")\n    print(np.hstack([all_coordinates]))","metadata":{"execution":{"iopub.status.busy":"2022-03-09T22:28:54.802243Z","iopub.execute_input":"2022-03-09T22:28:54.802527Z","iopub.status.idle":"2022-03-09T22:31:13.521297Z","shell.execute_reply.started":"2022-03-09T22:28:54.802498Z","shell.execute_reply":"2022-03-09T22:31:13.520342Z"},"trusted":true},"execution_count":null,"outputs":[]}]}