{"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":22962,"databundleVersionId":3171193,"sourceType":"competition"},{"sourceId":45917,"databundleVersionId":5024308,"sourceType":"competition"},{"sourceId":928083,"sourceType":"datasetVersion","datasetId":501015},{"sourceId":6230584,"sourceType":"datasetVersion","datasetId":2982879},{"sourceId":7958145,"sourceType":"datasetVersion","datasetId":4681153},{"sourceId":3848,"sourceType":"modelInstanceVersion","modelInstanceId":2749}],"dockerImageVersionId":30458,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nfrom matplotlib import pyplot as plt\nimport torch\nimport cv2\nfrom PIL import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-27T15:42:12.362925Z","iopub.execute_input":"2024-03-27T15:42:12.363481Z","iopub.status.idle":"2024-03-27T15:42:12.370948Z","shell.execute_reply.started":"2024-03-27T15:42:12.363434Z","shell.execute_reply":"2024-03-27T15:42:12.369455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"pip install git+https://github.com/facebookresearch/segment-anything.git","metadata":{"execution":{"iopub.status.busy":"2024-03-27T14:47:22.623525Z","iopub.execute_input":"2024-03-27T14:47:22.625134Z","iopub.status.idle":"2024-03-27T14:47:40.56313Z","shell.execute_reply.started":"2024-03-27T14:47:22.625064Z","shell.execute_reply":"2024-03-27T14:47:40.561383Z"},"trusted":true},"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\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\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.status.busy":"2024-03-27T14:43:26.747985Z","iopub.execute_input":"2024-03-27T14:43:26.748428Z","iopub.status.idle":"2024-03-27T14:43:26.773604Z","shell.execute_reply.started":"2024-03-27T14:43:26.74838Z","shell.execute_reply":"2024-03-27T14:43:26.771905Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Automatically Generating Object Masks with SAM\n\nhttps://github.com/facebookresearch/segment-anything/blob/main/notebooks/automatic_mask_generator_example.ipynb","metadata":{}},{"cell_type":"code","source":"from segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor\n\nsam_checkpoint = \"/kaggle/input/segment-anything/pytorch/vit-b/1/model.pth\"\nmodel_type = \"vit_b\"\n\n#device = \"cuda\"\n\nsam = sam_model_registry[model_type](checkpoint=sam_checkpoint)\n#sam.to(device=device)\n\nmask_generator = SamAutomaticMaskGenerator(sam, points_per_batch=16)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T15:42:19.582152Z","iopub.execute_input":"2024-03-27T15:42:19.58375Z","iopub.status.idle":"2024-03-27T15:42:21.49857Z","shell.execute_reply.started":"2024-03-27T15:42:19.583686Z","shell.execute_reply":"2024-03-27T15:42:21.497353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '/kaggle/input/myforces/image3A417656'\nos.rename(image_path,'/kaggle/input/myforces/image3A417656.jpg')\nimage_array = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\nmasks = mask_generator.generate(image_array)\n#print(masks)\n_, axes = plt.subplots(1,3, figsize=(16,16))\naxes[0].imshow(image_array)\nshow_anns(masks, axes[1])\naxes[2].imshow(image_array)\nshow_anns(masks, axes[2])\n# 创建一个与原始图像相同大小的全白图像\ncanvas = np.ones_like(image_array)\n# 将所有掩码绘制在画布上，使用不同的颜色\nfor i, mask in enumerate(masks):\n    # 转换掩码为 RGB 格式\n    real_mask = mask['segmentation'].astype(np.uint8)\n    mask_rgb = cv2.cvtColor(real_mask, cv2.COLOR_GRAY2RGB)\n    # 使用不同的颜色\n    color = (np.random.randint(0, 256), np.random.randint(0, 256), np.random.randint(0, 256))\n    # 将掩码绘制在画布上\n    canvas = cv2.addWeighted(canvas, 1, canvas, 1, 0)\n    canvas[real_mask > 0] = color\n\n# 将画布转换为 PIL 图像，并保存\nimage_pil = Image.fromarray(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))\nimage_pil.save(\"/kaggle/working/ces.png\")","metadata":{"execution":{"iopub.status.busy":"2024-03-27T15:50:39.687814Z","iopub.execute_input":"2024-03-27T15:50:39.688354Z","iopub.status.idle":"2024-03-27T15:50:39.728704Z","shell.execute_reply.started":"2024-03-27T15:50:39.688307Z","shell.execute_reply":"2024-03-27T15:50:39.726757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = \"/kaggle/working/ces.png\"\nimage_array = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RBG)\n_, axes = plt.subplots(1,3, figsize=(16,16))\naxes[0].imshow(image_array)\nshow_anns(masks, axes[1])\naxes[2].imshow(image_array)\nshow_anns(masks, axes[2])","metadata":{"execution":{"iopub.status.busy":"2024-03-27T15:09:10.37707Z","iopub.execute_input":"2024-03-27T15:09:10.378405Z","iopub.status.idle":"2024-03-27T15:09:10.422365Z","shell.execute_reply.started":"2024-03-27T15:09:10.378343Z","shell.execute_reply":"2024-03-27T15:09:10.420727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '/kaggle/input/stable-diffusion-image-to-prompts/images/c98f79f71.png'\nimage = cv2.imread(image_path)\n#masks = # 创建一个与原始图像相同大小的全白图像\ncanvas = np.ones_like(image_array)\n# 将所有掩码绘制在画布上，使用不同的颜色\nall_mask =np.zeros_like(masks[0]['segmentation']).astype(np.uint8)\nfor i, mask in enumerate(masks):\n    # 转换掩码为 RGB 格\n    real_mask = mask['segmentation'].astype(np.uint8)*255\n    all_mask = cv2.bitwise_or(all_mask,real_mask)\n  #  print(real_mask)\n   # mask_rgb = cv2.cvtColor(real_mask, cv2.COLOR_GRAY2RGB)\n    # 使用不同的颜色\n    #color = (np.random.randint(0, 256), np.random.randint(0, 256), np.random.randint(0, 256))\n    # 将掩码绘制在画布上\n    #canvas = cv2.addWeighted(canvas, 1, canvas, 1, 0)\n    #canvas[real_mask > 0] = color\nnasked_img=cv2.bitwise_and(image,image,mask=all_mask)\ncanvs=cv2.add(canvas,nasked_img)\nprint(all_mask)\nresult_image=cv2.addWeighted(image,0.5,mask_rgb,0.5,0)\n# 将画布转换为 PIL 图像，并保存\n#cv2.imshow('ss',result_image)\nimage_pil = Image.fromarray(cv2.cvtColor(canvs, cv2.COLOR_BGR2RGB))\nimage_pil.save(\"/kaggle/working/ces.png\")","metadata":{"execution":{"iopub.status.busy":"2024-03-27T16:39:15.733195Z","iopub.execute_input":"2024-03-27T16:39:15.733739Z","iopub.status.idle":"2024-03-27T16:39:15.809689Z","shell.execute_reply.started":"2024-03-27T16:39:15.733686Z","shell.execute_reply":"2024-03-27T16:39:15.808215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '/kaggle/input/happy-whale-and-dolphin/train_images/00177f3c614d1e.jpg'\nimage_array = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\nmasks = mask_generator.generate(image_array)\n\n_, axes = plt.subplots(1,3, figsize=(16,16))\naxes[0].imshow(image_array)\nshow_anns(masks, axes[1])\naxes[2].imshow(image_array)\nshow_anns(masks, axes[2])","metadata":{"execution":{"iopub.status.busy":"2024-03-27T14:43:33.12794Z","iopub.status.idle":"2024-03-27T14:43:33.129378Z","shell.execute_reply.started":"2024-03-27T14:43:33.12908Z","shell.execute_reply":"2024-03-27T14:43:33.129107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '/kaggle/input/food-segmentation/Food Segmentation/images/8.png'\nimage_array = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\nmasks = mask_generator.generate(image_array)\n\n_, axes = plt.subplots(1,3, figsize=(16,16))\naxes[0].imshow(image_array)\nshow_anns(masks, axes[1])\naxes[2].imshow(image_array)\nshow_anns(masks, axes[2])","metadata":{"execution":{"iopub.status.busy":"2024-03-27T14:43:33.130625Z","iopub.status.idle":"2024-03-27T14:43:33.131881Z","shell.execute_reply.started":"2024-03-27T14:43:33.131537Z","shell.execute_reply":"2024-03-27T14:43:33.131574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"del(mask_generator)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T14:43:33.133533Z","iopub.status.idle":"2024-03-27T14:43:33.134665Z","shell.execute_reply.started":"2024-03-27T14:43:33.134381Z","shell.execute_reply":"2024-03-27T14:43:33.134411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Object masks from prompts with SAM\n\nhttps://github.com/facebookresearch/segment-anything/blob/main/notebooks/predictor_example.ipynb","metadata":{}},{"cell_type":"code","source":"from segment_anything import sam_model_registry, SamPredictor\n\npredictor = SamPredictor(sam)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T14:43:33.136445Z","iopub.status.idle":"2024-03-27T14:43:33.137217Z","shell.execute_reply.started":"2024-03-27T14:43:33.136969Z","shell.execute_reply":"2024-03-27T14:43:33.136996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Selecting objects with SAM\n","metadata":{}},{"cell_type":"markdown","source":"## Specifying a specific object with points","metadata":{}},{"cell_type":"code","source":"image_path = '/kaggle/input/the-car-connection-picture-dataset/Acura_ILX_2014_28_16_110_15_4_70_55_179_39_FWD_5_4_4dr_Klc.jpg'\nimage_array = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\npredictor.set_image(image_array)\n\ninput_point = np.array([[120, 135]])\ninput_label = np.array([1])\n\nplt.imshow(image_array)\nshow_points(input_point, input_label, plt.gca())\nplt.axis('on')\nplt.show()  \n\n\n\nmasks, scores, logits = predictor.predict(\n    point_coords=input_point,\n    point_labels=input_label,\n    multimask_output=True,\n)\n\nfor i, (mask, score) in enumerate(zip(masks, scores)):\n#     plt.figure(figsize=(10,10))\n    plt.imshow(image_array)\n    show_mask(mask, plt.gca())\n    show_points(input_point, input_label, plt.gca())\n    plt.title(f\"Mask {i+1}, Score: {score:.3f}\", fontsize=18)\n    plt.show()  \n  ","metadata":{"execution":{"iopub.status.busy":"2024-03-27T14:43:33.139618Z","iopub.status.idle":"2024-03-27T14:43:33.140153Z","shell.execute_reply.started":"2024-03-27T14:43:33.139904Z","shell.execute_reply":"2024-03-27T14:43:33.139932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Specifying a specific object with additional points (include and exclude)","metadata":{}},{"cell_type":"code","source":"image_path = '/kaggle/input/stable-diffusion-image-to-prompts/images/c98f79f71.png'\nimage_array = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\npredictor.set_image(image_array)\n\ninput_point = np.array([[300, 50], [185, 100], [250, 200], [400, 50]])\ninput_label = np.array([1, 1, 0, 1])\n\nplt.imshow(image_array)\nshow_points(input_point, input_label, plt.gca())\nplt.axis('on')\nplt.show()  \n\n\n\nmasks, scores, logits = predictor.predict(\n    point_coords=input_point,\n    point_labels=input_label,\n    multimask_output=True,\n)\n\nfor i, (mask, score) in enumerate(zip(masks, scores)):\n#     plt.figure(figsize=(10,10))\n    plt.imshow(image_array)\n    show_mask(mask, plt.gca())\n    show_points(input_point, input_label, plt.gca())\n    plt.title(f\"Mask {i+1}, Score: {score:.3f}\", fontsize=18)\n    plt.show()  ","metadata":{"execution":{"iopub.status.busy":"2024-03-27T14:43:33.141529Z","iopub.status.idle":"2024-03-27T14:43:33.142012Z","shell.execute_reply.started":"2024-03-27T14:43:33.141779Z","shell.execute_reply":"2024-03-27T14:43:33.141804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '/kaggle/input/happy-whale-and-dolphin/train_images/00177f3c614d1e.jpg'\nimage_array = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\npredictor.set_image(image_array)\n\ninput_point = np.array([[1400, 1600], [2500, 500]])\ninput_label = np.array([1, 0])\n\nmask_input = logits[np.argmax(scores), :, :]  # Choose the model's best mask\nmasks, _, _ = predictor.predict(\n    point_coords=input_point,\n    point_labels=input_label,\n    mask_input=mask_input[None, :, :],\n    multimask_output=False,\n)\n\nplt.imshow(image_array)\nshow_mask(masks, plt.gca())\nshow_points(input_point, input_label, plt.gca())\nplt.show()   ","metadata":{"execution":{"iopub.status.busy":"2024-03-27T14:43:33.143973Z","iopub.status.idle":"2024-03-27T14:43:33.144548Z","shell.execute_reply.started":"2024-03-27T14:43:33.144219Z","shell.execute_reply":"2024-03-27T14:43:33.144245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Specifying a specific object with a box\n","metadata":{}},{"cell_type":"code","source":"input_box = np.array([600, 1100, 2100, 1800])\nmasks, _, _ = predictor.predict(\n    point_coords=None,\n    point_labels=None,\n    box=input_box[None, :],\n    multimask_output=False,\n)\nplt.imshow(image_array)\nshow_mask(masks[0], plt.gca())\nshow_box(input_box, plt.gca())\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T14:43:33.148717Z","iopub.status.idle":"2024-03-27T14:43:33.149324Z","shell.execute_reply.started":"2024-03-27T14:43:33.149035Z","shell.execute_reply":"2024-03-27T14:43:33.149065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Follow this notebook for additional prompting options:\n\nhttps://github.com/facebookresearch/segment-anything/blob/main/notebooks/predictor_example.ipynb","metadata":{}}]}