artificialguybr commited on
Commit
6870916
·
verified ·
1 Parent(s): 557d222

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -13
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import json
3
  from PIL import Image
 
4
  from surya.ocr import run_ocr
5
  from surya.detection import batch_detection
6
  from surya.model.detection.segformer import load_model as load_det_model, load_processor as load_det_processor
@@ -8,34 +9,35 @@ 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
- # Create a dictionary to map language names to codes
 
16
  with open("languages.json", "r") as file:
17
  languages = json.load(file)
18
  language_dict = {name: code for name, code in languages.items()}
19
-
20
- # Use the language names for the dropdown choices
21
- language_options = list(language_dict.keys())
22
 
23
  def ocr_function(img, lang_name):
24
- print(f"OCR Function Called with lang_name: {lang_name}") # Debug print
25
- # Get the language code from the dictionary
26
  lang_code = language_dict[lang_name]
27
- print(f"Language Code: {lang_code}") # Debug print
28
  predictions = run_ocr([img], [lang_code], det_model, det_processor, rec_model, rec_processor)
29
- print(f"Predictions: {predictions}") # Debug print
30
  if predictions:
31
  img_with_text = draw_polys_on_image(predictions[0]["polys"], img)
32
  return img_with_text, predictions[0]["text"]
33
  else:
34
  return img, "No text detected"
35
 
36
-
37
  def text_line_detection_function(img):
 
38
  preds = batch_detection([img], det_model, det_processor)[0]
 
39
  img_with_lines = draw_polys_on_image(preds["polygons"], img)
40
  return img_with_lines, preds
41
 
@@ -44,13 +46,12 @@ with gr.Blocks() as app:
44
  with gr.Tab("OCR"):
45
  with gr.Column():
46
  ocr_input_image = gr.Image(label="Input Image for OCR", type="pil")
47
- ocr_language_selector = gr.Dropdown(label="Select Language for OCR", choices=language_options, value="English")
48
  ocr_run_button = gr.Button("Run OCR")
49
  with gr.Column():
50
  ocr_output_image = gr.Image(label="OCR Output Image", type="pil", interactive=False)
51
  ocr_text_output = gr.TextArea(label="Recognized Text")
52
 
53
- # Pass the input image and the language name to the ocr_function
54
  ocr_run_button.click(fn=ocr_function, inputs=[ocr_input_image, ocr_language_selector], outputs=[ocr_output_image, ocr_text_output])
55
 
56
  with gr.Tab("Text Line Detection"):
@@ -61,7 +62,6 @@ with gr.Blocks() as app:
61
  detection_output_image = gr.Image(label="Detection Output Image", type="pil", interactive=False)
62
  detection_json_output = gr.JSON(label="Detection JSON Output")
63
 
64
- # Pass the input image to the text_line_detection_function
65
  detection_run_button.click(fn=text_line_detection_function, inputs=detection_input_image, outputs=[detection_output_image, detection_json_output])
66
 
67
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import json
3
  from PIL import Image
4
+ # Assuming these imports work as expected, but you might need to adjust based on your actual package structure
5
  from surya.ocr import run_ocr
6
  from surya.detection import batch_detection
7
  from surya.model.detection.segformer import load_model as load_det_model, load_processor as load_det_processor
 
9
  from surya.model.recognition.processor import load_processor as load_rec_processor
10
  from surya.postprocessing.heatmap import draw_polys_on_image
11
 
12
+ # Load models and processors with print statements to confirm loading
13
+ print("Loading models and processors...")
14
  det_model, det_processor = load_det_model(), load_det_processor()
15
  rec_model, rec_processor = load_rec_model(), load_rec_processor()
16
+ print("Models and processors loaded successfully.")
17
 
18
+ # Load language codes
19
+ print("Loading language codes...")
20
  with open("languages.json", "r") as file:
21
  languages = json.load(file)
22
  language_dict = {name: code for name, code in languages.items()}
23
+ print(f"Loaded languages: {list(language_dict.keys())}")
 
 
24
 
25
  def ocr_function(img, lang_name):
26
+ print(f"OCR Function Called with lang_name: {lang_name}")
 
27
  lang_code = language_dict[lang_name]
28
+ print(f"Language Code: {lang_code}")
29
  predictions = run_ocr([img], [lang_code], det_model, det_processor, rec_model, rec_processor)
30
+ print(f"Predictions: {predictions}")
31
  if predictions:
32
  img_with_text = draw_polys_on_image(predictions[0]["polys"], img)
33
  return img_with_text, predictions[0]["text"]
34
  else:
35
  return img, "No text detected"
36
 
 
37
  def text_line_detection_function(img):
38
+ print("Text Line Detection Function Called")
39
  preds = batch_detection([img], det_model, det_processor)[0]
40
+ print(f"Detection Predictions: {preds}")
41
  img_with_lines = draw_polys_on_image(preds["polygons"], img)
42
  return img_with_lines, preds
43
 
 
46
  with gr.Tab("OCR"):
47
  with gr.Column():
48
  ocr_input_image = gr.Image(label="Input Image for OCR", type="pil")
49
+ ocr_language_selector = gr.Dropdown(label="Select Language for OCR", choices=list(language_dict.keys()), value="English")
50
  ocr_run_button = gr.Button("Run OCR")
51
  with gr.Column():
52
  ocr_output_image = gr.Image(label="OCR Output Image", type="pil", interactive=False)
53
  ocr_text_output = gr.TextArea(label="Recognized Text")
54
 
 
55
  ocr_run_button.click(fn=ocr_function, inputs=[ocr_input_image, ocr_language_selector], outputs=[ocr_output_image, ocr_text_output])
56
 
57
  with gr.Tab("Text Line Detection"):
 
62
  detection_output_image = gr.Image(label="Detection Output Image", type="pil", interactive=False)
63
  detection_json_output = gr.JSON(label="Detection JSON Output")
64
 
 
65
  detection_run_button.click(fn=text_line_detection_function, inputs=detection_input_image, outputs=[detection_output_image, detection_json_output])
66
 
67
  if __name__ == "__main__":