noahzhy commited on
Commit
cb30e70
1 Parent(s): 5bc5c8c

Add image check and confidence calculation in

Browse files
Files changed (1) hide show
  1. app.py +43 -18
app.py CHANGED
@@ -16,6 +16,9 @@ def get_sample_images():
16
  def inference(image):
17
  # load model
18
  demo = TFliteDemo(os.path.join(os.path.dirname(__file__), 'model.tflite'))
 
 
 
19
  # load image
20
  image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
21
  image = center_fit(image, 128, 64, top_left=True)
@@ -25,7 +28,9 @@ def inference(image):
25
  # decode
26
  dict = load_dict(os.path.join(os.path.dirname(__file__), 'label.names'))
27
  res = decode_label(pred, dict)
28
- return res
 
 
29
 
30
 
31
  class TFliteDemo:
@@ -79,6 +84,21 @@ def load_dict(dict_path='label.names'):
79
  return dict
80
 
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  def decode_label(mat, chars) -> str:
83
  # mat is the output of model
84
  # get char indices along best path
@@ -90,20 +110,25 @@ def decode_label(mat, chars) -> str:
90
  res = res.replace(' ', '').replace('_', '')
91
  return res
92
 
93
- _TITLE = '''South Korean License Plate Recognition'''
94
- _DESCRIPTION = '''
95
- <div>
96
- <p style="text-align: center; font-size: 1.3em">This is a demo of South Korean License Plate Recognition.
97
- <a style="display:inline-block; margin-left: .5em" href='https://github.com/noahzhy/KR_LPR_TF/'><img src='https://img.shields.io/github/stars/noahzhy/KR_LPR_TF?style=social' /></a>
98
- </p>
99
- </div>
100
- '''
101
- interface = gr.Interface(
102
- fn=inference,
103
- inputs="image",
104
- outputs="text",
105
- title=_TITLE,
106
- description=_DESCRIPTION,
107
- examples=get_sample_images(),
108
- )
109
- interface.launch()
 
 
 
 
 
 
16
  def inference(image):
17
  # load model
18
  demo = TFliteDemo(os.path.join(os.path.dirname(__file__), 'model.tflite'))
19
+ # check image is not None
20
+ if image is None:
21
+ return 'None', 'None'
22
  # load image
23
  image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
24
  image = center_fit(image, 128, 64, top_left=True)
 
28
  # decode
29
  dict = load_dict(os.path.join(os.path.dirname(__file__), 'label.names'))
30
  res = decode_label(pred, dict)
31
+ # get confidence
32
+ confidence = get_confidence(pred)
33
+ return res, confidence
34
 
35
 
36
  class TFliteDemo:
 
84
  return dict
85
 
86
 
87
+ def get_confidence(mat) -> float:
88
+ # mat is the output of model
89
+ # get char indices along best path
90
+ best_path_indices = np.argmax(mat[0], axis=-1)
91
+ confidence = np.max(mat[0], axis=-1)
92
+ blank_idx = mat.shape[-1] - 1
93
+ avg_confidence = []
94
+ for idx, conf in zip(best_path_indices, confidence):
95
+ if idx != blank_idx:
96
+ avg_confidence.append(conf)
97
+ conf = np.mean(avg_confidence) / 255.0
98
+ # keep 4 decimal places
99
+ return "{:.4f}".format(conf)
100
+
101
+
102
  def decode_label(mat, chars) -> str:
103
  # mat is the output of model
104
  # get char indices along best path
 
110
  res = res.replace(' ', '').replace('_', '')
111
  return res
112
 
113
+
114
+ if __name__ == '__main__':
115
+ _TITLE = '''South Korean License Plate Recognition'''
116
+ _DESCRIPTION = '''
117
+ <div>
118
+ <p style="text-align: center; font-size: 1.3em">This is a demo of South Korean License Plate Recognition.
119
+ <a style="display:inline-block; margin-left: .5em" href='https://github.com/noahzhy/KR_LPR_TF/'><img src='https://img.shields.io/github/stars/noahzhy/KR_LPR_TF?style=social' /></a>
120
+ </p>
121
+ </div>
122
+ '''
123
+ interface = gr.Interface(
124
+ fn=inference,
125
+ inputs="image",
126
+ outputs=[
127
+ gr.Textbox(label="Plate Number", type="text"),
128
+ gr.Textbox(label="Confidence", type="text"),
129
+ ],
130
+ title=_TITLE,
131
+ description=_DESCRIPTION,
132
+ examples=get_sample_images(),
133
+ )
134
+ interface.launch()