ageitgey / face_recognition

The world's simplest facial recognition api for Python and the command line
MIT License
52.89k stars 13.44k forks source link

Doesn't find any face in webcam images sent as base64 over API #307

Open damey2011 opened 6 years ago

damey2011 commented 6 years ago

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

joastonish commented 6 years ago

Image.Open? Image is not defined

joastonish commented 6 years ago

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

Maninarang commented 6 years ago

@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

Bah1996 commented 2 years ago

@damey2011 can you give me the program code you used??