python - 在 Django 中运行面部识别脚本
问题描述
我的 django 应用程序中有一个面部识别脚本,下面是代码,它返回一个字符串,它是找到的人的姓名。当我将图像参数传递给它时,该脚本本身运行良好。我想在我的views.py 中运行这个脚本,所以当我得到一个名字时,我会在我的模型中搜索这个名字然后得到一个匹配的结果。模板加载得很好,但在上传图像后我提交没有任何操作发生。
def identify_face(imagename):
known_face_encodings = []
known_face_names = []
known_faces_filenames = []
for (dirpath, dirnames, filenames) in os.walk('assets/img/users/'):
known_faces_filenames.extend(filenames)
break
for filename in known_faces_filenames:
face = face_recognition.load_image_file('assets/img/users/' + filename)
known_face_names.append(re.sub("[0-9]",'', filename[:-4]))
known_face_encodings.append(face_recognition.face_encodings(face)[0])
face_locations = []
face_encodings = []
face_names = []
imagename_new = face_recognition.load_image_file(imagename)
face_locations = face_recognition.face_locations(imagename_new)
face_encodings = face_recognition.face_encodings(imagename_new, face_locations)
face_names = []
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face_names.append(name)
return name
这是我的 forms.py 和 views.py 代码
表格.py
from django import forms
class ImageUploadForm(forms.Form):
image = forms.ImageField()
视图.py
from .models import MyData
from cisdb.forms import SearchForm,ImageUploadForm
from django.contrib.postgres.search import SearchVector
from cisdb.face_recogniton_script import identify_citizen
def citizen_match(request):
form = ImageUploadForm()
query = None
match = ''
results = []
if 'query' in request.GET:
form = ImageUploadForm(request.GET)
if form.is_valid():
query = form.cleaned_data['query']
match = identify_citizen(query)
results = MyData.objects.annotate(
search=SearchVector('first_name', 'last_name','other_name'),
).filter(search=match)
return render(request,'match.html',{'form': form,'query': query, 'results': results,'match':match})
匹配.html
{% block content %}
{% if query %}
<h3>
{% with results.count as total_results %}
Found {{ total_results }} result{{ total_results|pluralize }}
{% endwith %}
</h3>
{% for data in results %}
<div class="section">
<div class="container"></div>
<h4><a href="{{ data.get_absolute_url }}">{{ data.last_name }} {{data.first_name}} {{ data.other_name }}</a></h4>
</div>
{% empty %}
<p>No matches found.</p>
{% endfor %}
<p><a href="{% url "match" %}">Match again</a></p>
{% else %}
<h1>Match faces</h1>
<form action="." method="get" enctype="multipart/form-data">
{{ form.as_p }}
<input type="submit" value="Match">
</form>
{% endif %}
{% endblock %}
这是我从命令行中得到的全部
System check identified no issues (0 silenced).
February 05, 2020 - 13:14:21
Django version 2.2, using settings 'CIS.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[05/Feb/2020 13:14:29] "GET /admin HTTP/1.1" 301 0
[05/Feb/2020 13:14:32] "GET /admin/ HTTP/1.1" 200 4078
[05/Feb/2020 13:14:32] "GET /static/admin/css/dashboard.css HTTP/1.1" 200 412
[05/Feb/2020 13:14:32] "GET /static/admin/css/responsive.css HTTP/1.1" 200 17944
[05/Feb/2020 13:14:32] "GET /static/admin/css/base.css HTTP/1.1" 200 16378
[05/Feb/2020 13:14:40] "GET /match_data/ HTTP/1.1" 200 1597
[05/Feb/2020 13:14:40] "GET /static/css/base.css HTTP/1.1" 304 0
[05/Feb/2020 13:14:51] "GET /match_data/?image=warren2.jpg HTTP/1.1" 200 1597
解决方案
推荐阅读
- asp.net - Web.Config 自定义错误未重定向到错误页面
- artifactory - Artifactory 实例的备份 - 未导入用户
- javascript - 具有纯 CSS 内容的初始自动高度的文本区域
- java - 无法为 Spring Integration 5.0.4 (Spring 5.0.5) 创建 Kotlin 配置
- c# - UWP 应用服务线程模型
- php - 在 Woocommerce 中将单个产品页面重定向到主页
- amazon-web-services - 通过 Cloudformation-Update 堆栈修改 EC2 上的文件
- javascript - 'cursor' 选项是来自每个聚合的必需错误
- java - 使用 marklogic java 客户端 api 处理 DOM
- json - 解析更复杂的嵌套响应