ageitgey / ageitgey/face_recognition
Doesn't find any face in webcam images sent as base64 over API
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 56.8k
- Forks
- 13.7k
- PR merge metrics
- No merged PRs in 30d
Description
- face_recognition version: 0.1.0
- Python version: 3.6
- Operating System: Windows
Description
Images taken with camera and internet images are easily and quickly recognized even when still uploaded as base64 images, but images taken with the webcam don't come up with any face with the default 'hog' mode until it is changed to the convolutional neural networks (cnn) which is a lot slower, takes about 7 - 10 secs to process one picture
What I Did
This is my django class view:
class WhoInPhoto(APIView):
def post(self, request):
photo = request.data.get('photo', '')
photo = photo.split(',')[1]
all_user_faces = UserImage.objects.all()
known_faces = [face_recognition.face_encodings(face_recognition.load_image_file(img.facepicture.path))[0] for
img in all_user_faces]
unknown_face = get_face_encoding_from_base64(photo)
if len(unknown_face) == 0:
return JsonResponse({'status': False, 'message': 'Face not found in photo uploaded'}, safe=False)
results = face_recognition.compare_faces(known_faces, unknown_face, tolerance=0.7)
if True in results:
user = all_user_faces[results.index(True)].user
return JsonResponse({'status': True, 'message': 'Are you {0} ?'.format(user.get_full_name())}, safe=False)
else:
return JsonResponse({'status': False, 'message': 'Face not found in database'}, safe=False)
Then the get_face_encoding function:
def get_face_encoding_from_base64(base64String):
try:
os.mkdir(os.path.join(MEDIA_ROOT, 'tmp'))
except FileExistsError:
pass
IMAGE = Image.open(BytesIO(b64decode(base64String)))
IMAGE.save(os.path.join(os.path.join(MEDIA_ROOT, 'tmp'), 'temp_encoding.jpg'), IMAGE.format)
image = face_recognition.load_image_file(os.path.join(os.path.join(MEDIA_ROOT, 'tmp'), 'temp_encoding.jpg'))
os.unlink(os.path.join(os.path.join(MEDIA_ROOT, 'tmp'), 'temp_encoding.jpg'))
face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")
# face_locations = face_recognition.face_locations(image)
print(face_locations)
face_encodings = face_recognition.face_encodings(image, face_locations, 2)
if len(face_encodings) > 0:
return face_encodings[0]
else:
return []
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Django WhoInPhoto view and get_face_encoding_from_base64 function, then reproduce the webcam base64 input while comparing the default HOG face_locations call with the shown CNN call. Check how the decoded and saved image is passed to face_recognition.load_image_file and face_locations; done means the reported webcam images are reliably detected with the intended model and processing path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, python
- Domain
- api, backend, computer-vision
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100