在 Django 中使用 Opencv 调用 Python 函数
Call Python Function with Opencv in Django
我有一个python人脸识别功能。我正在用 django 上传图片。在 django 中给了我一条图像路径。所以我想调用 python 函数来发送函数路径。这个python函数是做人脸识别和return识别图片路径。所以我想在 Django 中调用这个 python 函数。然后我可以在 django 中显示。
我的post方法:
def list(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('list'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render_to_response(
'myapp/list.html',
{'documents': documents, 'form': form},
context_instance=RequestContext(request)
)`
list.html:
<!-- List of uploaded documents -->
{% if documents %}
{% for document in documents %}
<article class="one_quarter" style="margin-bottom:6px;">
<figure>
<img src="{{ document.docfile.url }}" width="215" height="315" alt="{{ document.docfile.name }}">
<figcaption>Adsız</figcaption>
</figure>
</article>
{% endfor %}
{% else %}
<p>Resim Bulunamadı</p>
{% endif %}
<div>
<!-- Upload form. Note enctype attribute! -->
<form action="{% url "list" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} </p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
</p>
<p><input type="submit" value="Upload" /></p>
我上传图片的时候,这个python函数取到图片路径和测试图片文件夹路径,然后应该return到识别的图片路径。然后我想显示哪个图像路径 returned.
Python 图书馆:
import cv2, os
import numpy as np
from PIL import Image
和python中的函数定义:
def get_face_recog(path, image_path)
...
return frimage
调用 python 函数的最简单方法是在您的 视图 中,而不是在您的模板中。你可以这样做:
def my_view(request):
image_url = get_face_recog(path, image_path)
return render(request, 'mytemplate.html', {'image_url': image_url})
然后在您的模板中:
<img src="{{ MEDIA_URL }}{{ image_url }}" />
如果你真的需要从你的模板调用函数,你可以写一个custom tag or filter。
您需要配置 MEDIA_ROOT
和 MEDIA_URL
设置,并确保您的服务器正在为图像所在的目录提供服务。
我有一个python人脸识别功能。我正在用 django 上传图片。在 django 中给了我一条图像路径。所以我想调用 python 函数来发送函数路径。这个python函数是做人脸识别和return识别图片路径。所以我想在 Django 中调用这个 python 函数。然后我可以在 django 中显示。
我的post方法:
def list(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('list'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render_to_response(
'myapp/list.html',
{'documents': documents, 'form': form},
context_instance=RequestContext(request)
)`
list.html:
<!-- List of uploaded documents -->
{% if documents %}
{% for document in documents %}
<article class="one_quarter" style="margin-bottom:6px;">
<figure>
<img src="{{ document.docfile.url }}" width="215" height="315" alt="{{ document.docfile.name }}">
<figcaption>Adsız</figcaption>
</figure>
</article>
{% endfor %}
{% else %}
<p>Resim Bulunamadı</p>
{% endif %}
<div>
<!-- Upload form. Note enctype attribute! -->
<form action="{% url "list" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} </p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
</p>
<p><input type="submit" value="Upload" /></p>
我上传图片的时候,这个python函数取到图片路径和测试图片文件夹路径,然后应该return到识别的图片路径。然后我想显示哪个图像路径 returned.
Python 图书馆:
import cv2, os
import numpy as np
from PIL import Image
和python中的函数定义:
def get_face_recog(path, image_path)
...
return frimage
调用 python 函数的最简单方法是在您的 视图 中,而不是在您的模板中。你可以这样做:
def my_view(request):
image_url = get_face_recog(path, image_path)
return render(request, 'mytemplate.html', {'image_url': image_url})
然后在您的模板中:
<img src="{{ MEDIA_URL }}{{ image_url }}" />
如果你真的需要从你的模板调用函数,你可以写一个custom tag or filter。
您需要配置 MEDIA_ROOT
和 MEDIA_URL
设置,并确保您的服务器正在为图像所在的目录提供服务。