ivoryxs commited on
Commit
f7276e3
·
verified ·
1 Parent(s): f426c52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -26
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 torch
5
 
6
- # Carregar modelo e tokenizer
7
- model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
8
- feature_extractor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
9
- tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
10
 
11
- device = "cuda" if torch.cuda.is_available() else "cpu"
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
- # Pré-processar a imagem
20
- pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values
21
- pixel_values = pixel_values.to(device)
22
 
23
- # Gerar legenda
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=generate_caption,
32
- inputs=gr.Image(type="pil", label="Envie uma imagem"),
33
- outputs=gr.Textbox(label="Legenda gerada"),
34
- title="Image-to-Text com Hugging Face",
35
- description="Este app usa o modelo ViT-GPT2 para gerar descrições de imagens. Ideal para produtos, cenas e mais!"
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()