feat: demo
Browse files- README.md +1 -1
- app.py +92 -0
- data/00.jpg +0 -0
- data/01.jpg +0 -0
- data/02.jpg +0 -0
- data/03.jpg +0 -0
- data/04.jpg +0 -0
- data/06.jpg +0 -0
- data/07.jpg +0 -0
- data/08.jpg +0 -0
- requirements.txt +3 -0
- tinyLPR.tflite +3 -0
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
title: Multi-line License Plate Recognition
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
|
|
|
| 1 |
---
|
| 2 |
title: Multi-line License Plate Recognition
|
| 3 |
+
emoji: 🚘
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, glob
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from ai_edge_litert.interpreter import Interpreter
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def get_samples():
|
| 11 |
+
list_ = glob.glob(os.path.join(os.path.dirname(__file__), 'data/*.jpg'))
|
| 12 |
+
list_.sort(key=lambda x: int(Path(x).stem))
|
| 13 |
+
return list_
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def cv2_imread(path):
|
| 17 |
+
return cv2.imdecode(np.fromfile(path, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def load_dict(dict_path='label.names'):
|
| 21 |
+
dict_path = os.path.join(os.path.dirname(__file__), dict_path)
|
| 22 |
+
with open(dict_path, 'r', encoding='utf-8') as f:
|
| 23 |
+
_dict = f.read().splitlines()
|
| 24 |
+
_dict = {i: _dict[i] for i in range(len(_dict))}
|
| 25 |
+
return _dict
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class TFliteDemo:
|
| 29 |
+
def __init__(self, model_path, blank=0):
|
| 30 |
+
self.blank = blank
|
| 31 |
+
self.interpreter = Interpreter(model_path=model_path, num_threads=os.cpu_count())
|
| 32 |
+
self.interpreter.allocate_tensors()
|
| 33 |
+
self.inputs = self.interpreter.get_input_details()
|
| 34 |
+
self.outputs = self.interpreter.get_output_details()
|
| 35 |
+
|
| 36 |
+
def inference(self, x):
|
| 37 |
+
self.interpreter.set_tensor(self.inputs[0]['index'], x)
|
| 38 |
+
self.interpreter.invoke()
|
| 39 |
+
return self.interpreter.get_tensor(self.outputs[0]['index'])
|
| 40 |
+
|
| 41 |
+
def preprocess(self, img):
|
| 42 |
+
if isinstance(img, str):
|
| 43 |
+
image = cv2_imread(img)
|
| 44 |
+
else:
|
| 45 |
+
if img is None:
|
| 46 |
+
raise ValueError('img is None')
|
| 47 |
+
image = img.copy()
|
| 48 |
+
|
| 49 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 50 |
+
image = cv2.resize(image, (96, 32), interpolation=cv2.INTER_LINEAR)
|
| 51 |
+
image = np.reshape(image, (1, 1, *image.shape)).astype(np.float32) / 255.0
|
| 52 |
+
return image
|
| 53 |
+
|
| 54 |
+
def postprocess(self, pred):
|
| 55 |
+
label_dict = load_dict()
|
| 56 |
+
pred_probs = pred[0]
|
| 57 |
+
pred_indices = np.argmax(pred_probs, axis=-1)
|
| 58 |
+
pred_label = [label_dict[i] for i in pred_indices]
|
| 59 |
+
label = ''.join(pred_label)
|
| 60 |
+
conf = np.min(np.max(pred_probs, axis=-1))
|
| 61 |
+
conf = float(f'{conf:.4f}')
|
| 62 |
+
return label, conf
|
| 63 |
+
|
| 64 |
+
def get_results(self, img):
|
| 65 |
+
img = self.preprocess(img)
|
| 66 |
+
pred = self.inference(img)
|
| 67 |
+
return self.postprocess(pred)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
if __name__ == '__main__':
|
| 71 |
+
_TITLE = '''Lightweight South Korean Multi-line License Plate Recognition'''
|
| 72 |
+
_DESCRIPTION = '''
|
| 73 |
+
<div>
|
| 74 |
+
<p style="text-align: center; font-size: 1.3em">This is a demo of Lightweight South Korean Multi-line License Plate Recognition.
|
| 75 |
+
<a style="display:inline-block; margin-left: .5em" href='https://github.com/noahzhy/SALPR'><img src='https://img.shields.io/github/stars/noahzhy/SALPR?style=social' /></a>
|
| 76 |
+
</p>
|
| 77 |
+
</div>
|
| 78 |
+
'''
|
| 79 |
+
# init model
|
| 80 |
+
demo = TFliteDemo(os.path.join(os.path.dirname(__file__), 'tinyLPR.tflite'))
|
| 81 |
+
app = gr.Interface(
|
| 82 |
+
fn=demo.get_results,
|
| 83 |
+
inputs="image",
|
| 84 |
+
outputs=[
|
| 85 |
+
gr.Textbox(label="Plate Number", type="text"),
|
| 86 |
+
gr.Textbox(label="Confidence", type="text"),
|
| 87 |
+
],
|
| 88 |
+
title=_TITLE,
|
| 89 |
+
description=_DESCRIPTION,
|
| 90 |
+
examples=get_samples(),
|
| 91 |
+
)
|
| 92 |
+
app.launch()
|
data/00.jpg
ADDED
|
data/01.jpg
ADDED
|
data/02.jpg
ADDED
|
data/03.jpg
ADDED
|
data/04.jpg
ADDED
|
data/06.jpg
ADDED
|
data/07.jpg
ADDED
|
data/08.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
opencv-python
|
| 3 |
+
ai-edge-litert
|
tinyLPR.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cf5f2dc5e2032347830ab49057ed3b10475037ca8b7ac96a9d13ac72b50f33ae
|
| 3 |
+
size 543036
|