Skip to content

Commit 9ba3443

Browse files
authored
Update SAM 2 video notebook (#315)
1 parent 6f798f8 commit 9ba3443

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

docs/examples/sam2_video.ipynb

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@
123123
"The prompts can be points and boxes. The points are represented as a list of tuples, where each tuple contains the x and y coordinates of the point. The boxes are represented as a list of tuples, where each tuple contains the x, y, width, and height of the box."
124124
]
125125
},
126+
{
127+
"cell_type": "code",
128+
"execution_count": null,
129+
"metadata": {},
130+
"outputs": [],
131+
"source": [
132+
"predictor.show_images()"
133+
]
134+
},
126135
{
127136
"cell_type": "code",
128137
"execution_count": null,
@@ -147,6 +156,13 @@
147156
"predictor.show_prompts(prompts, frame_idx=0)"
148157
]
149158
},
159+
{
160+
"cell_type": "markdown",
161+
"metadata": {},
162+
"source": [
163+
"![image](https://github.com/user-attachments/assets/2290c685-5d08-4605-8859-ecc3d9986e8f)"
164+
]
165+
},
150166
{
151167
"cell_type": "markdown",
152168
"metadata": {},
@@ -221,6 +237,121 @@
221237
" \"blended\", fps=5, output_video=\"segments_blended.mp4\"\n",
222238
")"
223239
]
240+
},
241+
{
242+
"cell_type": "markdown",
243+
"metadata": {},
244+
"source": [
245+
"![image](https://github.com/user-attachments/assets/111cada6-ada1-4785-8d37-385872bd7a4f)"
246+
]
247+
},
248+
{
249+
"cell_type": "markdown",
250+
"metadata": {},
251+
"source": [
252+
"## Segment the objects from a video"
253+
]
254+
},
255+
{
256+
"cell_type": "code",
257+
"execution_count": null,
258+
"metadata": {},
259+
"outputs": [],
260+
"source": [
261+
"predictor = SamGeo2(\n",
262+
" model_id=\"sam2-hiera-large\",\n",
263+
" video=True,\n",
264+
")"
265+
]
266+
},
267+
{
268+
"cell_type": "code",
269+
"execution_count": null,
270+
"metadata": {},
271+
"outputs": [],
272+
"source": [
273+
"url = \"https://github.com/opengeos/datasets/releases/download/videos/cars.mp4\""
274+
]
275+
},
276+
{
277+
"cell_type": "code",
278+
"execution_count": null,
279+
"metadata": {},
280+
"outputs": [],
281+
"source": [
282+
"video_path = url\n",
283+
"predictor.set_video(video_path)"
284+
]
285+
},
286+
{
287+
"cell_type": "code",
288+
"execution_count": null,
289+
"metadata": {},
290+
"outputs": [],
291+
"source": [
292+
"predictor.show_images()"
293+
]
294+
},
295+
{
296+
"cell_type": "code",
297+
"execution_count": null,
298+
"metadata": {},
299+
"outputs": [],
300+
"source": [
301+
"prompts = {\n",
302+
" 1: {\n",
303+
" \"points\": [[335, 203]],\n",
304+
" \"labels\": [1],\n",
305+
" \"frame_idx\": 0,\n",
306+
" },\n",
307+
" 2: {\n",
308+
" \"points\": [[420, 201]],\n",
309+
" \"labels\": [1],\n",
310+
" \"frame_idx\": 0,\n",
311+
" },\n",
312+
"}"
313+
]
314+
},
315+
{
316+
"cell_type": "code",
317+
"execution_count": null,
318+
"metadata": {},
319+
"outputs": [],
320+
"source": [
321+
"predictor.show_prompts(prompts, frame_idx=0)"
322+
]
323+
},
324+
{
325+
"cell_type": "markdown",
326+
"metadata": {},
327+
"source": [
328+
"![image](https://github.com/user-attachments/assets/5f55c814-2a86-45a0-b75c-84e2757b88d5)"
329+
]
330+
},
331+
{
332+
"cell_type": "code",
333+
"execution_count": null,
334+
"metadata": {},
335+
"outputs": [],
336+
"source": [
337+
"predictor.predict_video(prompts)"
338+
]
339+
},
340+
{
341+
"cell_type": "code",
342+
"execution_count": null,
343+
"metadata": {},
344+
"outputs": [],
345+
"source": [
346+
"predictor.save_video_segments_blended(\"cars\", output_video=\"cars_blended.mp4\", fps=25)"
347+
]
348+
},
349+
{
350+
"cell_type": "markdown",
351+
"metadata": {},
352+
"source": [
353+
"![image](https://github.com/user-attachments/assets/ef5a0310-26d1-4d64-a880-73b1e5de42c9)"
354+
]
224355
}
225356
],
226357
"metadata": {

0 commit comments

Comments
 (0)