File size: 878 Bytes
2f73fd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import cv2
import numpy as np
import easyocr
import torch

# Inicializar EasyOCR
device = "cuda" if torch.cuda.is_available() else "cpu"
reader = easyocr.Reader(["en"], gpu=(device == "cuda"), verbose=False)

def extract_text_from_image(img, gpu_available):
    reader = easyocr.Reader(['en'], gpu=gpu_available, verbose=False)

    img = np.array(img) 
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

    # Resizing and blurring
    scale_factor = 2
    upscaled = cv2.resize(img, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)
    blur_img = cv2.blur(upscaled, (5, 5))
    
    all_text_found = []
    text_ = reader.readtext(blur_img, detail=1, paragraph=False, text_threshold=0.3)

    for t in text_:
        bbox, text, score = t
        if score > 0.1:  # Filter weak detections
            all_text_found.append(text)

    return all_text_found