ivoryxs commited on
Commit
1288bb2
·
verified ·
1 Parent(s): e77b421

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -53
app.py CHANGED
@@ -1,77 +1,41 @@
1
  import gradio as gr
2
- from paddleocr import PaddleOCR, draw_ocr
3
  from PIL import Image
4
  import numpy as np
5
 
6
- # Inicializa o PaddleOCR com parâmetros compatíveis
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 com texto."
18
 
19
- # Converter PIL Image para np.array
20
  img = np.array(image.convert("RGB"))
21
 
22
- # Executa OCR
23
- result = ocr.ocr(img, det=True, rec=True, cls=True)
24
 
25
- # Extrair somente o texto
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
- texto_detectado = caixa[1][0]
32
- textos.append(texto_detectado)
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
- def ocr_with_boxes(image):
41
- """Função que mostra também caixas de texto desenhadas (opcional)"""
42
- if image is None:
43
- return image, "Por favor, envie uma imagem."
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 Atualizado",
73
- description="Extrai texto de imagens. Se quiser, ativa caixas de texto."
74
  )
75
 
76
  if __name__ == "__main__":
77
- iface.launch()
 
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()