Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions data/face/enrollment_data/Mary/Family1-Daughter3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 32 additions & 12 deletions notebooks/build_person_directory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
"client.create_person_directory(person_directory_id)\n",
"logging.info(f\"Created person directory with ID: {person_directory_id}\")\n",
"\n",
"# Initialize persons list\n",
"persons: list = []\n",
"\n",
"# Iterate through all subfolders in the folder_path\n",
"for subfolder_name in os.listdir(folder_path):\n",
" subfolder_path = os.path.join(folder_path, subfolder_name)\n",
Expand All @@ -107,6 +110,12 @@
" person = client.add_person(person_directory_id, tags={\"name\": person_name})\n",
" logging.info(f\"Created person {person_name} with person_id: {person['personId']}\")\n",
" if person:\n",
" # Initialize person entry in persons list\n",
" person_entry = {\n",
" 'personId': person['personId'],\n",
" 'name': person_name,\n",
" 'faceIds': []\n",
" }\n",
" # Iterate through all images in the subfolder\n",
" for filename in os.listdir(subfolder_path):\n",
" if filename.lower().endswith(('.png', '.jpg', '.jpeg')):\n",
Expand All @@ -116,11 +125,19 @@
" # Add a face to the Person Directory and associate it to the added person\n",
" face = client.add_face(person_directory_id, image_data, person['personId'])\n",
" if face:\n",
" # Add face ID to the person's faceIds list\n",
" person_entry['faceIds'].append(face['faceId'])\n",
" logging.info(f\"Added face from {filename} with face_id: {face['faceId']} to person_id: {person['personId']}\")\n",
" else:\n",
" logging.warning(f\"Failed to add face from {filename} to person_id: {person['personId']}\")\n",
"\n",
"logging.info(\"Done\")"
" # Add person entry to persons list\n",
" persons.append(person_entry)\n",
"\n",
"logging.info(\"Done\")\n",
"logging.info(f\"Created {len(persons)} persons:\")\n",
"for person in persons:\n",
" logging.info(f\"Person: {person['name']} (ID: {person['personId']}) with {len(person['faceIds'])} faces\")"
]
},
{
Expand Down Expand Up @@ -170,8 +187,9 @@
"metadata": {},
"outputs": [],
"source": [
"new_face_image_path = \"new_face_image_path\" # The path to the face image you want to add.\n",
"existing_person_id = \"existing_person_id\" # The unique ID of the person to whom the face should be associated.\n",
"person_bill = next(person for person in persons if person['name'] == 'Bill')\n",
"new_face_image_path = \"../data/face/new_face_image.jpg\" # The path to the face image you want to add.\n",
"existing_person_id = person_bill['personId'] # The unique ID of the person to whom the face should be associated.\n",
"\n",
"# Convert the new face image to base64\n",
"image_data = AzureContentUnderstandingFaceClient.read_file_to_base64(new_face_image_path)\n",
Expand Down Expand Up @@ -200,8 +218,8 @@
"metadata": {},
"outputs": [],
"source": [
"existing_person_id = \"existing_person_id\" # The unique ID of the person to whom the face should be associated.\n",
"existing_face_id_list = [\"existing_face_id_1\", \"existing_face_id_2\"] # The list of face IDs to be associated.\n",
"existing_person_id = person_bill['personId'] # The unique ID of the person to whom the face should be associated.\n",
"existing_face_id_list = [person_bill['faceIds'][0], person_bill['faceIds'][1], person_bill['faceIds'][2]] # The list of face IDs to be associated.\n",
"\n",
"# Associate the existing face IDs with the existing person\n",
"client.update_person(person_directory_id, existing_person_id, face_ids=existing_face_id_list)"
Expand All @@ -223,15 +241,17 @@
"metadata": {},
"outputs": [],
"source": [
"existing_face_id = \"existing_face_id\" # The unique ID of the face.\n",
"person_mary = next(person for person in persons if person['name'] == 'Mary')\n",
"existing_face_id = person_mary['faceIds'][0] # The unique ID of the face.\n",
"\n",
"# Remove the association of the existing face ID from the person\n",
"client.update_face(person_directory_id, existing_face_id, person_id=\"\") # The person_id is set to \"\" to remove the association\n",
"logging.info(f\"Removed association of face_id: {existing_face_id} from the existing person_id\")\n",
"logging.info(client.get_face(person_directory_id, existing_face_id)) # This will return the face information without the person association\n",
"\n",
"# Associate the existing face ID with a person\n",
"existing_person_id = \"existing_person_id\" # The unique ID of the person to be associated with the face.\n",
"person_jordan = next(person for person in persons if person['name'] == 'Jordan')\n",
"existing_person_id = person_jordan['personId'] # The unique ID of the person to be associated with the face.\n",
"client.update_face(person_directory_id, existing_face_id, person_id=existing_person_id)\n",
"logging.info(f\"Associated face_id: {existing_face_id} with person_id: {existing_person_id}\")\n",
"logging.info(client.get_face(person_directory_id, existing_face_id)) # This will return the face information with the new person association"
Expand Down Expand Up @@ -266,7 +286,7 @@
"logging.info(client.get_person_directory(person_directory_id)) # This will return the updated person directory information\n",
"\n",
"# Update the tags for an individual person\n",
"existing_person_id = \"existing_person_id\" # The unique ID of the person to update.\n",
"existing_person_id = person_bill['personId'] # The unique ID of the person to update.\n",
"person_tags = {\"role\": \"tester\", \"department\": \"engineering\", \"name\": \"\"} # This will remove the name tag from the person.\n",
"\n",
"client.update_person(\n",
Expand Down Expand Up @@ -294,7 +314,7 @@
"metadata": {},
"outputs": [],
"source": [
"existing_face_id = \"existing_face_id\" # The unique ID of the face to delete.\n",
"existing_face_id = person_mary['faceIds'][0] # The unique ID of the face to delete.\n",
"\n",
"client.delete_face(person_directory_id, existing_face_id)\n",
"logging.info(f\"Deleted face with face_id: {existing_face_id}\")"
Expand All @@ -317,7 +337,7 @@
"metadata": {},
"outputs": [],
"source": [
"existing_person_id = \"existing_person_id\" # The unique ID of the person to delete.\n",
"existing_person_id = person_mary['personId'] # The unique ID of the person to delete.\n",
"\n",
"client.delete_person(person_directory_id, existing_person_id)\n",
"logging.info(f\"Deleted person with person_id: {existing_person_id}\")"
Expand All @@ -340,7 +360,7 @@
"metadata": {},
"outputs": [],
"source": [
"existing_person_id = \"existing_person_id\" # The unique ID of the person to delete.\n",
"existing_person_id = person_bill['personId'] # The unique ID of the person to delete.\n",
"\n",
"# Get the list of face IDs associated with the person\n",
"response = client.get_person(person_directory_id, existing_person_id)\n",
Expand Down Expand Up @@ -373,7 +393,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.12"
"version": "3.11.13"
}
},
"nbformat": 4,
Expand Down