| import cv2 |
| import numpy as np |
| import easyocr |
| import torch |
|
|
| |
| 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) |
|
|
| |
| 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: |
| all_text_found.append(text) |
|
|
| return all_text_found |
|
|