基础
人脸识别face_recognition编程案例-CSDN博客
案例
需求一、编写代码实现“人脸库注册”服务
post http://127.0.0.1:8000/faceregister 请求参数:imgFile:图片base64编码cardid:身份证ID
步骤一:创建sourceai/controller/face_view.py文件,内容如下:
from django.http import HttpResponse
import json
from sourceai.model.face.custom import base64util
def custom_facec_regiter(request):
try:
base64 =request.POST.get('imgFile')
cardid = request.POST.get('cardid')
print(base64)
print(cardid)
base64 = base64.replace(' ','+')
# base64 = 'iVBORw0KGgoAAAANSUhEUgAAAZMAAAF...'
# cardid = 'z_test_6'
path = 'static/facedb/' + cardid + '.jpg'
res =base64util.base2picture(base64,path)
except Exception as e:
print('出现问题',e)
res = ''
return HttpResponse(json.dumps({"res": res}))
步骤二:soft863ai/urls.py中新增如下内容:
from sourceai.controller import face_view
path('faceregister', face_view.custom_facec_regiter),
全部内容如下:
"""soft863ai URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from sourceai import views
from sourceai.controller import imgc_view
from sourceai.controller import nlp_view
from sourceai.controller import chat_view
from sourceai.controller import draw_view
from sourceai.controller import ppt_view
from sourceai.controller import face_view
urlpatterns = [
path('admin/', admin.site.urls),
path('first', views.first_index),
path('firstvalue', views.first_value),
path('urltotext', views.url_to_text),
path('ocrurl', views.ocr_index_url),
path('imgtotext', views.img_to_text),
path('ocrindex', views.ocr_index),
path('imgc', imgc_view.imgc_index),
path('imgcto', imgc_view.img_to_text),
path('poem', nlp_view.poem_index),
path('poemto', nlp_view.poem_to_create),
path('chat', chat_view.chat_index),
path('chatto', chat_view.chat_to_create),
path('sparkchat', chat_view.spark_index),
path('sparkchatto', chat_view.spark_to_create),
path('baichuan2chat', chat_view.baichuan2_index),
path('baichuan2chatto', chat_view.baichuan2_to_create),
path('sparkdrawindex', draw_view.sparkdraw_index),
path('sparkdrawto', draw_view.sparkdraw_to_create),
path('sparkppt', ppt_view.ppt_index),
path('sparkpptto', ppt_view.ppt_to_create),
path('faceregister', face_view.custom_facec_regiter),
]
步骤三: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文件中添加内容如下:
from django.shortcuts import render
from django.http import HttpResponse
import json
from sourceai.model.face.custom import facesearch
from sourceai.model.face.custom import base64util
# 返回图像识别结果
def custom_facec_search(request):
dict_data = {}
try:
base64 =request.POST.get('imgFile')
base64 = base64.replace(' ', '+')
import time
path = 'static/test/' +str(time.time()) + '.jpg'
res =base64util.base2picture(base64,path)
if res:
train = 'static/facedb/'
result = facesearch.start(train, path)
dict_data['flag'] = 1
dict_data['msg'] = '成功'
dict_data['cardid'] = result
else:
result=''
dict_data['flag'] = 0
dict_data['msg'] = '未匹配到人脸数据'
dict_data['cardid'] = result
except Exception as e:
print('出现问题',e)
dict_data['flag'] = 0
dict_data['msg'] = '异常'
dict_data['cardid'] = ''
return HttpResponse(json.dumps({"res": dict_data}))
步骤二:soft863ai/urls.py中新增如下内容:
from sourceai.controller import face_view
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文件中添加内容如下:
def custom_facec_list(request):
# if request.POST:
list = []
try:
import os
path = 'static/facedb/'
datanames = os.listdir(path)
list = []
for i in datanames:
list.append(i)
print(list)
except Exception as e:
print('出现问题',e)
return HttpResponse(json.dumps({"res": list}))
步骤二:soft863ai/urls.py中新增如下内容:
from sourceai.controller import face_view
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文件中添加内容如下:
def custom_facec_get(request):
# if request.POST:
dict_data = {}
try:
name = request.GET.get('cardid')
path = 'facedb/'+name+'.jpg'
strbase64 = base64util.pic2base64(path)
dict_data = {}
dict_data['path'] = path
dict_data['img'] = strbase64
except Exception as e:
print('出现问题',e)
return HttpResponse(json.dumps({"res": dict_data}))
步骤二:soft863ai/urls.py中新增如下内容:
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'
解决办法
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路径中不要有中文
版权归原作者 数智侠 所有, 如有侵权,请联系我们删除。