В моем приложении 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
forms.py
from django import forms
class ImageUploadForm(forms.Form):
image = forms.ImageField()
views.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})
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