Skip to content

Commit 0634f62

Browse files
author
Zhi Zhou
committed
Update sample code after testing
1 parent 8a4ab9e commit 0634f62

File tree

2 files changed

+50
-55
lines changed

2 files changed

+50
-55
lines changed

notebooks/build_person_directory.ipynb

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@
8080
"outputs": [],
8181
"source": [
8282
"import os\n",
83+
"import uuid\n",
8384
"folder_path = \"../data/face/enrollment_data\" # Replace with the path to your folder containing subfolders of images\n",
8485
"\n",
8586
"# 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",
8788
"client.create_person_directory(person_directory_id)\n",
89+
"print(f\"Created person directory with ID: {person_directory_id}\")\n",
8890
"\n",
89-
"person_ids = []\n",
9091
"# Iterate through all subfolders in the folder_path\n",
9192
"for subfolder_name in os.listdir(folder_path):\n",
9293
" subfolder_path = os.path.join(folder_path, subfolder_name)\n",
@@ -96,22 +97,18 @@
9697
" person = client.add_person(person_directory_id, tags={\"name\": person_name})\n",
9798
" print(f\"Created person {person_name} with person_id: {person['personId']}\")\n",
9899
" 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",
115112
"\n",
116113
"print(\"Done\")"
117114
]
@@ -135,15 +132,14 @@
135132
"test_image_path = \"../data/face/family.jpg\" # Path to the test image\n",
136133
"\n",
137134
"# 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",
147143
"\n",
148144
"print(\"Done\")"
149145
]
@@ -170,9 +166,9 @@
170166
"# Convert the new face image to base64\n",
171167
"image_data = AzureContentUnderstandingFaceClient.read_file_to_base64(new_face_image_path)\n",
172168
"# 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",
176172
"else:\n",
177173
" print(f\"Failed to add face from {new_face_image_path} to person_id: {existing_person_id}\")"
178174
]
@@ -220,11 +216,15 @@
220216
"existing_face_id = \"existing_face_id\" # The unique ID of the face.\n",
221217
"\n",
222218
"# 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",
224222
"\n",
225223
"# Associate the existing face ID with a person\n",
226224
"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"
228228
]
229229
},
230230
{
@@ -253,17 +253,19 @@
253253
" tags=person_directory_tags\n",
254254
")\n",
255255
"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",
256257
"\n",
257258
"# Update the tags for an individual person\n",
258259
"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",
260261
"\n",
261262
"client.update_person(\n",
262263
" person_directory_id,\n",
263264
" existing_person_id,\n",
264265
" tags=person_tags\n",
265266
")\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"
267269
]
268270
},
269271
{
@@ -284,10 +286,8 @@
284286
"source": [
285287
"existing_face_id = \"existing_face_id\" # The unique ID of the face to delete.\n",
286288
"\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}\")"
291291
]
292292
},
293293
{
@@ -308,10 +308,9 @@
308308
"outputs": [],
309309
"source": [
310310
"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}\")"
315314
]
316315
},
317316
{
@@ -339,16 +338,12 @@
339338
"\n",
340339
"# Delete each face associated with the person\n",
341340
"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",
346343
"\n",
347344
"# 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.\")"
352347
]
353348
}
354349
],

python/content_understanding_face_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def _handle_response(self, response: Response, action: str):
6060
if response.status_code == 204:
6161
self._logger.info(f"{action} completed successfully with status 204.")
6262
return None
63-
if response.status_code != 200:
63+
if response.status_code != 200 and response.status_code != 201:
6464
self._logger.error(
6565
f"Error in {action}: {response.status_code} - {response.text}"
6666
)
@@ -141,14 +141,14 @@ def delete_person_directory(self, person_directory_id: str):
141141
)
142142
return self._handle_response(response, "delete_person_directory")
143143

144-
def get_persons(self, person_directory_id: str):
144+
def list_persons(self, person_directory_id: str):
145145
response = requests.get(
146146
self._get_person_directory_url(
147147
self._endpoint, self._api_version, f"{person_directory_id}/persons"
148148
),
149149
headers=self._headers,
150150
)
151-
return self._handle_response(response, "get_persons")
151+
return self._handle_response(response, "list_persons")
152152

153153
def get_person(self, person_directory_id: str, person_id: str):
154154
response = requests.get(
@@ -209,7 +209,7 @@ def delete_person(self, person_directory_id: str, person_id: str):
209209
)
210210
return self._handle_response(response, "delete_person")
211211

212-
def get_faces(self, person_directory_id: str):
212+
def list_faces(self, person_directory_id: str):
213213
response = requests.get(
214214
self._get_person_directory_url(
215215
self._endpoint,
@@ -218,7 +218,7 @@ def get_faces(self, person_directory_id: str):
218218
),
219219
headers=self._headers,
220220
)
221-
return self._handle_response(response, "get_faces")
221+
return self._handle_response(response, "list_faces")
222222

223223
def get_face(self, person_directory_id: str, face_id: str):
224224
response = requests.get(

0 commit comments

Comments
 (0)