How to use a Python function in Django

Asked

Viewed 161 times

1

I’m currently having a problem where I haven’t found any solution that I’ve been able to understand and execute. I will be summarizing here the scope of what I need, otherwise it would be very extensive.

In one work, I did some functions in Python that involve image processing in Python, in which I use libraries like Opencv and Pytesseract, to get a better idea on the subject, can consult a question of mine here in Stack on the subject, as:https://stackoverflow.com/questions/61599703/detecting-warm-colors-in-the-python-image

The function I created basically receives a directory that contains images or an isolated image (in . jpg), does the necessary processes and returns the image with hot spot location indications (as shown below) and temperature at this point (a float number).

Resultado da função de ponto quente

Note: The blue circle, Coodinate and Value are function results and are not part of the original image.

With this, I would like to create a web application that applies these functions, using Django, by already having this interaction with Python. My idea is to create a page that I can enter with the images I want to analyze, through a Forms (this part is already ready, I can enter the images through the Uploading Multiple files and save them in the database).

On another page, I would like to put a button that performs this function on the previously saved images and save the results (image .jpg and the float value) in the database, so I can show them in the template for user view. It is this part that I am not able to evolve, I found some tutorials that speak in Ajax, but none of them could apply. I found others that indicated to perform this function in the views.py, but also no result appeared.

I would like to know if it is possible to do this kind of application and if so, how could I do it? I accept indication of tutorials, examples, anyway, anything that can help me.

  • 2

    I suggest you return the post to the question and then post the solution as an answer, then mark it as an accepted answer. This will help other people with a similar problem

  • 2

    @Evilmaax is right.. Any questions read: I can answer my own question?

  • 1

    When so edit the question by removing the answer and add the answer as being a wiki.

1 answer

0

My own solution to the question.

Actually, it was simpler than I thought. I just needed to call the function to carry out the processes that it would already do and then save this data in the database. Below are the views.py and models.py files

view.py
def WarmView(request):
if str(request.method) == 'GET':
    image_path = r"C:\Users\bruno\PycharmProjects\teste3\media\termografias"
    for name_file in sorted(os.listdir(image_path)):
        img = cv2.imread(os.path.join(image_path, name_file))
        temperatura = point.show_warm_point(img, name_file) # Função que executa o processo desejado

        teste_temp = fr"C:\Users\bruno\PycharmProjects\teste3\media\processadas\{name_file}"

        image = Processings()
        image.images = f"{teste_temp}"
        Processings.objects.create(images=image.images, temperature=temperatura)

models.py
class Processings(models.Model):
images = models.ImageField('Processada', upload_to='processadas')
temperature = models.DecimalField('Temperatura', max_digits=5, decimal_places=2)
created = models.DateTimeField(auto_now=True)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.