1

I have a python function for face recognition. I am uploading image with django. and in django given to me a path of image. So i want to call the python function for send a path to function. This python function is making face recognition and return to recognition image path. So i want to call this python function in django. Then I can show as in django.

My post method:

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>

When i was uploading image, This python function take to image path and test image folder path, then should return to image path of recognition. Then i want to show which image path returned.

Python libraries :

import cv2, os
import numpy as np
from PIL import Image

and function definition in python:

def get_face_recog(path, image_path)
    ...
    return frimage
2
  • 1
    Could you explain a little more what you want to do? Can you provide a sample of the template you want to use and what you want to get back? Commented Oct 28, 2015 at 12:51
  • Actually i have a python function as i said. I am uploading image with django. and in django given to me a path of image. So i want to call the python function for send a path to function. This python function is making face recognition and return to recognition image path. So i want to call this python function in django. Then I can show as <img src="path from python function"> in django. Commented Oct 28, 2015 at 13:11

1 Answer 1

1

The easiest way to call a python function is in your view, not your template. You could do something like:

def my_view(request):
    image_url = get_face_recog(path, image_path)
    return render(request, 'mytemplate.html', {'image_url': image_url})

Then in your template:

<img src="{{ MEDIA_URL }}{{ image_url }}" />

If you really need to call the function from your template, you could write a custom tag or filter.

You'll need to configure your MEDIA_ROOT and MEDIA_URL settings, and make sure that your server is serving the directory that the images are in.

Sign up to request clarification or add additional context in comments.

7 Comments

shouldn't that be <img src="{{ image_url}}">?
Also, maybe I'm overthinking it, but don't you need to apply a static or media tag so that it will convert from location on disk to how that URL is served via the web browser?
@Foon Yes, there will be some configuration required to serve the image. The point I was trying to make is that the best place to call a python function is the view, not the template.
How can i do this for post method ? i am uploading image then i can take a path of image, i want to send image path to function, then it will return recognition image path then i can show recognition image path.
Please update your question with what you've tried so far for your post method, and explain what the problem is.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.