Skip to content

Commit e6c0dbf

Browse files
hustcalmCopilot
andauthored
Add keyframe saving to video content sample (#26)
* Add implementation of saving keyframes in the Video Content sample * Remove notebook outputs * Update notebooks/content_extraction.ipynb Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 1503b0c commit e6c0dbf

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

notebooks/content_extraction.ipynb

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,21 @@
7575
" api_version=AZURE_AI_API_VERSION,\n",
7676
" token_provider=token_provider,\n",
7777
" x_ms_useragent=\"azure-ai-content-understanding-python/content_extraction\", # This header is used for sample usage telemetry, please comment out this line if you want to opt out.\n",
78-
")"
78+
")\n",
79+
"\n",
80+
"# Utility function to save images\n",
81+
"from PIL import Image\n",
82+
"from io import BytesIO\n",
83+
"import re\n",
84+
"\n",
85+
"def save_image(image_id: str, response):\n",
86+
" raw_image = client.get_image_from_analyze_operation(analyze_response=response,\n",
87+
" image_id=image_id\n",
88+
" )\n",
89+
" image = Image.open(BytesIO(raw_image))\n",
90+
" # image.show()\n",
91+
" Path(\".cache\").mkdir(exist_ok=True)\n",
92+
" image.save(f\".cache/{image_id}.jpg\", \"JPEG\")\n"
7993
]
8094
},
8195
{
@@ -184,6 +198,27 @@
184198
"result = client.poll_result(response)\n",
185199
"\n",
186200
"print(json.dumps(result, indent=2))\n",
201+
"\n",
202+
"# Save keyframes (optional)\n",
203+
"keyframe_ids = set()\n",
204+
"result_data = result.get(\"result\", {})\n",
205+
"contents = result_data.get(\"contents\", [])\n",
206+
"\n",
207+
"# Iterate over contents to find keyframes if available\n",
208+
"for content in contents:\n",
209+
" # Extract keyframe IDs from \"markdown\" if it exists and is a string\n",
210+
" markdown_content = content.get(\"markdown\", \"\")\n",
211+
" if isinstance(markdown_content, str):\n",
212+
" keyframe_ids.update(re.findall(r\"(keyFrame\\.\\d+)\\.jpg\", markdown_content))\n",
213+
"\n",
214+
"# Output the results\n",
215+
"print(\"Unique Keyframe IDs:\", keyframe_ids)\n",
216+
"\n",
217+
"# Save all keyframe images\n",
218+
"for keyframe_id in keyframe_ids:\n",
219+
" save_image(keyframe_id, response)\n",
220+
"\n",
221+
"# Delete analyzer\n",
187222
"client.delete_analyzer(ANALYZER_ID)"
188223
]
189224
},
@@ -229,21 +264,6 @@
229264
"metadata": {},
230265
"outputs": [],
231266
"source": [
232-
"from PIL import Image\n",
233-
"from io import BytesIO\n",
234-
"import re\n",
235-
"\n",
236-
"\n",
237-
"def save_image(image_id: str):\n",
238-
" raw_image = client.get_image_from_analyze_operation(analyze_response=response,\n",
239-
" image_id=image_id\n",
240-
" )\n",
241-
" image = Image.open(BytesIO(raw_image))\n",
242-
" # image.show()\n",
243-
" Path(\".cache\").mkdir(exist_ok=True)\n",
244-
" image.save(f\".cache/{image_id}.jpg\", \"JPEG\")\n",
245-
"\n",
246-
"\n",
247267
"# Initialize sets for unique face IDs and keyframe IDs\n",
248268
"face_ids = set()\n",
249269
"keyframe_ids = set()\n",
@@ -273,11 +293,11 @@
273293
"\n",
274294
"# Save all face images\n",
275295
"for face_id in face_ids:\n",
276-
" save_image(face_id)\n",
296+
" save_image(face_id, response)\n",
277297
"\n",
278298
"# Save all keyframe images\n",
279299
"for keyframe_id in keyframe_ids:\n",
280-
" save_image(keyframe_id)"
300+
" save_image(keyframe_id, response)"
281301
]
282302
},
283303
{

0 commit comments

Comments
 (0)