|
75 | 75 | " api_version=AZURE_AI_API_VERSION,\n",
|
76 | 76 | " token_provider=token_provider,\n",
|
77 | 77 | " 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" |
79 | 93 | ]
|
80 | 94 | },
|
81 | 95 | {
|
|
184 | 198 | "result = client.poll_result(response)\n",
|
185 | 199 | "\n",
|
186 | 200 | "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", |
187 | 222 | "client.delete_analyzer(ANALYZER_ID)"
|
188 | 223 | ]
|
189 | 224 | },
|
|
229 | 264 | "metadata": {},
|
230 | 265 | "outputs": [],
|
231 | 266 | "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", |
247 | 267 | "# Initialize sets for unique face IDs and keyframe IDs\n",
|
248 | 268 | "face_ids = set()\n",
|
249 | 269 | "keyframe_ids = set()\n",
|
|
273 | 293 | "\n",
|
274 | 294 | "# Save all face images\n",
|
275 | 295 | "for face_id in face_ids:\n",
|
276 |
| - " save_image(face_id)\n", |
| 296 | + " save_image(face_id, response)\n", |
277 | 297 | "\n",
|
278 | 298 | "# Save all keyframe images\n",
|
279 | 299 | "for keyframe_id in keyframe_ids:\n",
|
280 |
| - " save_image(keyframe_id)" |
| 300 | + " save_image(keyframe_id, response)" |
281 | 301 | ]
|
282 | 302 | },
|
283 | 303 | {
|
|
0 commit comments