Character detection on an image

(edited because it was sugested to clarify and format my question)

i have a proyect where i upload an image using a FileLoader, my code in Deepnote is using opencv-python and easyocr should read the text on an image and present it on a textbox text_area_2_change, also it shows it on anvil the image uploaded. the issue im having is that im not sure if im calling incorrectly the functions, im not using the correct variables or both, the code is fairly simple, but i just started using anvil, and my time for this proyect is short, can someone look at it an might point me in the right direcction?

-this is what i have in my Anvil form:

def file_loader_1_change(self, file, **event_args): 
    "This method is called when a new file is loaded into this FileLoader"
    result = anvil.server.call('analisis', file)
    
    self.image_1.source = file
    self.text_area_2_change.text = f"{resultado}"

-for the moment only the image is shown

-the variable image will read an image using cv2.read(“whatevernameimageiwantittoread”), reader tells to use easyocr to recognice the characters on the image, use the spanish lenguage and to use the cpu, not the gpu, and result will use reader.readtext to analize the image stored in image,
the second part draws rectangles around the words, and put on top of it the interpretation, the interpretation or text readed and presented is the one i want on the textbox
-this is in my Deepnote notebook:

import easyocr
import cv2
import imutils
import numpy as np
from PIL import Image
 
!apt update
!apt install ffmpeg libsm6 libxext6 -y

reader = easyocr.Reader(["es"], gpu=False)
image = cv2.imread("9.jpg")
result = reader.readtext(image, paragraph=False)

def resumeimagen(result):
    for res in result:
        print("res:", res)
        pt0 = res[0][0]
        pt1 = res[0][1]
        pt2 = res[0][2]
        pt3 = res[0][3]
        cv2.rectangle(image, pt0,(pt1[0], pt1[1] -23), (166, 56, 242), -1)
        cv2.putText(image, res[1],(pt0[0], pt0[1] -3), 2, 0.8, (255, 255, 255), 1)     
        cv2.rectangle(image, pt0, pt2, (166, 56, 242),  2)
        cv2.circle(image, pt0, 2, (255, 0, 0), 2)
        cv2.circle(image, pt1, 2, (0, 255, 0), 2)
        cv2.circle(image, pt2, 2, (0, 0, 255), 2)
        cv2.circle(image, pt3, 2, (0, 255, 255), 2)
        return res
import anvil.media
@anvil.server.callable

def analisis(file):
    with anvil.media.TempFile(file) as img:
        reader = easyocr.Reader(["es"], gpu=False)
        image = cv2.imread(img)
        
    result = reader.readtext(image, paragraph=False)
    resultado = resumeimagen()
    return result, resultado

Without a proper explanation on what you are trying to do and why your current code does not work, I don’t think anyone on the forum can help you further.

Also, please format your code properly using three backticks (```). It makes the code much easier to read and understand

maybe now the question and the intend is more clear? i edited it

Having a quick glance, the anallisis function returns two items: result, and resultado.

However, when you call it with anvil.server.call you are only returning 1 item called “result” (which will actually be a tuple I believe).

Perhpas try this:

result, resultado = anvil.server.call('analisis', file)

self.image_1.source = file #original file

self.text_area_2_change.text = resultado

There’s some other odd things, like resumeimagen being called without an argument. Anyway, I hope this helps.

1 Like

in another question i made, i wasnt able to clarify my intentions with the proyect, i want to upload an image, that image will be processed using easy ocr and open cv, i was following a tutorial here: (🔠 Reconocimiento Óptico de Caracteres (OCR) con EASYOCR | Python - OpenCV - YouTube) to that end.
here is mine with some changes
13


and my anvil form is this


how do i solve the problem i got when i want to see the text obtained from the character recognition in the text area?

It’s hard to understand things properly without a clone link but I think you can replace the line

self.text_area_2_change.text=resultado

with

self.text_area_2.text=resultado
#Provided that the name of your text area is not actually text_area_2_change
1 Like