0


基于face_recognition与Django人脸识别服务案例

基础

人脸识别face_recognition编程案例-CSDN博客

案例

需求一、编写代码实现“人脸库注册”服务

post http://127.0.0.1:8000/faceregister 请求参数:imgFile:图片base64编码cardid:身份证ID

步骤一:创建sourceai/controller/face_view.py文件,内容如下:

  1. from django.http import HttpResponse
  2. import json
  3. from sourceai.model.face.custom import base64util
  4. def custom_facec_regiter(request):
  5. try:
  6. base64 =request.POST.get('imgFile')
  7. cardid = request.POST.get('cardid')
  8. print(base64)
  9. print(cardid)
  10. base64 = base64.replace(' ','+')
  11. # base64 = 'iVBORw0KGgoAAAANSUhEUgAAAZMAAAF...'
  12. # cardid = 'z_test_6'
  13. path = 'static/facedb/' + cardid + '.jpg'
  14. res =base64util.base2picture(base64,path)
  15. except Exception as e:
  16. print('出现问题',e)
  17. res = ''
  18. return HttpResponse(json.dumps({"res": res}))

步骤二:soft863ai/urls.py中新增如下内容:

  1. from sourceai.controller import face_view
  2. path('faceregister', face_view.custom_facec_regiter),

全部内容如下:

  1. """soft863ai URL Configuration
  2. The `urlpatterns` list routes URLs to views. For more information please see:
  3. https://docs.djangoproject.com/en/3.2/topics/http/urls/
  4. Examples:
  5. Function views
  6. 1. Add an import: from my_app import views
  7. 2. Add a URL to urlpatterns: path('', views.home, name='home')
  8. Class-based views
  9. 1. Add an import: from other_app.views import Home
  10. 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
  11. Including another URLconf
  12. 1. Import the include() function: from django.urls import include, path
  13. 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
  14. """
  15. from django.contrib import admin
  16. from django.urls import path
  17. from sourceai import views
  18. from sourceai.controller import imgc_view
  19. from sourceai.controller import nlp_view
  20. from sourceai.controller import chat_view
  21. from sourceai.controller import draw_view
  22. from sourceai.controller import ppt_view
  23. from sourceai.controller import face_view
  24. urlpatterns = [
  25. path('admin/', admin.site.urls),
  26. path('first', views.first_index),
  27. path('firstvalue', views.first_value),
  28. path('urltotext', views.url_to_text),
  29. path('ocrurl', views.ocr_index_url),
  30. path('imgtotext', views.img_to_text),
  31. path('ocrindex', views.ocr_index),
  32. path('imgc', imgc_view.imgc_index),
  33. path('imgcto', imgc_view.img_to_text),
  34. path('poem', nlp_view.poem_index),
  35. path('poemto', nlp_view.poem_to_create),
  36. path('chat', chat_view.chat_index),
  37. path('chatto', chat_view.chat_to_create),
  38. path('sparkchat', chat_view.spark_index),
  39. path('sparkchatto', chat_view.spark_to_create),
  40. path('baichuan2chat', chat_view.baichuan2_index),
  41. path('baichuan2chatto', chat_view.baichuan2_to_create),
  42. path('sparkdrawindex', draw_view.sparkdraw_index),
  43. path('sparkdrawto', draw_view.sparkdraw_to_create),
  44. path('sparkppt', ppt_view.ppt_index),
  45. path('sparkpptto', ppt_view.ppt_to_create),
  46. path('faceregister', face_view.custom_facec_regiter),
  47. ]
  1. 步骤三:soft863ai/settings.py中注释MIDDLEWARE'django.middleware.csrf.CsrfViewMiddleware'

步骤四:创建static/facedb文件夹,后续将人脸库中已有图片放置其中

步骤五:启动项目

步骤六:打开Postman进行测试

浏览器输入https://www.bchrt.com/tools/image-to-base64/

选择一张图片加载后获取base64编码

打开postman,输入http://127.0.0.1:8000/faceregister

设置imgFile与cardid参数化

需求二:编写代码实现“人脸库检索”服务

post http://127.0.0.1:8000/facedbsearch 请求参数:imgFile:图片base64编码

步骤一:在sourceai/controller/face_view.py文件中添加内容如下:

  1. from django.shortcuts import render
  2. from django.http import HttpResponse
  3. import json
  4. from sourceai.model.face.custom import facesearch
  5. from sourceai.model.face.custom import base64util
  6. # 返回图像识别结果
  7. def custom_facec_search(request):
  8. dict_data = {}
  9. try:
  10. base64 =request.POST.get('imgFile')
  11. base64 = base64.replace(' ', '+')
  12. import time
  13. path = 'static/test/' +str(time.time()) + '.jpg'
  14. res =base64util.base2picture(base64,path)
  15. if res:
  16. train = 'static/facedb/'
  17. result = facesearch.start(train, path)
  18. dict_data['flag'] = 1
  19. dict_data['msg'] = '成功'
  20. dict_data['cardid'] = result
  21. else:
  22. result=''
  23. dict_data['flag'] = 0
  24. dict_data['msg'] = '未匹配到人脸数据'
  25. dict_data['cardid'] = result
  26. except Exception as e:
  27. print('出现问题',e)
  28. dict_data['flag'] = 0
  29. dict_data['msg'] = '异常'
  30. dict_data['cardid'] = ''
  31. return HttpResponse(json.dumps({"res": dict_data}))

步骤二:soft863ai/urls.py中新增如下内容:

  1. from sourceai.controller import face_view
  2. path('facedbsearch', face_view.custom_facec_search),

步骤三:soft863ai/settings.py中注释MIDDLEWARE的'django.middleware.csrf.CsrfViewMiddleware'

步骤四:确保有static/facedb文件夹,将人脸库中已有图片放置其中

步骤五:创建static/test文件夹,放置人脸搜索原始图片

步骤六:启动项目

步骤七:打开Postman进行测试

人脸库搜索http://127.0.0.1:8000/facedbsearch

需求三:编写代码实现“人脸库图片列表”服务

get http://127.0.0.1:8000/facelist

步骤一:在sourceai/controller/face_view.py文件中添加内容如下:

  1. def custom_facec_list(request):
  2. # if request.POST:
  3. list = []
  4. try:
  5. import os
  6. path = 'static/facedb/'
  7. datanames = os.listdir(path)
  8. list = []
  9. for i in datanames:
  10. list.append(i)
  11. print(list)
  12. except Exception as e:
  13. print('出现问题',e)
  14. return HttpResponse(json.dumps({"res": list}))
  1. 步骤二:soft863ai/urls.py中新增如下内容:
  1. from sourceai.controller import face_view
  2. path('facelist', face_view.custom_facec_list),

步骤三:启动项目

步骤四:打开Postman进行测试

人脸库图片列表:http://127.0.0.1:8000/facelist

需求四:编写代码实现“人脸库某个图片获取”服务

get http://127.0.0.1:8000/faceget 请求参数:cardid:身份证ID

步骤一:在sourceai/controller/face_view.py文件中添加内容如下:

  1. def custom_facec_get(request):
  2. # if request.POST:
  3. dict_data = {}
  4. try:
  5. name = request.GET.get('cardid')
  6. path = 'facedb/'+name+'.jpg'
  7. strbase64 = base64util.pic2base64(path)
  8. dict_data = {}
  9. dict_data['path'] = path
  10. dict_data['img'] = strbase64
  11. except Exception as e:
  12. print('出现问题',e)
  13. return HttpResponse(json.dumps({"res": dict_data}))

步骤二:soft863ai/urls.py中新增如下内容:

  1. path('faceget', face_view.custom_facec_get),

步骤三:启动项目

步骤四:打开Postman进行测试

人脸库某张图片获取:

获取到的img内容可以复制到https://www.bchrt.com/tools/base64-to-image/,进行图片还原

FAQ手册

1、执行报错AttributeError: module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline'

解决办法

  1. pip install --user --upgrade opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple

2、incompatible function arguments.

TypeError: call(): incompatible function arguments. The following argument types are supported: 1. (self: dlib.fhog_object_detector, image: array, upsample_num_times: int=0) -> dlib.rectangles

Invoked with: <dlib.fhog_object_detector object at 0x00000242BB795FB8>, None, 1

解决方案:查看输入路径是否存在

3、Unable to onen C:NUsers veny lib site-dackaces face recognition modelsimodels shape-predictor-68-face-landmarks.dat

解决方案:Python路径中不要有中文

标签: django 人工智能

本文转载自: https://blog.csdn.net/taogumo/article/details/142337956
版权归原作者 数智侠 所有, 如有侵权,请联系我们删除。

“基于face_recognition与Django人脸识别服务案例”的评论:

还没有评论