Skip to content

Commit be72448

Browse files
changjian-wangChangjian Wang
andauthored
Enhance person directory notebook by initializing persons list (#78)
* Enhance person directory notebook by initializing persons list and updating face association logic * Refactor face ID association logic to use the entire list of face IDs for a person * Update Python version in Jupyter notebook metadata to 3.13.7 --------- Co-authored-by: Changjian Wang <[email protected]>
1 parent 49c8fbe commit be72448

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed
Lines changed: 3 additions & 0 deletions
Loading
File renamed without changes.

notebooks/build_person_directory.ipynb

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@
9898
"client.create_person_directory(person_directory_id)\n",
9999
"logging.info(f\"Created person directory with ID: {person_directory_id}\")\n",
100100
"\n",
101+
"# Initialize persons list\n",
102+
"persons: list = []\n",
103+
"\n",
101104
"# Iterate through all subfolders in the folder_path\n",
102105
"for subfolder_name in os.listdir(folder_path):\n",
103106
" subfolder_path = os.path.join(folder_path, subfolder_name)\n",
@@ -107,6 +110,12 @@
107110
" person = client.add_person(person_directory_id, tags={\"name\": person_name})\n",
108111
" logging.info(f\"Created person {person_name} with person_id: {person['personId']}\")\n",
109112
" if person:\n",
113+
" # Initialize person entry in persons list\n",
114+
" person_entry = {\n",
115+
" 'personId': person['personId'],\n",
116+
" 'name': person_name,\n",
117+
" 'faceIds': []\n",
118+
" }\n",
110119
" # Iterate through all images in the subfolder\n",
111120
" for filename in os.listdir(subfolder_path):\n",
112121
" if filename.lower().endswith(('.png', '.jpg', '.jpeg')):\n",
@@ -116,11 +125,19 @@
116125
" # Add a face to the Person Directory and associate it to the added person\n",
117126
" face = client.add_face(person_directory_id, image_data, person['personId'])\n",
118127
" if face:\n",
128+
" # Add face ID to the person's faceIds list\n",
129+
" person_entry['faceIds'].append(face['faceId'])\n",
119130
" logging.info(f\"Added face from {filename} with face_id: {face['faceId']} to person_id: {person['personId']}\")\n",
120131
" else:\n",
121132
" logging.warning(f\"Failed to add face from {filename} to person_id: {person['personId']}\")\n",
122133
"\n",
123-
"logging.info(\"Done\")"
134+
" # Add person entry to persons list\n",
135+
" persons.append(person_entry)\n",
136+
"\n",
137+
"logging.info(\"Done\")\n",
138+
"logging.info(f\"Created {len(persons)} persons:\")\n",
139+
"for person in persons:\n",
140+
" logging.info(f\"Person: {person['name']} (ID: {person['personId']}) with {len(person['faceIds'])} faces\")"
124141
]
125142
},
126143
{
@@ -170,8 +187,9 @@
170187
"metadata": {},
171188
"outputs": [],
172189
"source": [
173-
"new_face_image_path = \"new_face_image_path\" # The path to the face image you want to add.\n",
174-
"existing_person_id = \"existing_person_id\" # The unique ID of the person to whom the face should be associated.\n",
190+
"person_bill = next(person for person in persons if person['name'] == 'Bill')\n",
191+
"new_face_image_path = \"../data/face/new_face_image.jpg\" # The path to the face image you want to add.\n",
192+
"existing_person_id = person_bill['personId'] # The unique ID of the person to whom the face should be associated.\n",
175193
"\n",
176194
"# Convert the new face image to base64\n",
177195
"image_data = AzureContentUnderstandingFaceClient.read_file_to_base64(new_face_image_path)\n",
@@ -200,8 +218,8 @@
200218
"metadata": {},
201219
"outputs": [],
202220
"source": [
203-
"existing_person_id = \"existing_person_id\" # The unique ID of the person to whom the face should be associated.\n",
204-
"existing_face_id_list = [\"existing_face_id_1\", \"existing_face_id_2\"] # The list of face IDs to be associated.\n",
221+
"existing_person_id = person_bill['personId'] # The unique ID of the person to whom the face should be associated.\n",
222+
"existing_face_id_list = person_bill['faceIds'] # The list of face IDs to be associated.\n",
205223
"\n",
206224
"# Associate the existing face IDs with the existing person\n",
207225
"client.update_person(person_directory_id, existing_person_id, face_ids=existing_face_id_list)"
@@ -223,15 +241,17 @@
223241
"metadata": {},
224242
"outputs": [],
225243
"source": [
226-
"existing_face_id = \"existing_face_id\" # The unique ID of the face.\n",
244+
"person_mary = next(person for person in persons if person['name'] == 'Mary')\n",
245+
"existing_face_id = person_mary['faceIds'][0] # The unique ID of the face.\n",
227246
"\n",
228247
"# Remove the association of the existing face ID from the person\n",
229248
"client.update_face(person_directory_id, existing_face_id, person_id=\"\") # The person_id is set to \"\" to remove the association\n",
230249
"logging.info(f\"Removed association of face_id: {existing_face_id} from the existing person_id\")\n",
231250
"logging.info(client.get_face(person_directory_id, existing_face_id)) # This will return the face information without the person association\n",
232251
"\n",
233252
"# Associate the existing face ID with a person\n",
234-
"existing_person_id = \"existing_person_id\" # The unique ID of the person to be associated with the face.\n",
253+
"person_jordan = next(person for person in persons if person['name'] == 'Jordan')\n",
254+
"existing_person_id = person_jordan['personId'] # The unique ID of the person to be associated with the face.\n",
235255
"client.update_face(person_directory_id, existing_face_id, person_id=existing_person_id)\n",
236256
"logging.info(f\"Associated face_id: {existing_face_id} with person_id: {existing_person_id}\")\n",
237257
"logging.info(client.get_face(person_directory_id, existing_face_id)) # This will return the face information with the new person association"
@@ -266,7 +286,7 @@
266286
"logging.info(client.get_person_directory(person_directory_id)) # This will return the updated person directory information\n",
267287
"\n",
268288
"# Update the tags for an individual person\n",
269-
"existing_person_id = \"existing_person_id\" # The unique ID of the person to update.\n",
289+
"existing_person_id = person_bill['personId'] # The unique ID of the person to update.\n",
270290
"person_tags = {\"role\": \"tester\", \"department\": \"engineering\", \"name\": \"\"} # This will remove the name tag from the person.\n",
271291
"\n",
272292
"client.update_person(\n",
@@ -294,7 +314,7 @@
294314
"metadata": {},
295315
"outputs": [],
296316
"source": [
297-
"existing_face_id = \"existing_face_id\" # The unique ID of the face to delete.\n",
317+
"existing_face_id = person_mary['faceIds'][0] # The unique ID of the face to delete.\n",
298318
"\n",
299319
"client.delete_face(person_directory_id, existing_face_id)\n",
300320
"logging.info(f\"Deleted face with face_id: {existing_face_id}\")"
@@ -317,7 +337,7 @@
317337
"metadata": {},
318338
"outputs": [],
319339
"source": [
320-
"existing_person_id = \"existing_person_id\" # The unique ID of the person to delete.\n",
340+
"existing_person_id = person_mary['personId'] # The unique ID of the person to delete.\n",
321341
"\n",
322342
"client.delete_person(person_directory_id, existing_person_id)\n",
323343
"logging.info(f\"Deleted person with person_id: {existing_person_id}\")"
@@ -340,7 +360,7 @@
340360
"metadata": {},
341361
"outputs": [],
342362
"source": [
343-
"existing_person_id = \"existing_person_id\" # The unique ID of the person to delete.\n",
363+
"existing_person_id = person_bill['personId'] # The unique ID of the person to delete.\n",
344364
"\n",
345365
"# Get the list of face IDs associated with the person\n",
346366
"response = client.get_person(person_directory_id, existing_person_id)\n",
@@ -373,7 +393,7 @@
373393
"name": "python",
374394
"nbconvert_exporter": "python",
375395
"pygments_lexer": "ipython3",
376-
"version": "3.11.12"
396+
"version": "3.13.7"
377397
}
378398
},
379399
"nbformat": 4,

0 commit comments

Comments
 (0)