{"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":"# Ink Detection Segment Anything Approach\n","metadata":{"id":"SQJ1qfByPrMW","papermill":{"duration":0.008134,"end_time":"2023-05-06T09:38:53.470774","exception":false,"start_time":"2023-05-06T09:38:53.462640","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!pip install git+https://github.com/facebookresearch/segment-anything.git","metadata":{"execution":{"iopub.execute_input":"2023-05-06T09:38:53.491213Z","iopub.status.busy":"2023-05-06T09:38:53.489735Z","iopub.status.idle":"2023-05-06T09:39:07.323639Z","shell.execute_reply":"2023-05-06T09:39:07.322730Z"},"papermill":{"duration":13.844532,"end_time":"2023-05-06T09:39:07.323808","exception":false,"start_time":"2023-05-06T09:38:53.479276","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport requests\nfrom io import BytesIO\nimport matplotlib.pyplot as plt\nimport torch\nimport torch.nn as nn\nimport torchvision\nfrom torchvision import transforms as pth_transforms\nimport numpy as np\nfrom PIL import Image\nfrom segment_anything import sam_model_registry \nfrom segment_anything import SamAutomaticMaskGenerator\nfrom segment_anything import SamPredictor","metadata":{"execution":{"iopub.execute_input":"2023-05-06T09:39:07.348968Z","iopub.status.busy":"2023-05-06T09:39:07.347883Z","iopub.status.idle":"2023-05-06T09:39:09.249239Z","shell.execute_reply":"2023-05-06T09:39:09.248724Z","shell.execute_reply.started":"2023-05-05T06:17:04.761084Z"},"id":"GV8ILLyuQR6J","papermill":{"duration":1.915908,"end_time":"2023-05-06T09:39:09.249386","exception":false,"start_time":"2023-05-06T09:39:07.333478","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tpaths=[]\nfor dirname, _, filenames in os.walk('/kaggle/input/vesuvius-challenge-ink-detection/train/1'):\n    for filename in filenames:\n        if filename[-4:]=='.tif':\n            tpaths+=[(os.path.join(dirname, filename))]\ntpaths=sorted(tpaths)\nprint(tpaths[0])\nprint(len(tpaths))","metadata":{"execution":{"iopub.execute_input":"2023-05-06T09:39:09.272603Z","iopub.status.busy":"2023-05-06T09:39:09.270827Z","iopub.status.idle":"2023-05-06T09:39:09.297946Z","shell.execute_reply":"2023-05-06T09:39:09.297531Z","shell.execute_reply.started":"2023-05-05T06:17:04.768337Z"},"papermill":{"duration":0.039441,"end_time":"2023-05-06T09:39:09.298088","exception":false,"start_time":"2023-05-06T09:39:09.258647","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## mask_generator","metadata":{"id":"BbrKTiUiXMm4","papermill":{"duration":0.009086,"end_time":"2023-05-06T09:39:09.316357","exception":false,"start_time":"2023-05-06T09:39:09.307271","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sam_checkpoint = \"/kaggle/input/segment-anything/pytorch/vit-h/1/model.pth\"\nmodel_type = \"vit_h\" #\ndevice = \"cpu\"  #cpu,cuda\nsam = sam_model_registry[model_type](checkpoint=sam_checkpoint)\nsam.to(device=device)\nmask_generator = SamAutomaticMaskGenerator(sam)","metadata":{"execution":{"iopub.execute_input":"2023-05-06T09:39:09.341080Z","iopub.status.busy":"2023-05-06T09:39:09.340378Z","iopub.status.idle":"2023-05-06T09:39:34.401479Z","shell.execute_reply":"2023-05-06T09:39:34.400902Z"},"papermill":{"duration":25.076075,"end_time":"2023-05-06T09:39:34.401627","exception":false,"start_time":"2023-05-06T09:39:09.325552","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_anns(anns, axes=None):\n    if len(anns) == 0:\n        return\n    if axes:\n        ax = axes\n    else:\n        ax = plt.gca()\n        ax.set_autoscale_on(False)\n    sorted_anns = sorted(anns, key=(lambda x: x['area']), reverse=True)\n    polygons = []\n    color = []\n    for ann in sorted_anns:\n        m = ann['segmentation']\n        img = np.ones((m.shape[0], m.shape[1], 3))\n        color_mask = np.random.random((1, 3)).tolist()[0]\n        for i in range(3):\n            img[:,:,i] = color_mask[i]\n        ax.imshow(np.dstack((img, m*0.5)))\n\ndef show_mask(mask, ax, random_color=False):\n    if random_color:\n        color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)\n    else:\n        color = np.array([30/255, 144/255, 255/255, 0.6])\n    h, w = mask.shape[-2:]\n    mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)\n    ax.imshow(mask_image)\n\ndef show_points(coords, labels, ax, marker_size=375):\n    pos_points = coords[labels==1]\n    neg_points = coords[labels==0]\n    ax.scatter(pos_points[:, 0], pos_points[:, 1], color='green', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)\n    ax.scatter(neg_points[:, 0], neg_points[:, 1], color='red', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)   \n\ndef show_box(box, ax):\n    x0, y0 = box[0], box[1]\n    w, h = box[2] - box[0], box[3] - box[1]\n    ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor='green', facecolor=(0,0,0,0), lw=2)) ","metadata":{"execution":{"iopub.execute_input":"2023-05-06T09:39:34.434351Z","iopub.status.busy":"2023-05-06T09:39:34.432666Z","iopub.status.idle":"2023-05-06T09:39:34.434947Z","shell.execute_reply":"2023-05-06T09:39:34.435390Z"},"papermill":{"duration":0.02427,"end_time":"2023-05-06T09:39:34.435521","exception":false,"start_time":"2023-05-06T09:39:34.411251","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path=tpaths[4]\nimage=cv2.imread(path)\nimage=cv2.resize(image,dsize=(0,0),fx=0.1,fy=0.1)  \nplt.imshow(image)\nplt.axis('off') \nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-05-06T09:39:34.458802Z","iopub.status.busy":"2023-05-06T09:39:34.458188Z","iopub.status.idle":"2023-05-06T09:39:36.425429Z","shell.execute_reply":"2023-05-06T09:39:36.425847Z","shell.execute_reply.started":"2023-05-05T06:31:46.822809Z"},"papermill":{"duration":1.981005,"end_time":"2023-05-06T09:39:36.425997","exception":false,"start_time":"2023-05-06T09:39:34.444992","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image=cv2.imread(path)\nimage=cv2.resize(image,dsize=(0,0),fx=0.2,fy=0.2)  \n(h,w,c)=image.shape\nprint(h,h//3)\nprint(w,w//3)\nfig, axs = plt.subplots(3,3,figsize=(15,20))\nfor i in range(9):\n    r=i//3\n    c=i%3\n    imagei=image[(h//3)*r:(h//3)*(r+1),(w//3)*c:(w//3)*(c+1),:]\n    print(r,c,imagei.shape)\n    axs[r][c].imshow(imagei)\n    axs[r][c].axis('off') \nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-05-06T09:39:36.455485Z","iopub.status.busy":"2023-05-06T09:39:36.454500Z","iopub.status.idle":"2023-05-06T09:39:37.916212Z","shell.execute_reply":"2023-05-06T09:39:37.916635Z","shell.execute_reply.started":"2023-05-05T06:17:08.982888Z"},"papermill":{"duration":1.478995,"end_time":"2023-05-06T09:39:37.916785","exception":false,"start_time":"2023-05-06T09:39:36.437790","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image=cv2.imread(path)\nimage=cv2.resize(image,dsize=(0,0),fx=0.2,fy=0.2)  \n(h,w,c)=image.shape\nfig, axs = plt.subplots(3,3,figsize=(15,20))\nfor i in range(9):\n    r=i//3\n    c=i%3\n    imagei=image[(h//3)*r:(h//3)*(r+1),(w//3)*c:(w//3)*(c+1),:]\n    masks = mask_generator.generate(imagei)\n    show_anns(masks,axs[r][c])\n    axs[r][c].axis('off') \nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-05-06T09:39:37.975104Z","iopub.status.busy":"2023-05-06T09:39:37.974133Z","iopub.status.idle":"2023-05-06T10:37:09.066150Z","shell.execute_reply":"2023-05-06T10:37:09.066634Z","shell.execute_reply.started":"2023-05-05T06:17:38.792237Z"},"papermill":{"duration":3451.124588,"end_time":"2023-05-06T10:37:09.066804","exception":false,"start_time":"2023-05-06T09:39:37.942216","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.034571,"end_time":"2023-05-06T10:37:09.131817","exception":false,"start_time":"2023-05-06T10:37:09.097246","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}