Face_recognition: Doesn't find any face in webcam images sent as base64 over API

Created on 18 Jan 2018  路  3Comments  路  Source: ageitgey/face_recognition

  • 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 []

Most helpful comment

import io,base64
import face_recognition

def get_face_encoding_from_base64(base64String):
image = face_recognition.load_image_file(io.BytesIO(base64.b64decode(base64string)))
image_encodeing = face_recognition.face_encodings(image)
return image_encodeing

All 3 comments

Image.Open? Image is not defined

import io,base64
import face_recognition

def get_face_encoding_from_base64(base64String):
image = face_recognition.load_image_file(io.BytesIO(base64.b64decode(base64string)))
image_encodeing = face_recognition.face_encodings(image)
return image_encodeing

@joastonish i tried the base64 with your way..its working with camera images or internet images but not with webcam images..can u pls help me with this

Was this page helpful?
0 / 5 - 0 ratings