SWHL commited on
Commit
19a047e
·
1 Parent(s): 7e03ca9
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -86,6 +86,8 @@ def visualize(image_path, boxes, txts, scores,
86
 
87
  def inference(img_path, box_thresh=0.5, unclip_ratio=1.6, text_score=0.5,
88
  text_det=None, text_rec=None):
 
 
89
  det_model_path = str(Path('models') / 'text_det' / text_det)
90
  rec_model_path = str(Path('models') / 'text_rec' / text_rec)
91
  if 'v2' in rec_model_path:
@@ -93,27 +95,35 @@ def inference(img_path, box_thresh=0.5, unclip_ratio=1.6, text_score=0.5,
93
  else:
94
  rec_image_shape = [3, 48, 320]
95
 
96
- print('Init Class')
97
  s = time.time()
98
  rapid_ocr = RapidOCR(det_model_path=det_model_path,
99
  rec_model_path=rec_model_path,
100
  rec_img_shape=rec_image_shape)
101
- print(det_model_path, rec_model_path, rec_image_shape)
102
  elapse = time.time() - s
103
- print(elapse)
 
 
 
 
104
 
105
  img = cv2.imread(img_path)
106
- ocr_result, _ = rapid_ocr(img, box_thresh=box_thresh,
107
- unclip_ratio=unclip_ratio,
108
- text_score=text_score)
 
 
 
 
 
109
  if not ocr_result:
110
- return img_path, '未识别到有效文本'
111
 
112
  dt_boxes, rec_res, scores = list(zip(*ocr_result))
113
  img_save_path = visualize(img_path, dt_boxes, rec_res, scores)
114
  output_text = [f'{one_rec} {float(score):.4f}'
115
  for one_rec, score in zip(rec_res, scores)]
116
- return img_save_path, output_text
117
 
118
 
119
  examples = [['images/1.jpg'], ['images/ch_en_num.jpg']]
@@ -163,14 +173,15 @@ with gr.Blocks(title='RapidOCR') as demo:
163
  with gr.Row():
164
  input_img = gr.Image(type='filepath', label='Input')
165
  out_img = gr.Image(type='filepath', label='Output')
 
166
  out_txt = gr.outputs.Textbox(type='text', label='RecText')
167
  button = gr.Button('Submit')
168
  button.click(fn=inference,
169
  inputs=[input_img, box_thresh, unclip_ratio, text_score,
170
  text_det, text_rec],
171
- outputs=[out_img, out_txt])
172
  gr.Examples(examples=examples,
173
  inputs=[input_img, box_thresh, unclip_ratio, text_score,
174
  text_det, text_rec],
175
- outputs=[out_img, out_txt], fn=inference)
176
  demo.launch(debug=True, enable_queue=True)
 
86
 
87
  def inference(img_path, box_thresh=0.5, unclip_ratio=1.6, text_score=0.5,
88
  text_det=None, text_rec=None):
89
+ out_log_list = []
90
+
91
  det_model_path = str(Path('models') / 'text_det' / text_det)
92
  rec_model_path = str(Path('models') / 'text_rec' / text_rec)
93
  if 'v2' in rec_model_path:
 
95
  else:
96
  rec_image_shape = [3, 48, 320]
97
 
98
+ out_log_list.append('Init Model')
99
  s = time.time()
100
  rapid_ocr = RapidOCR(det_model_path=det_model_path,
101
  rec_model_path=rec_model_path,
102
  rec_img_shape=rec_image_shape)
 
103
  elapse = time.time() - s
104
+
105
+ out_log_list.append(f'Init Model cost: {elapse:.5f}')
106
+ out_log_list.extend([f'det_model:{det_model_path}',
107
+ f'rec_model: {rec_model_path}',
108
+ f'rec_image_shape: {rec_image_shape}'])
109
 
110
  img = cv2.imread(img_path)
111
+ ocr_result, infer_elapse = rapid_ocr(img, box_thresh=box_thresh,
112
+ unclip_ratio=unclip_ratio,
113
+ text_score=text_score)
114
+ det_cost, cls_cost, rec_cost = infer_elapse
115
+ out_log_list.extend([f'det cost: {det_cost:.5f}',
116
+ f'cls cost: {cls_cost:.5f}',
117
+ f'rec cost: {rec_cost:.5f}'])
118
+ out_log = '\n'.join([str(v) for v in out_log_list])
119
  if not ocr_result:
120
+ return img_path, '未识别到有效文本', out_log
121
 
122
  dt_boxes, rec_res, scores = list(zip(*ocr_result))
123
  img_save_path = visualize(img_path, dt_boxes, rec_res, scores)
124
  output_text = [f'{one_rec} {float(score):.4f}'
125
  for one_rec, score in zip(rec_res, scores)]
126
+ return img_save_path, output_text, out_log
127
 
128
 
129
  examples = [['images/1.jpg'], ['images/ch_en_num.jpg']]
 
173
  with gr.Row():
174
  input_img = gr.Image(type='filepath', label='Input')
175
  out_img = gr.Image(type='filepath', label='Output')
176
+ out_log = gr.outputs.Textbox(type='text', label='Run Log')
177
  out_txt = gr.outputs.Textbox(type='text', label='RecText')
178
  button = gr.Button('Submit')
179
  button.click(fn=inference,
180
  inputs=[input_img, box_thresh, unclip_ratio, text_score,
181
  text_det, text_rec],
182
+ outputs=[out_img, out_txt, out_log])
183
  gr.Examples(examples=examples,
184
  inputs=[input_img, box_thresh, unclip_ratio, text_score,
185
  text_det, text_rec],
186
+ outputs=[out_img, out_txt, out_log], fn=inference)
187
  demo.launch(debug=True, enable_queue=True)