Spaces:
Sleeping
Sleeping
Create modules/vision.py
Browse files- modules/vision.py +23 -0
modules/vision.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import easyocr
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Inicializa o leitor apenas uma vez (é pesado)
|
| 6 |
+
reader = easyocr.Reader(['en'], gpu=False) # GPU=False para garantir compatibilidade no HF CPU
|
| 7 |
+
|
| 8 |
+
def analyze_image_text(uploaded_image):
|
| 9 |
+
"""Lê texto de IHMs ou placas de identificação."""
|
| 10 |
+
try:
|
| 11 |
+
# Converte o arquivo do streamlit para formato OpenCV
|
| 12 |
+
file_bytes = np.asarray(bytearray(uploaded_image.read()), dtype=np.uint8)
|
| 13 |
+
img = cv2.imdecode(file_bytes, 1)
|
| 14 |
+
|
| 15 |
+
# Realiza o OCR
|
| 16 |
+
results = reader.readtext(img, detail=0)
|
| 17 |
+
|
| 18 |
+
if not results:
|
| 19 |
+
return "Nenhum texto legível detectado."
|
| 20 |
+
|
| 21 |
+
return "\n".join(results)
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"Erro no processamento óptico: {e}"
|