|
80 | 80 | "outputs": [],
|
81 | 81 | "source": [
|
82 | 82 | "import os\n",
|
| 83 | + "import uuid\n", |
83 | 84 | "folder_path = \"../data/face/enrollment_data\" # Replace with the path to your folder containing subfolders of images\n",
|
84 | 85 | "\n",
|
85 | 86 | "# Create a person directory\n",
|
86 |
| - "person_directory_id = \"person_directory_id\"\n", |
| 87 | + "person_directory_id = f\"person_directory_id_{uuid.uuid4().hex[:8]}\"\n", |
87 | 88 | "client.create_person_directory(person_directory_id)\n",
|
| 89 | + "print(f\"Created person directory with ID: {person_directory_id}\")\n", |
88 | 90 | "\n",
|
89 |
| - "person_ids = []\n", |
90 | 91 | "# Iterate through all subfolders in the folder_path\n",
|
91 | 92 | "for subfolder_name in os.listdir(folder_path):\n",
|
92 | 93 | " subfolder_path = os.path.join(folder_path, subfolder_name)\n",
|
|
96 | 97 | " person = client.add_person(person_directory_id, tags={\"name\": person_name})\n",
|
97 | 98 | " print(f\"Created person {person_name} with person_id: {person['personId']}\")\n",
|
98 | 99 | " if person:\n",
|
99 |
| - " person_ids.append(person['personId'])\n", |
100 |
| - " # Iterate through all images in the subfolder\n", |
101 |
| - " for filename in os.listdir(subfolder_path):\n", |
102 |
| - " if filename.lower().endswith(('.png', '.jpg', '.jpeg')):\n", |
103 |
| - " image_path = os.path.join(subfolder_path, filename)\n", |
104 |
| - " # Convert image to base64\n", |
105 |
| - " image_data = AzureContentUnderstandingFaceClient.read_file_to_base64(image_path)\n", |
106 |
| - " # Add a face to the Person Directory and associate it to the added person\n", |
107 |
| - " face = client.add_face(person_directory_id, image_data, person['personId'])\n", |
108 |
| - " if face:\n", |
109 |
| - " print(f\"Added face from {filename} with face_id: {face['faceId']} to person_id: {person['personId']}\")\n", |
110 |
| - " else:\n", |
111 |
| - " print(f\"Failed to add face from {filename} to person_id: {person['personId']}\")\n", |
112 |
| - "\n", |
113 |
| - "# Output the person IDs created\n", |
114 |
| - "print(f\"Person IDs: {person_ids}\")\n", |
| 100 | + " # Iterate through all images in the subfolder\n", |
| 101 | + " for filename in os.listdir(subfolder_path):\n", |
| 102 | + " if filename.lower().endswith(('.png', '.jpg', '.jpeg')):\n", |
| 103 | + " image_path = os.path.join(subfolder_path, filename)\n", |
| 104 | + " # Convert image to base64\n", |
| 105 | + " image_data = AzureContentUnderstandingFaceClient.read_file_to_base64(image_path)\n", |
| 106 | + " # Add a face to the Person Directory and associate it to the added person\n", |
| 107 | + " face = client.add_face(person_directory_id, image_data, person['personId'])\n", |
| 108 | + " if face:\n", |
| 109 | + " print(f\"Added face from {filename} with face_id: {face['faceId']} to person_id: {person['personId']}\")\n", |
| 110 | + " else:\n", |
| 111 | + " print(f\"Failed to add face from {filename} to person_id: {person['personId']}\")\n", |
115 | 112 | "\n",
|
116 | 113 | "print(\"Done\")"
|
117 | 114 | ]
|
|
135 | 132 | "test_image_path = \"../data/face/family.jpg\" # Path to the test image\n",
|
136 | 133 | "\n",
|
137 | 134 | "# Detect faces in the test image\n",
|
138 |
| - "with open(test_image_path, \"rb\") as image_file:\n", |
139 |
| - " image_data = AzureContentUnderstandingFaceClient.read_file_to_base64(image_file)\n", |
140 |
| - " detected_faces = client.detect_faces(person_directory_id, image_data)\n", |
141 |
| - " for face in detected_faces:\n", |
142 |
| - " identified_persons = client.identify_person(person_directory_id, image_data, face['boundingBox'])\n", |
143 |
| - " if identified_persons.get(\"personCandidates\"):\n", |
144 |
| - " person = identified_persons[\"personCandidates\"][0]\n", |
145 |
| - " name = person.get(\"tags\", {}).get(\"name\", \"Unknown\")\n", |
146 |
| - " print(f\"Detected person: {name} with confidence: {person.get('confidence', 0)} at bounding box: {face['boundingBox']}\")\n", |
| 135 | + "image_data = AzureContentUnderstandingFaceClient.read_file_to_base64(test_image_path)\n", |
| 136 | + "detected_faces = client.detect_faces(data=image_data)\n", |
| 137 | + "for face in detected_faces['detectedFaces']:\n", |
| 138 | + " identified_persons = client.identify_person(person_directory_id, image_data, face['boundingBox'])\n", |
| 139 | + " if identified_persons.get(\"personCandidates\"):\n", |
| 140 | + " person = identified_persons[\"personCandidates\"][0]\n", |
| 141 | + " name = person.get(\"tags\", {}).get(\"name\", \"Unknown\")\n", |
| 142 | + " print(f\"Detected person: {name} with confidence: {person.get('confidence', 0)} at bounding box: {face['boundingBox']}\")\n", |
147 | 143 | "\n",
|
148 | 144 | "print(\"Done\")"
|
149 | 145 | ]
|
|
170 | 166 | "# Convert the new face image to base64\n",
|
171 | 167 | "image_data = AzureContentUnderstandingFaceClient.read_file_to_base64(new_face_image_path)\n",
|
172 | 168 | "# Add the new face to the person directory and associate it with the existing person\n",
|
173 |
| - "face_id = client.add_face(person_directory_id, image_data, person['personId'])\n", |
174 |
| - "if face_id:\n", |
175 |
| - " print(f\"Added face from {new_face_image_path} with face_id: {face_id} to person_id: {existing_person_id}\")\n", |
| 169 | + "face = client.add_face(person_directory_id, image_data, person['personId'])\n", |
| 170 | + "if face:\n", |
| 171 | + " print(f\"Added face from {new_face_image_path} with face_id: {face['faceId']} to person_id: {existing_person_id}\")\n", |
176 | 172 | "else:\n",
|
177 | 173 | " print(f\"Failed to add face from {new_face_image_path} to person_id: {existing_person_id}\")"
|
178 | 174 | ]
|
|
220 | 216 | "existing_face_id = \"existing_face_id\" # The unique ID of the face.\n",
|
221 | 217 | "\n",
|
222 | 218 | "# Remove the association of the existing face ID from the person\n",
|
223 |
| - "client.update_face(person_directory_id, existing_face_id, person_id=\"null\") # The person_id is set to \"null\" to remove the association\n", |
| 219 | + "client.update_face(person_directory_id, existing_face_id, person_id=\"\") # The person_id is set to \"\" to remove the association\n", |
| 220 | + "print(f\"Removed association of face_id: {existing_face_id} from the existing person_id\")\n", |
| 221 | + "print(client.get_face(person_directory_id, existing_face_id)) # This will return the face information without the person association\n", |
224 | 222 | "\n",
|
225 | 223 | "# Associate the existing face ID with a person\n",
|
226 | 224 | "existing_person_id = \"existing_person_id\" # The unique ID of the person to be associated with the face.\n",
|
227 |
| - "client.update_face(person_directory_id, existing_face_id, person_id=existing_person_id)" |
| 225 | + "client.update_face(person_directory_id, existing_face_id, person_id=existing_person_id)\n", |
| 226 | + "print(f\"Associated face_id: {existing_face_id} with person_id: {existing_person_id}\")\n", |
| 227 | + "print(client.get_face(person_directory_id, existing_face_id)) # This will return the face information with the new person association" |
228 | 228 | ]
|
229 | 229 | },
|
230 | 230 | {
|
|
253 | 253 | " tags=person_directory_tags\n",
|
254 | 254 | ")\n",
|
255 | 255 | "print(f\"Updated Person Directory with description: '{person_directory_description}' and tags: {person_directory_tags}\")\n",
|
| 256 | + "print(client.get_person_directory(person_directory_id)) # This will return the updated person directory information\n", |
256 | 257 | "\n",
|
257 | 258 | "# Update the tags for an individual person\n",
|
258 | 259 | "existing_person_id = \"existing_person_id\" # The unique ID of the person to update.\n",
|
259 |
| - "person_tags = {\"role\": \"tester\", \"department\": \"engineering\"}\n", |
| 260 | + "person_tags = {\"role\": \"tester\", \"department\": \"engineering\", \"name\": \"\"} # This will remove the name tag from the person.\n", |
260 | 261 | "\n",
|
261 | 262 | "client.update_person(\n",
|
262 | 263 | " person_directory_id,\n",
|
263 | 264 | " existing_person_id,\n",
|
264 | 265 | " tags=person_tags\n",
|
265 | 266 | ")\n",
|
266 |
| - "print(f\"Updated person with person_id: {existing_person_id} with tags: {person_tags}\")" |
| 267 | + "print(f\"Updated person with person_id: {existing_person_id} with tags: {person_tags}\")\n", |
| 268 | + "print(client.get_person(person_directory_id, existing_person_id)) # This will return the updated person information" |
267 | 269 | ]
|
268 | 270 | },
|
269 | 271 | {
|
|
284 | 286 | "source": [
|
285 | 287 | "existing_face_id = \"existing_face_id\" # The unique ID of the face to delete.\n",
|
286 | 288 | "\n",
|
287 |
| - "if client.delete_face(person_directory_id, existing_face_id):\n", |
288 |
| - " print(f\"Deleted face with face_id: {existing_face_id}\")\n", |
289 |
| - "else:\n", |
290 |
| - " print(f\"Failed to delete face with face_id: {existing_face_id}\")" |
| 289 | + "client.delete_face(person_directory_id, existing_face_id)\n", |
| 290 | + "print(f\"Deleted face with face_id: {existing_face_id}\")" |
291 | 291 | ]
|
292 | 292 | },
|
293 | 293 | {
|
|
308 | 308 | "outputs": [],
|
309 | 309 | "source": [
|
310 | 310 | "existing_person_id = \"existing_person_id\" # The unique ID of the person to delete.\n",
|
311 |
| - "if client.delete_person(person_directory_id, existing_person_id):\n", |
312 |
| - " print(f\"Deleted person with person_id: {existing_person_id}\")\n", |
313 |
| - "else:\n", |
314 |
| - " print(f\"Failed to delete person with person_id: {existing_person_id}\")" |
| 311 | + "\n", |
| 312 | + "client.delete_person(person_directory_id, existing_person_id)\n", |
| 313 | + "print(f\"Deleted person with person_id: {existing_person_id}\")" |
315 | 314 | ]
|
316 | 315 | },
|
317 | 316 | {
|
|
339 | 338 | "\n",
|
340 | 339 | "# Delete each face associated with the person\n",
|
341 | 340 | "for face_id in face_ids:\n",
|
342 |
| - " if client.delete_face(person_directory_id, face_id):\n", |
343 |
| - " print(f\"Deleted face with face_id: {face_id}\")\n", |
344 |
| - " else:\n", |
345 |
| - " print(f\"Failed to delete face with face_id: {face_id}\")\n", |
| 341 | + " print(f\"Deleting face with face_id: {face_id} from person_id: {existing_person_id}\")\n", |
| 342 | + " client.delete_face(person_directory_id, face_id)\n", |
346 | 343 | "\n",
|
347 | 344 | "# Delete the person after deleting all associated faces\n",
|
348 |
| - "if client.delete_person(person_directory_id, existing_person_id):\n", |
349 |
| - " print(f\"Deleted person with person_id: {existing_person_id}\")\n", |
350 |
| - "else:\n", |
351 |
| - " print(f\"Failed to delete person with person_id: {existing_person_id}\")" |
| 345 | + "client.delete_person(person_directory_id, existing_person_id)\n", |
| 346 | + "print(f\"Deleted person with person_id: {existing_person_id} and all associated faces.\")" |
352 | 347 | ]
|
353 | 348 | }
|
354 | 349 | ],
|
|
0 commit comments