我是Django的新手,我正在通过Django 2.0制作一个演示聊天网站。我的动机是在他们注册时保存人们的照片,并在后端运行一个面部认证python脚本(我已经通过Python上的开源face_recognition库来完成)来识别用户当他们登录。我的脚本现在使用cv2点击照片并将其发送到脸部识别引擎。
我必须将用户的照片保存在服务器端的目录中,并在用户签名后将图片名称作为用户名称 up,所以我可以运行一个面部验证器来循环遍历我的文件夹中的面部列表以找到匹配的面部。当它发现时,我可以返回名称以查询我的数据库并为该特定用户创建会话。 (我知道这是耗费时间和资源密集型的,但由于它是一个演示,我想我可以得到它。还要善意建议是否可以有更快的方式进行基于面部的身份验证)
我的用户模型看起来像这样
class User(models.Model):
username = models.CharField(max_length=100)
name = models.CharField(max_length=100)
age = models.CharField(max_length=100)
image = models.ImageFile()
Django的任何人都可以指导我这条路吗?我可以采取哪些步骤来完成这项工作?
答案 0 :(得分:0)
我假设您为python安装了OpenCV
,cv2
,numpy
肯定是face_recognition
def recognize_face(id_image, q_image):
"""
:param image: image of the face in the id
:param q_image: image of the face from the cam
:return:
"""
q_face_encoding = face_recognition.face_encodings(id_image)[0]
face_locations = face_recognition.face_locations(q_image)
face_encodings = face_recognition.face_encodings(q_image, face_locations)
# Loop through each face in this image
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
match = face_recognition.compare_faces([q_face_encoding], face_encoding)
result = False
if match[0]:
result = True
return result
def _grab_image(path=None, stream=None, url=None):
# if the path is not None, then load the image from disk
if path is not None:
image = cv2.imread(path)
# otherwise, the image does not reside on disk
else:
# if the URL is not None, then download the image
if url is not None:
resp = requests.get(url)
data = resp.text()
# if the stream is not None, then the image has been uploaded
elif stream is not None:
data = stream.read()
# convert the image to a NumPy array and then read it into
# OpenCV format
image = np.asarray(bytearray(data), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
# return the image
return image
class FaceVerifyAPIView(APIView):
http_method_names = ['get', 'post', 'options']
parser_classes = (parsers.MultiPartParser, parsers.FormParser)
renderer_classes = (renderers.JSONRenderer, )
def post(self, request, *args, **kwargs):
serializer = FaceDectSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
data = serializer.validated_data
id_image = data['id_image']
q_image = data['q_image'] # OR from request.user.image
id_image = _grab_image(stream=id_image)
q_image = _grab_image(stream=q_image)
result = recognize_face(id_image, q_image)
return JsonResponse({'message': str(result)}, status=200)
def get(self, request, *args, **kwargs):
return JsonResponse({'message': 'You\'re here but use post method'}, status=201)
拍摄他脸上的照片,拍下reference