|
51 | 51 | "import json\n", |
52 | 52 | "import os\n", |
53 | 53 | "import sys\n", |
| 54 | + "import uuid\n", |
| 55 | + "from pathlib import Path\n", |
54 | 56 | "from dotenv import find_dotenv, load_dotenv\n", |
| 57 | + "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n", |
55 | 58 | "\n", |
56 | 59 | "load_dotenv(find_dotenv())\n", |
57 | 60 | "logging.basicConfig(level=logging.INFO)\n", |
58 | 61 | "\n", |
59 | 62 | "AZURE_AI_ENDPOINT = os.getenv(\"AZURE_AI_ENDPOINT\")\n", |
60 | 63 | "AZURE_AI_API_VERSION = os.getenv(\"AZURE_AI_API_VERSION\", \"2024-12-01-preview\")\n", |
61 | 64 | "\n", |
62 | | - "# Import utility package from python samples root directory\n", |
63 | | - "py_samples_root_dir = os.path.abspath(os.path.join(os.getcwd(), \"..\"))\n", |
64 | | - "sys.path.append(py_samples_root_dir)\n", |
| 65 | + "# Add the parent directory to the path to use shared modules\n", |
| 66 | + "parent_dir = Path(Path.cwd()).parent\n", |
| 67 | + "sys.path.append(str(parent_dir))\n", |
65 | 68 | "from python.content_understanding_client import AzureContentUnderstandingClient\n", |
66 | 69 | "\n", |
| 70 | + "credential = DefaultAzureCredential()\n", |
| 71 | + "token_provider = get_bearer_token_provider(credential, \"https://cognitiveservices.azure.com/.default\")\n", |
| 72 | + "\n", |
67 | 73 | "client = AzureContentUnderstandingClient(\n", |
68 | 74 | " endpoint=AZURE_AI_ENDPOINT,\n", |
69 | 75 | " api_version=AZURE_AI_API_VERSION,\n", |
| 76 | + " token_provider=token_provider,\n", |
70 | 77 | " x_ms_useragent=\"azure-ai-content-understanding-python/content_extraction\",\n", |
71 | 78 | ")" |
72 | 79 | ] |
|
90 | 97 | "name": "stderr", |
91 | 98 | "output_type": "stream", |
92 | 99 | "text": [ |
93 | | - "INFO:python.content_understanding_client:Analyzer field-extraction-sample-97d1d17d-29b6-4af1-9078-00650666fda1 create request accepted.\n", |
| 100 | + "INFO:python.content_understanding_client:Analyzer content-doc-sample-f79b4605-4990-4788-ba6a-47d55d244212 create request accepted.\n", |
94 | 101 | "INFO:python.content_understanding_client:Request result is ready after 0.00 seconds.\n", |
95 | | - "INFO:python.content_understanding_client:Analyzing file ../data/purchase_order.jpg with analyzer: field-extraction-sample-97d1d17d-29b6-4af1-9078-00650666fda1\n", |
96 | | - "INFO:python.content_understanding_client:Request cc64dcc4-0797-45d5-b18e-49c77b5b1122 in progress ...\n", |
97 | | - "INFO:python.content_understanding_client:Request cc64dcc4-0797-45d5-b18e-49c77b5b1122 in progress ...\n", |
98 | | - "INFO:python.content_understanding_client:Request result is ready after 4.37 seconds.\n" |
| 102 | + "INFO:python.content_understanding_client:Analyzing file ../data/purchase_order.jpg with analyzer: content-doc-sample-f79b4605-4990-4788-ba6a-47d55d244212\n", |
| 103 | + "INFO:python.content_understanding_client:Request 6c1f5fea-8f2e-423b-afbe-cd35872379f6 in progress ...\n", |
| 104 | + "INFO:python.content_understanding_client:Request 6c1f5fea-8f2e-423b-afbe-cd35872379f6 in progress ...\n", |
| 105 | + "INFO:python.content_understanding_client:Request result is ready after 4.56 seconds.\n" |
99 | 106 | ] |
100 | 107 | }, |
101 | 108 | { |
102 | 109 | "name": "stdout", |
103 | 110 | "output_type": "stream", |
104 | 111 | "text": [ |
105 | 112 | "{\n", |
106 | | - " \"id\": \"cc64dcc4-0797-45d5-b18e-49c77b5b1122\",\n", |
| 113 | + " \"id\": \"6c1f5fea-8f2e-423b-afbe-cd35872379f6\",\n", |
107 | 114 | " \"status\": \"Succeeded\",\n", |
108 | 115 | " \"result\": {\n", |
109 | | - " \"analyzerId\": \"field-extraction-sample-97d1d17d-29b6-4af1-9078-00650666fda1\",\n", |
| 116 | + " \"analyzerId\": \"content-doc-sample-f79b4605-4990-4788-ba6a-47d55d244212\",\n", |
110 | 117 | " \"apiVersion\": \"2024-12-01-preview\",\n", |
111 | | - " \"createdAt\": \"2024-12-10T06:50:03Z\",\n", |
| 118 | + " \"createdAt\": \"2024-12-10T23:29:52Z\",\n", |
112 | 119 | " \"warnings\": [],\n", |
113 | 120 | " \"contents\": [\n", |
114 | 121 | " {\n", |
|
133 | 140 | } |
134 | 141 | ], |
135 | 142 | "source": [ |
136 | | - "import uuid\n", |
137 | | - "\n", |
138 | 143 | "ANALYZER_ID = \"content-doc-sample-\" + str(uuid.uuid4())\n", |
139 | 144 | "ANALYZER_TEMPLATE_FILE = '../analyzer_templates/content_document.json'\n", |
140 | 145 | "ANALYZER_SAMPLE_FILE = '../data/purchase_order.jpg'\n", |
|
147 | 152 | "response = client.begin_analyze(ANALYZER_ID, file_location=ANALYZER_SAMPLE_FILE)\n", |
148 | 153 | "result = client.poll_result(response)\n", |
149 | 154 | "\n", |
150 | | - "print(json.dumps(result, indent=2))" |
| 155 | + "print(json.dumps(result, indent=2))\n", |
| 156 | + "client.delete_analyzer(ANALYZER_ID)" |
151 | 157 | ] |
152 | 158 | }, |
153 | 159 | { |
|
186 | 192 | "response = client.begin_analyze(ANALYZER_ID, file_location=ANALYZER_SAMPLE_FILE)\n", |
187 | 193 | "result = client.poll_result(response)\n", |
188 | 194 | "\n", |
189 | | - "print(json.dumps(result, indent=2))" |
| 195 | + "print(json.dumps(result, indent=2))\n", |
| 196 | + "client.delete_analyzer(ANALYZER_ID)" |
190 | 197 | ] |
191 | 198 | }, |
192 | 199 | { |
|
222 | 229 | "response = client.begin_analyze(ANALYZER_ID, file_location=ANALYZER_SAMPLE_FILE)\n", |
223 | 230 | "result = client.poll_result(response)\n", |
224 | 231 | "\n", |
| 232 | + "print(json.dumps(result, indent=2))\n", |
| 233 | + "client.delete_analyzer(ANALYZER_ID)" |
| 234 | + ] |
| 235 | + }, |
| 236 | + { |
| 237 | + "cell_type": "markdown", |
| 238 | + "metadata": {}, |
| 239 | + "source": [ |
| 240 | + "## Video Content with Face" |
| 241 | + ] |
| 242 | + }, |
| 243 | + { |
| 244 | + "cell_type": "code", |
| 245 | + "execution_count": null, |
| 246 | + "metadata": {}, |
| 247 | + "outputs": [], |
| 248 | + "source": [ |
| 249 | + "ANALYZER_ID = \"content-video-face-sample-\" + str(uuid.uuid4())\n", |
| 250 | + "ANALYZER_TEMPLATE_FILE = '../analyzer_templates/face_aware_in_video.json'\n", |
| 251 | + "ANALYZER_SAMPLE_FILE = '../data/video.mp4'\n", |
| 252 | + "\n", |
| 253 | + "# Create analyzer\n", |
| 254 | + "response = client.begin_create_analyzer(ANALYZER_ID, analyzer_template_path=ANALYZER_TEMPLATE_FILE)\n", |
| 255 | + "result = client.poll_result(response)\n", |
| 256 | + "\n", |
| 257 | + "# Analyzer file\n", |
| 258 | + "response = client.begin_analyze(ANALYZER_ID, file_location=ANALYZER_SAMPLE_FILE)\n", |
| 259 | + "result = client.poll_result(response)\n", |
| 260 | + "\n", |
225 | 261 | "print(json.dumps(result, indent=2))" |
226 | 262 | ] |
| 263 | + }, |
| 264 | + { |
| 265 | + "cell_type": "markdown", |
| 266 | + "metadata": {}, |
| 267 | + "source": [ |
| 268 | + "### Get and Save Key Frames and Face Thumbnails" |
| 269 | + ] |
| 270 | + }, |
| 271 | + { |
| 272 | + "cell_type": "code", |
| 273 | + "execution_count": null, |
| 274 | + "metadata": {}, |
| 275 | + "outputs": [], |
| 276 | + "source": [ |
| 277 | + "from PIL import Image\n", |
| 278 | + "from io import BytesIO\n", |
| 279 | + "import re\n", |
| 280 | + "\n", |
| 281 | + "\n", |
| 282 | + "def save_image(image_id: str):\n", |
| 283 | + " raw_image = client.get_image_from_analyze_operation(analyze_response=response,\n", |
| 284 | + " image_id=image_id\n", |
| 285 | + " )\n", |
| 286 | + " image = Image.open(BytesIO(raw_image))\n", |
| 287 | + " # image.show()\n", |
| 288 | + " Path(\".cache\").mkdir(exist_ok=True)\n", |
| 289 | + " image.save(f\".cache/{image_id}.jpg\", \"JPEG\")\n", |
| 290 | + "\n", |
| 291 | + "\n", |
| 292 | + "# Initialize sets for unique face IDs and keyframe IDs\n", |
| 293 | + "face_ids = set()\n", |
| 294 | + "keyframe_ids = set()\n", |
| 295 | + "\n", |
| 296 | + "# Extract unique face IDs safely\n", |
| 297 | + "result_data = result.get(\"result\", {})\n", |
| 298 | + "contents = result_data.get(\"contents\", [])\n", |
| 299 | + "\n", |
| 300 | + "# Iterate over contents to find faces and keyframes if available\n", |
| 301 | + "for content in contents:\n", |
| 302 | + " # Safely retrieve face IDs if \"faces\" exists and is a list\n", |
| 303 | + " faces = content.get(\"faces\", [])\n", |
| 304 | + " if isinstance(faces, list):\n", |
| 305 | + " for face in faces:\n", |
| 306 | + " face_id = face.get(\"faceId\")\n", |
| 307 | + " if face_id:\n", |
| 308 | + " face_ids.add(f\"face.{face_id}\")\n", |
| 309 | + "\n", |
| 310 | + " # Extract keyframe IDs from \"markdown\" if it exists and is a string\n", |
| 311 | + " markdown_content = content.get(\"markdown\", \"\")\n", |
| 312 | + " if isinstance(markdown_content, str):\n", |
| 313 | + " keyframe_ids.update(re.findall(r\"(keyFrame\\.\\d+)\\.jpg\", markdown_content))\n", |
| 314 | + "\n", |
| 315 | + "# Output the results\n", |
| 316 | + "print(\"Unique Face IDs:\", face_ids)\n", |
| 317 | + "print(\"Unique Keyframe IDs:\", keyframe_ids)\n", |
| 318 | + "\n", |
| 319 | + "# Save all face images\n", |
| 320 | + "for face_id in face_ids:\n", |
| 321 | + " save_image(face_id)\n", |
| 322 | + "\n", |
| 323 | + "# Save all keyframe images\n", |
| 324 | + "for keyframe_id in keyframe_ids:\n", |
| 325 | + " save_image(keyframe_id)" |
| 326 | + ] |
227 | 327 | } |
228 | 328 | ], |
229 | 329 | "metadata": { |
|
242 | 342 | "name": "python", |
243 | 343 | "nbconvert_exporter": "python", |
244 | 344 | "pygments_lexer": "ipython3", |
245 | | - "version": "3.8.10" |
| 345 | + "version": "3.11.11" |
246 | 346 | } |
247 | 347 | }, |
248 | 348 | "nbformat": 4, |
|
0 commit comments