def receive_recognize(request): if not request.user.is_authenticated(): return redirect(login) if not request.is_ajax(): return redirect(handler404) photos = request.POST.getlist('photos[]') paths = [] for i, photo in enumerate(photos): ext, img = photo.split(';base64,') ext = ext.split('/')[-1] name = 'static/temp/rec' + str(i) + '.' + ext fh = open(name, 'wb') fh.write(base64.b64decode(img)) fh.close() paths.append('static/temp/rec' + str(i) + '.' + ext) utility.crop_photos(paths=paths) user_id, percentage = face_recognizer.get_image_label(*paths) name = 'Unknown' if user_id in (-1, None) else User.objects.get(id=user_id).first_name + ' ' + User \ .objects.get(id=user_id).last_name data_rec = { 'user': user_id, 'date': datetime.datetime.now(), 'inout': None } serializer = AttendanceSerializer(data=data_rec) if serializer.is_valid() and percentage == 100: serializer.save() return JsonResponse({ 'id': user_id, 'name': name, 'percentage': percentage })
def post(self, request, format=None): data = loads(request.body) operation = int(data['operation']) if operation == 100: # Sent images to recognize date = datetime.datetime.fromtimestamp(int(data['date'])) if date.hour <= 13: inout = True if date.hour <= 9: late = None else: late = True emailalert() else: inout = False late = None emailalert() paths = [] for i, photo in enumerate(data['images']): name = 'static/temp/rec' + str(i) + '.png' with open(name, 'wb') as fh: fh.write(base64.b64decode(photo)) paths.append(name) elif operation == 200: # Remote capture photos date = datetime.datetime.now() paths = utility.remote_capture(3) inout = None utility.crop_photos(paths=paths) user_id, percentage = face_recognizer.get_image_label(*paths) if user_id not in (-1, None) and percentage > 0: data_rec = { 'user': user_id, 'date': date, 'late': late, 'inout': inout } serializer = AttendanceSerializer(data=data_rec) if serializer.is_valid(): # Add DB records serializer.save() inout = Attendance.objects.last().inout print("----------------") print(inout) # Run assigned tasks #tasks.do_user_tasks(user_id, inout=inout) # Save captured images for future training # utility.add_new_user_photos(user=user_id, path=paths[0]) user = User.objects.get(id=user_id) json_data = { 'user': user.first_name + ' ' + user.last_name, 'department': user.department, 'late': late, 'inout': inout } return JsonResponse(json_data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_204_NO_CONTENT)