{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/vscode/.local/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"from solareyes.sam import SAM\n",
"\n",
"sam = SAM()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"from PIL import Image\n",
"\n",
"img_url = \"https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png\"\n",
"raw_image = Image.open(requests.get(img_url, stream=True).raw).convert(\"RGB\")\n",
"input_points = [[[450, 600]]] # 2D location of a window in the image"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"\n",
"masks, scores = sam.segment(raw_image, input_points)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"flat_first_mask = masks[0][0][0]\n",
"ffmnp = flat_first_mask.numpy()\n",
"ffmnp.shape\n",
"ffmnp[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'masks' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/workspaces/solar_eyes/sandbox.ipynb Cell 6\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> 1\u001b[0m squeezed \u001b[39m=\u001b[39m masks[\u001b[39m0\u001b[39m]\u001b[39m.\u001b[39msqueeze()\n\u001b[1;32m 2\u001b[0m squeezed[\u001b[39m0\u001b[39m][\u001b[39m500\u001b[39m][\u001b[39m600\u001b[39m]\n",
"\u001b[0;31mNameError\u001b[0m: name 'masks' is not defined"
]
}
],
"source": [
"squeezed = masks[0].squeeze()\n",
"squeezed[0][500][600]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"def flatten_mask(mask, 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",
" return mask.reshape(h, w, 1) * color.reshape(1, 1, -1)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1764\n",
"2646\n"
]
}
],
"source": [
"squeezed = masks[0].squeeze()\n",
"h, w = squeezed.shape[-2:]\n",
"print(h)\n",
"print(w)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"ename": "RuntimeError",
"evalue": "shape '[1764, 2646, 1]' is invalid for input of size 14002632",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/workspaces/solar_eyes/sandbox.ipynb Cell 9\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> 1\u001b[0m flatten_mask(masks[\u001b[39m0\u001b[39;49m][\u001b[39m0\u001b[39;49m]\u001b[39m.\u001b[39;49msqueeze())\n",
"\u001b[1;32m/workspaces/solar_eyes/sandbox.ipynb Cell 9\u001b[0m line \u001b[0;36m8\n\u001b[1;32m 6\u001b[0m color \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39marray([\u001b[39m30\u001b[39m\u001b[39m/\u001b[39m\u001b[39m255\u001b[39m, \u001b[39m144\u001b[39m\u001b[39m/\u001b[39m\u001b[39m255\u001b[39m, \u001b[39m255\u001b[39m\u001b[39m/\u001b[39m\u001b[39m255\u001b[39m, \u001b[39m0.6\u001b[39m])\n\u001b[1;32m 7\u001b[0m h, w \u001b[39m=\u001b[39m mask\u001b[39m.\u001b[39mshape[\u001b[39m-\u001b[39m\u001b[39m2\u001b[39m:]\n\u001b[0;32m----> 8\u001b[0m \u001b[39mreturn\u001b[39;00m mask\u001b[39m.\u001b[39;49mreshape(h, w, \u001b[39m1\u001b[39;49m) \u001b[39m*\u001b[39m color\u001b[39m.\u001b[39mreshape(\u001b[39m1\u001b[39m, \u001b[39m1\u001b[39m, \u001b[39m-\u001b[39m\u001b[39m1\u001b[39m)\n",
"\u001b[0;31mRuntimeError\u001b[0m: shape '[1764, 2646, 1]' is invalid for input of size 14002632"
]
}
],
"source": [
"flatten_mask(masks[0][0].squeeze())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}