Atualli leakyrelu commited on
Commit
69af24f
·
0 Parent(s):

Duplicate from leakyrelu/MobilenetV2SSDLite_LPRnet

Browse files

Co-authored-by: Carlos Sandoval <[email protected]>

Files changed (9) hide show
  1. .gitattributes +27 -0
  2. 3.jpg +0 -0
  3. 4.jpg +0 -0
  4. README.md +14 -0
  5. app.py +92 -0
  6. detection.tflite +3 -0
  7. recognition.tflite +3 -0
  8. recognition2.tflite +3 -0
  9. requirements.txt +4 -0
.gitattributes ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.onnx filter=lfs diff=lfs merge=lfs -text
13
+ *.ot filter=lfs diff=lfs merge=lfs -text
14
+ *.parquet filter=lfs diff=lfs merge=lfs -text
15
+ *.pb filter=lfs diff=lfs merge=lfs -text
16
+ *.pt filter=lfs diff=lfs merge=lfs -text
17
+ *.pth filter=lfs diff=lfs merge=lfs -text
18
+ *.rar filter=lfs diff=lfs merge=lfs -text
19
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
20
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
21
+ *.tflite filter=lfs diff=lfs merge=lfs -text
22
+ *.tgz filter=lfs diff=lfs merge=lfs -text
23
+ *.wasm filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
3.jpg ADDED
4.jpg ADDED
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Automatic Licence plate Recognition
3
+ emoji: 🦀
4
+ colorFrom: blue
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 2.9.4
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ duplicated_from: leakyrelu/MobilenetV2SSDLite_LPRnet
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import re, datetime,time, cv2, numpy as np, tensorflow as tf, sys
3
+
4
+
5
+ CHARS = "ABCDEFGHIJKLMNPQRSTUVWXYZ0123456789" # exclude I, O
6
+ CHARS_DICT = {char:i for i, char in enumerate(CHARS)}
7
+ DECODE_DICT = {i:char for i, char in enumerate(CHARS)}
8
+
9
+ interpreter = tf.lite.Interpreter(model_path='detection.tflite')
10
+ interpreter.allocate_tensors()
11
+ recog_interpreter = tf.lite.Interpreter(model_path='recognition2.tflite')
12
+ recog_input_details = recog_interpreter.get_input_details()
13
+ recog_output_details = recog_interpreter.get_output_details()
14
+ recog_interpreter.resize_tensor_input(recog_input_details[0]['index'], (1, 24, 94, 3))
15
+ recog_interpreter.allocate_tensors()
16
+ input_details = interpreter.get_input_details()
17
+ output_details = interpreter.get_output_details()
18
+
19
+
20
+
21
+ def execute_text_recognition_tflite( boxes, frame, interpreter, input_details, output_details):
22
+ x1, x2, y1, y2 = boxes[1], boxes[3], boxes[0], boxes[2]
23
+ save_frame = frame[
24
+ max( 0, int(y1*1079) ) : min( 1079, int(y2*1079) ),
25
+ max( 0, int(x1*1920) ) : min( 1920, int(x2*1920) )
26
+ ]
27
+
28
+ # Execute text recognition
29
+ print(frame.shape)
30
+ test_image = cv2.resize(save_frame,(94,24))/256
31
+ test_image = np.expand_dims(test_image,axis=0)
32
+ test_image = test_image.astype(np.float32)
33
+ interpreter.set_tensor(input_details[0]['index'], test_image)
34
+ interpreter.invoke()
35
+ output_data = interpreter.get_tensor(output_details[0]['index'])
36
+ decoded = tf.keras.backend.ctc_decode(output_data,(24,),greedy=False)
37
+ text = ""
38
+ for i in np.array(decoded[0][0][0]):
39
+ if i >-1:
40
+ text += DECODE_DICT[i]
41
+ # Do nothing if text is empty
42
+ if not len(text): return
43
+ license_plate = text
44
+ text[:3].replace("0",'O')
45
+
46
+ return text,cv2.resize(save_frame,(94,24))
47
+
48
+ def greet(image):
49
+ resized = cv2.resize(image, (320,320), interpolation=cv2.INTER_AREA)
50
+ input_data = resized.astype(np.float32) # Set as 3D RGB float array
51
+ input_data /= 255. # Normalize
52
+ input_data = np.expand_dims(input_data, axis=0) # Batch dimension (wrap in 4D)
53
+
54
+ # Initialize input tensor
55
+ interpreter.set_tensor(input_details[0]['index'], input_data)
56
+ interpreter.invoke()
57
+ output_data = interpreter.get_tensor(output_details[0]['index'])
58
+
59
+ # Bounding boxes
60
+ boxes = interpreter.get_tensor(output_details[1]['index'])
61
+
62
+ text = None
63
+ # For index and confidence value of the first class [0]
64
+ for i, confidence in enumerate(output_data[0]):
65
+ if confidence > .3:
66
+ text, crop = execute_text_recognition_tflite(
67
+ boxes[0][i], image,
68
+ recog_interpreter, recog_input_details, recog_output_details,
69
+ )
70
+ return text, crop
71
+ image = gr.inputs.Image(shape=(1920,1080))
72
+ output_image =gr.outputs.Image(type="auto", label="Output")
73
+
74
+
75
+ title = "Automatic licence plate detection and recognition"
76
+ description = "Gradio demo for an automatic licence plate recognition system. To use it, simply upload your image of a car with a licence plate, or click one of the examples to load them. Read more at the links below."
77
+ article = "<p style='text-align: center'><a href='https://ieeexplore.ieee.org/document/9071863'>Robust Real time Lightweight Automatic License plate Recognition System for Iranian License Plates</a> | <a href='https://github.com/clsandoval/LPRnet-keras'>Github Repo</a></p>"
78
+
79
+
80
+ iface = gr.Interface(
81
+ fn=greet,
82
+ inputs=image,
83
+ outputs=["text",output_image],
84
+ title = title,
85
+ description = description,
86
+ article=article,
87
+ examples = [
88
+ "3.jpg",
89
+ "4.jpg",
90
+ ]
91
+ )
92
+ 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
recognition2.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0e5552d69c86c8d596be83aad9dffdf9ee466a5932db2f37d07bbe37cab64e35
3
+ size 797248
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ opencv-contrib-python==4.5.4.58
2
+ opencv-python==4.5.5.62
3
+ opencv-python-headless==4.5.4.58
4
+ tensorflow==2.7.0