Spaces:
Build error
Build error
add initial test
Browse files- app.py +64 -3
- detection.tflite +3 -0
- recognition.tflite +3 -0
app.py
CHANGED
|
@@ -1,8 +1,69 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
def greet(name):
|
| 5 |
-
return "Hello " + name + "!!"
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import re, datetime,time, cv2, numpy as np, tensorflow as tf, sys
|
| 3 |
|
| 4 |
+
interpreter = tf.lite.Interpreter(model_path='detection.tflite')
|
| 5 |
+
interpreter.allocate_tensors()
|
| 6 |
+
recog_interpreter = tf.lite.Interpreter(model_path='recognition.tflite')
|
| 7 |
+
recog_interpreter.allocate_tensors()
|
| 8 |
+
input_details = interpreter.get_input_details()
|
| 9 |
+
output_details = interpreter.get_output_details()
|
| 10 |
+
recog_input_details = recog_interpreter.get_input_details()
|
| 11 |
+
recog_output_details = recog_interpreter.get_output_details()
|
| 12 |
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
|
| 15 |
+
def execute_text_recognition_tflite( boxes, frame, interpreter, input_details, output_details):
|
| 16 |
+
x1, x2, y1, y2 = boxes[1], boxes[3], boxes[0], boxes[2]
|
| 17 |
+
save_frame = frame[
|
| 18 |
+
max( 0, int(y1*1079) ) : min( 1079, int(y2*1079) ),
|
| 19 |
+
max( 0, int(x1*1920) ) : min( 1920, int(x2*1920) )
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
# Execute text recognition
|
| 23 |
+
|
| 24 |
+
test_image = cv2.resize(save_frame,(94,24))/256
|
| 25 |
+
test_image = np.expand_dims(test_image,axis=0)
|
| 26 |
+
test_image = test_image.astype(np.float32)
|
| 27 |
+
interpreter.set_tensor(input_details[0]['index'], test_image)
|
| 28 |
+
interpreter.invoke()
|
| 29 |
+
output_data = interpreter.get_tensor(output_details[0]['index'])
|
| 30 |
+
decoded = tf.keras.backend.ctc_decode(output_data,(24,),greedy=False)
|
| 31 |
+
text = ""
|
| 32 |
+
for i in np.array(decoded[0][0][0]):
|
| 33 |
+
if i >-1:
|
| 34 |
+
text += DECODE_DICT[i]
|
| 35 |
+
# Do nothing if text is empty
|
| 36 |
+
if not len(text): return
|
| 37 |
+
license_plate = text
|
| 38 |
+
text[:3].replace("0",'O')
|
| 39 |
+
|
| 40 |
+
return text
|
| 41 |
+
|
| 42 |
+
def greet(image):
|
| 43 |
+
resized = cv2.resize(image, (320,320), interpolation=cv2.INTER_AREA)
|
| 44 |
+
demo_frame = cv2.resize(image, (680,480), interpolation=cv2.INTER_AREA)
|
| 45 |
+
input_data = resized.astype(np.float32) # Set as 3D RGB float array
|
| 46 |
+
input_data /= 255. # Normalize
|
| 47 |
+
input_data = np.expand_dims(input_data, axis=0) # Batch dimension (wrap in 4D)
|
| 48 |
+
|
| 49 |
+
# Initialize input tensor
|
| 50 |
+
interpreter.set_tensor(input_details[0]['index'], input_data)
|
| 51 |
+
interpreter.invoke()
|
| 52 |
+
output_data = interpreter.get_tensor(output_details[0]['index'])
|
| 53 |
+
|
| 54 |
+
# Bounding boxes
|
| 55 |
+
boxes = interpreter.get_tensor(output_details[1]['index'])
|
| 56 |
+
|
| 57 |
+
text = None
|
| 58 |
+
# For index and confidence value of the first class [0]
|
| 59 |
+
for i, confidence in enumerate(output_data[0]):
|
| 60 |
+
if confidence > .3:
|
| 61 |
+
text = execute_text_recognition_tflite(
|
| 62 |
+
boxes[0][i], image,
|
| 63 |
+
recog_interpreter, recog_input_details, recog_output_details,
|
| 64 |
+
)
|
| 65 |
+
return text
|
| 66 |
+
image = gr.inputs.Image(shape=(320,320))
|
| 67 |
+
|
| 68 |
+
iface = gr.Interface(fn=greet, inputs=image, outputs="text")
|
| 69 |
iface.launch()
|
detection.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9a985cc86131fac5be60478f4c10be416dfe035445b70813d6441ced7d330018
|
| 3 |
+
size 11495036
|
recognition.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b080d01c1c84eaa207c8ca5834070bd76ce8d62fe6a4dce7c31d238462a07796
|
| 3 |
+
size 820132
|