Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,41 @@
|
|
| 1 |
-
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
feature_extractor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 9 |
-
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
model.to(device)
|
| 13 |
-
|
| 14 |
-
# Função para gerar legenda
|
| 15 |
-
def generate_caption(image):
|
| 16 |
if image is None:
|
| 17 |
-
return "Por favor, envie uma imagem."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
pixel_values = pixel_values.to(device)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
output_ids = model.generate(pixel_values, max_length=32, num_beams=4)
|
| 25 |
-
caption = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 26 |
-
|
| 27 |
-
return caption
|
| 28 |
|
| 29 |
# Interface Gradio
|
| 30 |
interface = gr.Interface(
|
| 31 |
-
fn=
|
| 32 |
-
inputs=gr.Image(type="pil", label="Envie uma imagem"),
|
| 33 |
-
outputs=gr.Textbox(label="
|
| 34 |
-
title="
|
| 35 |
-
description="
|
| 36 |
)
|
| 37 |
|
| 38 |
-
# Rodar
|
| 39 |
if __name__ == "__main__":
|
| 40 |
interface.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from paddleocr import PaddleOCR
|
| 3 |
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
+
# Inicializar PaddleOCR com suporte a português
|
| 7 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='pt', show_log=False)
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
def ocr_paddle(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
if image is None:
|
| 11 |
+
return "Por favor, envie uma imagem com texto."
|
| 12 |
+
|
| 13 |
+
# Converter PIL para array numpy (formato esperado pelo PaddleOCR)
|
| 14 |
+
img = np.array(image)
|
| 15 |
+
|
| 16 |
+
# Realizar OCR
|
| 17 |
+
result = ocr.ocr(img, cls=True)
|
| 18 |
+
|
| 19 |
+
# Extrair textos detectados
|
| 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 foi detectado na imagem."
|
|
|
|
| 28 |
|
| 29 |
+
return "\n".join(textos)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Interface Gradio
|
| 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 estilo Google Lens 🧠",
|
| 37 |
+
description="Extrai texto de imagens, como rótulos, embalagens, documentos e placas. Powered by PaddleOCR."
|
| 38 |
)
|
| 39 |
|
|
|
|
| 40 |
if __name__ == "__main__":
|
| 41 |
interface.launch()
|