V
V
Vladimimary2020-05-22 09:31:47
Django
Vladimimary, 2020-05-22 09:31:47

How to process uploaded image and display result on screen with Django?

I'm uploading an image via a form. But I need to process it and display the result on the screen.

Image class (models.py file)

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)
        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')


Accordingly, recognize should process.

Throws the following error.
5ec771afcda6a307992307.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-05-22
@bacon

If you do not want to read the docks, then explore on your own what self.file contains, see dir'om what fields and methods it has.
cv2.imread(self.file.path)
PS well, the error must be shown in full and in text, and not in a picture

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question