artificialguybr commited on
Commit
48bbaa9
·
verified ·
1 Parent(s): c8dea56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -6
app.py CHANGED
@@ -12,12 +12,17 @@ from surya.postprocessing.heatmap import draw_polys_on_image
12
  det_model, det_processor = load_det_model(), load_det_processor()
13
  rec_model, rec_processor = load_rec_model(), load_rec_processor()
14
 
15
- # Assuming languages.json maps language codes to names, but we'll use codes directly for dropdown
16
  with open("languages.json", "r") as file:
17
  languages = json.load(file)
18
- language_options = list(languages.keys()) # Use codes directly
19
 
20
- def ocr_function(img, lang_code):
 
 
 
 
 
21
  predictions = run_ocr([img], [lang_code], det_model, det_processor, rec_model, rec_processor)
22
  # Assuming predictions is a list of dictionaries, one per image
23
  if predictions:
@@ -27,7 +32,7 @@ def ocr_function(img, lang_code):
27
  return img, {"error": "No text detected"}
28
 
29
  def text_line_detection_function(img):
30
- preds = batch_inference([img], det_model, det_processor)[0]
31
  img_with_lines = draw_polys_on_image(preds["polygons"], img)
32
  return img_with_lines, preds
33
 
@@ -36,12 +41,13 @@ with gr.Blocks() as app:
36
  with gr.Tab("OCR"):
37
  with gr.Column():
38
  ocr_input_image = gr.Image(label="Input Image for OCR", type="pil")
39
- ocr_language_selector = gr.Dropdown(label="Select Language for OCR", choices=language_options, value="en")
40
  ocr_run_button = gr.Button("Run OCR")
41
  with gr.Column():
42
  ocr_output_image = gr.Image(label="OCR Output Image", type="pil", interactive=False)
43
  ocr_text_output = gr.TextArea(label="Recognized Text")
44
 
 
45
  ocr_run_button.click(fn=ocr_function, inputs=[ocr_input_image, ocr_language_selector.value], outputs=[ocr_output_image, ocr_text_output])
46
 
47
 
@@ -56,4 +62,4 @@ with gr.Blocks() as app:
56
  detection_run_button.click(fn=text_line_detection_function, inputs=detection_input_image, outputs=[detection_output_image, detection_json_output])
57
 
58
  if __name__ == "__main__":
59
- app.launch()
 
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
+ # Get the language code from the dictionary
25
+ lang_code = language_dict[lang_name]
26
  predictions = run_ocr([img], [lang_code], det_model, det_processor, rec_model, rec_processor)
27
  # Assuming predictions is a list of dictionaries, one per image
28
  if predictions:
 
32
  return img, {"error": "No text detected"}
33
 
34
  def text_line_detection_function(img):
35
+ preds = batch_detection([img], det_model, det_processor)[0]
36
  img_with_lines = draw_polys_on_image(preds["polygons"], img)
37
  return img_with_lines, preds
38
 
 
41
  with gr.Tab("OCR"):
42
  with gr.Column():
43
  ocr_input_image = gr.Image(label="Input Image for OCR", type="pil")
44
+ ocr_language_selector = gr.Dropdown(label="Select Language for OCR", choices=language_options, value="English")
45
  ocr_run_button = gr.Button("Run OCR")
46
  with gr.Column():
47
  ocr_output_image = gr.Image(label="OCR Output Image", type="pil", interactive=False)
48
  ocr_text_output = gr.TextArea(label="Recognized Text")
49
 
50
+ # Pass the input image and the language name to the ocr_function
51
  ocr_run_button.click(fn=ocr_function, inputs=[ocr_input_image, ocr_language_selector.value], outputs=[ocr_output_image, ocr_text_output])
52
 
53
 
 
62
  detection_run_button.click(fn=text_line_detection_function, inputs=detection_input_image, outputs=[detection_output_image, detection_json_output])
63
 
64
  if __name__ == "__main__":
65
+ app.launch()