Update extract_text.py
Browse files- extract_text.py +13 -9
extract_text.py
CHANGED
|
@@ -1,29 +1,33 @@
|
|
| 1 |
-
import cv2
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import easyocr
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
# Inicializar EasyOCR
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
reader = easyocr.Reader(["en"], gpu=(device == "cuda"), verbose=False)
|
| 9 |
|
| 10 |
-
def extract_text_from_image(
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
img =
|
| 14 |
-
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 15 |
|
| 16 |
-
# Resizing and blurring
|
| 17 |
scale_factor = 2
|
| 18 |
upscaled = cv2.resize(img, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)
|
| 19 |
blur_img = cv2.blur(upscaled, (5, 5))
|
| 20 |
|
|
|
|
| 21 |
all_text_found = []
|
| 22 |
text_ = reader.readtext(blur_img, detail=1, paragraph=False, text_threshold=0.3)
|
| 23 |
|
| 24 |
for t in text_:
|
| 25 |
bbox, text, score = t
|
| 26 |
-
if score > 0.1:
|
| 27 |
all_text_found.append(text)
|
| 28 |
|
| 29 |
-
return all_text_found
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
+
import cv2
|
| 3 |
import easyocr
|
| 4 |
import torch
|
| 5 |
|
|
|
|
| 6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
reader = easyocr.Reader(["en"], gpu=(device == "cuda"), verbose=False)
|
| 8 |
|
| 9 |
+
def extract_text_from_image(upload_file, gpu_available):
|
| 10 |
+
upload_file.file.seek(0)
|
| 11 |
+
file_bytes = np.frombuffer(upload_file.file.read(), np.uint8)
|
| 12 |
+
|
| 13 |
+
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 14 |
+
|
| 15 |
+
if img is None:
|
| 16 |
+
raise ValueError("Não foi possível decodificar a imagem.")
|
| 17 |
|
| 18 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
|
|
|
| 19 |
|
|
|
|
| 20 |
scale_factor = 2
|
| 21 |
upscaled = cv2.resize(img, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)
|
| 22 |
blur_img = cv2.blur(upscaled, (5, 5))
|
| 23 |
|
| 24 |
+
reader = easyocr.Reader(['en'], gpu=gpu_available, verbose=False)
|
| 25 |
all_text_found = []
|
| 26 |
text_ = reader.readtext(blur_img, detail=1, paragraph=False, text_threshold=0.3)
|
| 27 |
|
| 28 |
for t in text_:
|
| 29 |
bbox, text, score = t
|
| 30 |
+
if score > 0.1:
|
| 31 |
all_text_found.append(text)
|
| 32 |
|
| 33 |
+
return all_text_found
|