Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,41 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from paddleocr import PaddleOCR
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
# Inicializa o
|
| 7 |
-
ocr = PaddleOCR(
|
| 8 |
-
lang='pt', # português
|
| 9 |
-
use_textline_orientation=True, # parâmetro atual para orientação de linhas de texto
|
| 10 |
-
det=True, # ativar detecção de texto
|
| 11 |
-
rec=True, # ativar reconhecimento
|
| 12 |
-
cls=True # ativar classificação de orientação de texto
|
| 13 |
-
)
|
| 14 |
|
| 15 |
def ocr_paddle(image):
|
| 16 |
if image is None:
|
| 17 |
-
return "Por favor, envie uma imagem
|
| 18 |
|
| 19 |
-
# Converter
|
| 20 |
img = np.array(image.convert("RGB"))
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
result = ocr.ocr(img
|
| 24 |
|
| 25 |
-
# Extrair
|
| 26 |
textos = []
|
| 27 |
-
# result é uma lista de linhas, cada linha pode conter múltiplas caixas
|
| 28 |
for linha in result:
|
| 29 |
-
# linha é uma lista de caixas: cada caixa é [bounding_box, (texto, confidence)]
|
| 30 |
for caixa in linha:
|
| 31 |
-
|
| 32 |
-
textos.append(
|
| 33 |
|
| 34 |
if not textos:
|
| 35 |
return "Nenhum texto detectado."
|
| 36 |
|
| 37 |
-
# Juntamos os textos com nova linha
|
| 38 |
return "\n".join(textos)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
img = np.array(image.convert("RGB"))
|
| 46 |
-
result = ocr.ocr(img, det=True, rec=True, cls=True)
|
| 47 |
-
|
| 48 |
-
boxes = []
|
| 49 |
-
txts = []
|
| 50 |
-
scores = []
|
| 51 |
-
|
| 52 |
-
for linha in result:
|
| 53 |
-
for caixa in linha:
|
| 54 |
-
boxes.append(caixa[0])
|
| 55 |
-
txts.append(caixa[1][0])
|
| 56 |
-
scores.append(caixa[1][1])
|
| 57 |
-
|
| 58 |
-
# desenhar caixas e texto na imagem
|
| 59 |
-
image_with_boxes = draw_ocr(
|
| 60 |
-
image.convert("RGB"),
|
| 61 |
-
boxes, txts, scores,
|
| 62 |
-
font_path=None # ou um caminho para fonte, se quiser estilizar
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
return image_with_boxes, "\n".join(txts)
|
| 66 |
-
|
| 67 |
-
iface = gr.Interface(
|
| 68 |
-
fn=ocr_paddle, # se quiser só texto
|
| 69 |
-
# fn=ocr_with_boxes, # se quiser imagem + texto extraído
|
| 70 |
-
inputs=gr.Image(type="pil", label="Envie a imagem com texto"),
|
| 71 |
outputs=gr.Textbox(label="Texto extraído"),
|
| 72 |
-
title="OCR com PaddleOCR
|
| 73 |
-
description="Extrai texto de imagens
|
| 74 |
)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from paddleocr import PaddleOCR
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
+
# Inicializa o OCR (versão estável)
|
| 7 |
+
ocr = PaddleOCR(lang='pt') # você pode trocar para 'en', 'ch', etc.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def ocr_paddle(image):
|
| 10 |
if image is None:
|
| 11 |
+
return "Por favor, envie uma imagem."
|
| 12 |
|
| 13 |
+
# Converter imagem para array
|
| 14 |
img = np.array(image.convert("RGB"))
|
| 15 |
|
| 16 |
+
# Rodar OCR
|
| 17 |
+
result = ocr.ocr(img)
|
| 18 |
|
| 19 |
+
# Extrair textos
|
| 20 |
textos = []
|
|
|
|
| 21 |
for linha in result:
|
|
|
|
| 22 |
for caixa in linha:
|
| 23 |
+
texto = caixa[1][0]
|
| 24 |
+
textos.append(texto)
|
| 25 |
|
| 26 |
if not textos:
|
| 27 |
return "Nenhum texto detectado."
|
| 28 |
|
|
|
|
| 29 |
return "\n".join(textos)
|
| 30 |
|
| 31 |
+
# Interface
|
| 32 |
+
interface = gr.Interface(
|
| 33 |
+
fn=ocr_paddle,
|
| 34 |
+
inputs=gr.Image(type="pil", label="Envie uma imagem com texto"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
outputs=gr.Textbox(label="Texto extraído"),
|
| 36 |
+
title="OCR com PaddleOCR",
|
| 37 |
+
description="Extrai texto visível de imagens como rótulos, embalagens e documentos."
|
| 38 |
)
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
+
interface.launch()
|