V
V
Vladimimary2020-05-22 10:53:55
Django
Vladimimary, 2020-05-22 10:53:55

How to display the result of processing an image in Django?

I have an Image class that looks like this:

class Image(models.Model):
    title = models.TextField()
    file = models.ImageField(upload_to='images/')

    def __str__(self):
        return self.title

    def recognise(self):
        im = cv2.imread(self.file.path)
        preprocess = "thresh"
        gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        if preprocess == "thresh":
            gray = cv2.threshold(gray, 0, 255,
                                 cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
        elif preprocess == "blur":
            gray = cv2.medianBlur(gray, 3)
        filename = "urui.png"
        cv2.imwrite(filename, gray)
        pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
        return pytesseract.image_to_string(Image.open(filename), lang='rus')


It is used when the user uploads an image on the site. Accordingly, the file is saved. And then I need to process it and display the result on the screen. How can I do that? I don’t quite understand where to prescribe what in views. I understand that recognize should be applied to the file, but where?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question