I have a patient list and each patient has a detail page with their information. I want to scan the patient’s face and if the face matches the existing patient’s picture, I went to render to open the details HTML page for that patient. However, I am unsure of what to call to open the details page. After I scan the face i get this error TypeError: join() argument must be str, bytes, or os.PathLike object, not ‘dict’. I have a rough idea that I need to fix in my views.py and utilities.py but just unsure of how.
utilities.py
def is_a_ajax(request):
return request.headers.get('x-requested-with') == 'XMLHttpRequest'
def get_all_encoded_faces():
# This function loads all user profile images and encodes their faces
# Retrieve all patient profiles from the database
ps = Patient.objects.all()
# Create a dictionary to hold the encoded face for each user
encoded = {}
for p in ps:
# Initialize the encoding variable with None so like default is none
encoding = None
# Load the user's profile image
face = fr.load_image_file(p.picture.path)
# Encode the face (if detected)
face_encodings = fr.face_encodings(face)
if len(face_encodings) > 0:
encoding = face_encodings[0]
else:
print("No face found in the image")
# Add the user's encoded face to the dictionary if encoding is not None
if encoding is not None:
encoded[p.id] = encoding #i believe this p. should be something else from my models
# Return the dictionary of encoded faces
return encoded
def match_face(img):
# This function takes an image as input and returns the name of the face it contains
# Load all the known faces and their encodings
faces = get_all_encoded_faces()
faces_encoded = list(faces.values())
known_face_names = list(faces.keys())
# Load the input image
img = fr.load_image_file(img)
try:
# Find the locations of all faces in the input image
face_locations = fr.face_locations(img)
# Encode the faces in the input image
unknown_face_encodings = fr.face_encodings(img, face_locations)
# Identify the faces in the input image
face_names = []
for face_encoding in unknown_face_encodings:
# Compare the encoding of the current face to the encodings of all known faces
matches = fr.compare_faces(faces_encoded, face_encoding)
# Find the known face with the closest encoding to the current face
face_distances = fr.face_distance(faces_encoded, face_encoding)
best_match_index = np.argmin(face_distances)
# If the closest known face is a match for the current face, label the face with the known name
if matches[best_match_index]:
name = known_face_names[best_match_index]
else:
name = "Patient not in system"
face_names.append(name)
# Return the name of the first face in the input image
return face_names[0]
except:
# If no faces are found in the input image or an error occurs, return False
return False
views.py
@login_required
def find_patient(request):
if is_a_ajax(request):
picture = request.POST.get('picture')
_, str_img = picture.split(';base64')
#decode base 64 into binary format
decoded_file = base64.b64decode(str_img)
y = LogPatient()
y.picture.save('uploadpatient.png', ContentFile(decoded_file))
y.save()
result = match_face(y.picture.path)
patient_exists = Patient.objects.filter(id=result).exists()
if patient_exists:
data = Patient.objects.get(id = result)
y.patient = data
y.save()
return render('details.html',{'x': data})
else:
messages.success(request, ("Face not in database! Please register."))
return JsonResponse({'success': False})