Update extract_text.py
Browse files- extract_text.py +17 -11
extract_text.py
CHANGED
@@ -2,21 +2,27 @@ 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(
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
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))
|
@@ -30,4 +36,4 @@ def extract_text_from_image(upload_file, gpu_available):
|
|
30 |
if score > 0.1:
|
31 |
all_text_found.append(text)
|
32 |
|
33 |
-
return all_text_found
|
|
|
2 |
import cv2
|
3 |
import easyocr
|
4 |
import torch
|
5 |
+
from starlette.datastructures import UploadFile
|
6 |
|
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(image_input, gpu_available):
|
11 |
+
# Se for UploadFile, lê bytes
|
12 |
+
if isinstance(image_input, UploadFile):
|
13 |
+
image_input.file.seek(0)
|
14 |
+
file_bytes = np.frombuffer(image_input.file.read(), np.uint8)
|
15 |
+
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
16 |
+
if img is None:
|
17 |
+
raise ValueError("Não foi possível decodificar a imagem.")
|
18 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
19 |
+
# Se já for ndarray
|
20 |
+
elif isinstance(image_input, np.ndarray):
|
21 |
+
img = image_input
|
22 |
+
else:
|
23 |
+
raise ValueError("Formato de imagem não suportado.")
|
24 |
|
25 |
+
# Resto do processamento
|
26 |
scale_factor = 2
|
27 |
upscaled = cv2.resize(img, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)
|
28 |
blur_img = cv2.blur(upscaled, (5, 5))
|
|
|
36 |
if score > 0.1:
|
37 |
all_text_found.append(text)
|
38 |
|
39 |
+
return all_text_found
|