artificialguybr commited on
Commit
b3bb9ad
·
verified ·
1 Parent(s): 8ef414c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -34
app.py CHANGED
@@ -8,53 +8,48 @@ from surya.model.recognition.model import load_model as load_rec_model
8
  from surya.model.recognition.processor import load_processor as load_rec_processor
9
  from surya.postprocessing.heatmap import draw_polys_on_image
10
 
11
- # Load models and processors
12
  det_model, det_processor = load_det_model(), load_det_processor()
13
  rec_model, rec_processor = load_rec_model(), load_rec_processor()
14
 
15
- # Load languages from JSON
16
  with open("languages.json", "r") as file:
17
  languages = json.load(file)
18
- language_options = [(code, language) for code, language in languages.items()]
19
-
20
- def ocr_function(img, langs):
21
- # Debugging: Print the languages input
22
- print("Languages input:", langs)
23
- lang_list = langs.split(',')
24
- print("Language list:", lang_list)
25
-
26
- # Ensure we're passing a list of images and a list of languages with matching lengths
27
- images = [img] # Wrap the single image in a list
28
- assert len(images) == len(lang_list), "Mismatch in the number of images and languages"
29
-
30
- predictions = run_ocr(images, lang_list, det_model, det_processor, rec_model, rec_processor)[0]
31
- img_with_text = draw_polys_on_image(predictions["polys"], img)
32
- return img_with_text, predictions
33
 
 
 
 
 
 
34
 
35
  def text_line_detection_function(img):
36
- preds = batch_detection([img], det_model, det_processor)[0]
37
  img_with_lines = draw_polys_on_image(preds["polygons"], img)
38
  return img_with_lines, preds
39
 
40
  with gr.Blocks() as app:
41
- gr.Markdown("# Surya OCR and Text Line Detection Demo")
42
  with gr.Tab("OCR"):
43
- with gr.Row():
44
- ocr_input_image = gr.Image(label="Input Image for OCR", type="pil")
45
- ocr_language_selector = gr.Dropdown(label="Select Language(s) for OCR", choices=language_options, value="en")
46
- ocr_output_image = gr.Image(label="OCR Output Image", type="pil", interactive=False)
47
- ocr_json_output = gr.JSON(label="OCR JSON Output")
48
- ocr_button = gr.Button("Run OCR")
49
- ocr_button.click(fn=ocr_function, inputs=[ocr_input_image, ocr_language_selector], outputs=[ocr_output_image, ocr_json_output])
50
-
51
- with gr.Tab("Text Line Detection"):
52
- with gr.Row():
53
- detection_input_image = gr.Image(label="Input Image for Detection", type="pil")
54
- detection_output_image = gr.Image(label="Detection Output Image", type="pil", interactive=False)
55
- detection_json_output = gr.JSON(label="Detection JSON Output")
56
- detection_button = gr.Button("Run Text Line Detection")
57
- detection_button.click(fn=text_line_detection_function, inputs=detection_input_image, outputs=[detection_output_image, detection_json_output])
 
 
 
 
58
 
59
  if __name__ == "__main__":
60
  app.launch()
 
8
  from surya.model.recognition.processor import load_processor as load_rec_processor
9
  from surya.postprocessing.heatmap import draw_polys_on_image
10
 
11
+ # Carregar modelos e processadores
12
  det_model, det_processor = load_det_model(), load_det_processor()
13
  rec_model, rec_processor = load_rec_model(), load_rec_processor()
14
 
15
+ # Carregar opções de idioma
16
  with open("languages.json", "r") as file:
17
  languages = json.load(file)
18
+ language_options = [(language, code) for code, language in languages.items()]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ def ocr_function(img, lang_code):
21
+ # Ajuste aqui para garantir que lang_code é uma lista
22
+ predictions = run_ocr([img], [lang_code], det_model, det_processor, rec_model, rec_processor)[0]
23
+ img_with_text = draw_polys_on_image(predictions["polys"], img)
24
+ return img_with_text, predictions["text"]
25
 
26
  def text_line_detection_function(img):
27
+ preds = batch_inference([img], det_model, det_processor)[0]
28
  img_with_lines = draw_polys_on_image(preds["polygons"], img)
29
  return img_with_lines, preds
30
 
31
  with gr.Blocks() as app:
32
+ gr.Markdown("# Surya OCR e Detecção de Linhas de Texto")
33
  with gr.Tab("OCR"):
34
+ with gr.Column():
35
+ ocr_input_image = gr.Image(label="Imagem de Entrada para OCR", type="pil")
36
+ ocr_language_selector = gr.Dropdown(label="Selecione o Idioma para OCR", choices=language_options, value="en")
37
+ ocr_run_button = gr.Button("Executar OCR")
38
+ with gr.Column():
39
+ ocr_output_image = gr.Image(label="Imagem de Saída do OCR", type="pil", interactive=False)
40
+ ocr_text_output = gr.TextArea(label="Texto Reconhecido")
41
+
42
+ ocr_run_button.click(fn=ocr_function, inputs=[ocr_input_image, ocr_language_selector], outputs=[ocr_output_image, ocr_text_output])
43
+
44
+ with gr.Tab("Detecção de Linhas de Texto"):
45
+ with gr.Column():
46
+ detection_input_image = gr.Image(label="Imagem de Entrada para Detecção", type="pil")
47
+ detection_run_button = gr.Button("Executar Detecção de Linhas de Texto")
48
+ with gr.Column():
49
+ detection_output_image = gr.Image(label="Imagem de Saída da Detecção", type="pil", interactive=False)
50
+ detection_json_output = gr.JSON(label="Saída JSON da Detecção")
51
+
52
+ detection_run_button.click(fn=text_line_detection_function, inputs=detection_input_image, outputs=[detection_output_image, detection_json_output])
53
 
54
  if __name__ == "__main__":
55
  app.launch()