justin2341 commited on
Commit
53bc3f4
·
verified ·
1 Parent(s): 0d50927

Upload 8 files

Browse files
Files changed (9) hide show
  1. .gitattributes +1 -0
  2. Dockerfile +44 -0
  3. alprsdk.py +26 -0
  4. app.py +175 -0
  5. demo.py +100 -0
  6. libalpr.so +3 -0
  7. libopencv.zip +3 -0
  8. requirements.txt +5 -0
  9. run.sh +3 -0
.gitattributes CHANGED
@@ -35,3 +35,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
  alpr_examples/test2.jpg filter=lfs diff=lfs merge=lfs -text
37
  alpr_examples/test3.jpg filter=lfs diff=lfs merge=lfs -text
 
 
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
  alpr_examples/test2.jpg filter=lfs diff=lfs merge=lfs -text
37
  alpr_examples/test3.jpg filter=lfs diff=lfs merge=lfs -text
38
+ libalpr.so filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM openvino/ubuntu20_runtime:2024.5.0
2
+
3
+ USER root
4
+ RUN rm -rf /var/lib/apt/lists/* && apt update && apt install -y unzip \
5
+ libjpeg8 \
6
+ libwebp6 \
7
+ libpng16-16 \
8
+ libtbb2 \
9
+ libtiff5 \
10
+ libtbb-dev \
11
+ libopenexr-dev \
12
+ libgl1-mesa-glx \
13
+ libglib2.0-0
14
+
15
+ # Set up working directory
16
+ RUN mkdir -p /home/openvino/kby-ai-alpr
17
+ WORKDIR /home/openvino/kby-ai-alpr
18
+
19
+ # Copy shared libraries and application files
20
+ COPY ./libopencv.zip .
21
+ RUN unzip libopencv.zip
22
+ RUN cp -f libopencv/* /usr/local/lib/
23
+ RUN ldconfig
24
+
25
+ # Copy Python and application files
26
+ COPY ./libalpr.so .
27
+ COPY ./app.py .
28
+ COPY ./demo.py .
29
+ COPY ./alprsdk.py .
30
+ COPY ./requirements.txt .
31
+ COPY ./alpr_examples ./alpr_examples
32
+ COPY ./run.sh .
33
+ COPY ./model ./model
34
+
35
+ # Install Python dependencies
36
+
37
+ RUN pip3 install --no-cache-dir -r requirements.txt
38
+ # RUN chmod +x ./run.sh
39
+ # USER openvino
40
+ # Set up entrypoint
41
+ CMD ["bash", "./run.sh"]
42
+
43
+ # Expose ports
44
+ EXPOSE 8080 9000
alprsdk.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from ctypes import *
4
+
5
+ libPath = os.path.abspath(os.path.dirname(__file__)) + '/libalpr.so'
6
+ alprsdk = cdll.LoadLibrary(libPath)
7
+
8
+ getMachineCode = alprsdk.getMachineCode
9
+ getMachineCode.argtypes = []
10
+ getMachineCode.restype = c_char_p
11
+
12
+ setActivation = alprsdk.setActivation
13
+ setActivation.argtypes = [c_char_p]
14
+ setActivation.restype = c_int32
15
+
16
+ initSDK = alprsdk.initSDK
17
+ initSDK.argtypes = []
18
+ initSDK.restype = c_int32
19
+
20
+ getLicensePlate = alprsdk.get_license_using_bytes
21
+ getLicensePlate.argtypes = [c_char_p, c_ulong, POINTER(POINTER(c_char_p)), POINTER(c_int), POINTER(c_float)]
22
+ getLicensePlate.restype = c_int32
23
+
24
+ freeLicenseResults = alprsdk.free_license_results
25
+ freeLicenseResults.argtypes = [POINTER(c_char_p), c_int]
26
+ freeLicenseResults.restype = None
app.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ sys.path.append('.')
3
+
4
+ import os
5
+ import base64
6
+ import json
7
+ from ctypes import *
8
+ from alprsdk import initSDK, getLicensePlate, getMachineCode, freeLicenseResults, setActivation
9
+ import cv2
10
+ import numpy as np
11
+ from flask import Flask, request, jsonify
12
+
13
+
14
+ licensePath = "license.txt"
15
+ license = ""
16
+
17
+ machineCode = getMachineCode()
18
+ print("\nmachineCode: ", machineCode.decode('utf-8'))
19
+
20
+ # Get a specific environment variable by name
21
+ license = os.environ.get("LICENSE")
22
+
23
+ # Check if the variable exists
24
+ if license is not None:
25
+ print("Value of LICENSE:")
26
+ else:
27
+ license = ""
28
+ try:
29
+ with open(licensePath, 'r') as file:
30
+ license = file.read().strip()
31
+ except IOError as exc:
32
+ print("failed to open license.txt: ", exc.errno)
33
+ print("license: ", license)
34
+
35
+ ret = setActivation(license.encode('utf-8'))
36
+ print("\nactivation: ", ret)
37
+
38
+ ret = initSDK()
39
+ print("init: ", ret)
40
+
41
+ app = Flask(__name__)
42
+
43
+ def mat_to_bytes(mat):
44
+ """
45
+ Convert cv::Mat image data (NumPy array in Python) to raw bytes.
46
+ """
47
+ # Encode cv::Mat as PNG bytes
48
+ is_success, buffer = cv2.imencode(".png", mat)
49
+ if not is_success:
50
+ raise ValueError("Failed to encode cv::Mat image")
51
+ return buffer.tobytes()
52
+
53
+ @app.route('/alpr', methods=['POST'])
54
+ def alpr():
55
+ result = "None"
56
+ license = {}
57
+ box = {}
58
+ pro = {}
59
+
60
+ file = request.files['file']
61
+
62
+ try:
63
+ image = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
64
+ image = cv2.resize(image, (1024, 640))
65
+ except:
66
+ result = "Failed to open file1"
67
+ response = jsonify({"result": result, "plate number": license, "coordinate": box, "score": pro})
68
+
69
+ response.status_code = 200
70
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
71
+ return response
72
+
73
+
74
+ img_byte = mat_to_bytes(image)
75
+
76
+ recog_array = (c_int * 1024)() # Assuming a maximum of 256 rectangles
77
+ score_array = (c_float * 1024)() # Assuming a maximum of 256 rectangles
78
+
79
+ license_plate_ptr = POINTER(c_char_p)()
80
+ cnt = getLicensePlate(img_byte, len(img_byte), byref(license_plate_ptr), recog_array, score_array)
81
+
82
+ license_plate = [license_plate_ptr[i].decode('utf-8') for i in range(cnt)]
83
+ rectangles = [
84
+ (recog_array[i * 4], recog_array[i * 4 + 1], recog_array[i * 4 + 2], recog_array[i * 4 + 3])
85
+ for i in range(cnt)]
86
+ scores = [score_array[i] for i in range(cnt)]
87
+
88
+ freeLicenseResults(license_plate_ptr, cnt)
89
+
90
+ print("number: ", cnt, rectangles, license_plate)
91
+ if cnt == 0:
92
+ result = "Nothing Detected !"
93
+ response = jsonify({"result": result, "plate number": license, "coordinate": box, "score": pro})
94
+
95
+ response.status_code = 200
96
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
97
+ return response
98
+
99
+ result = "License Plate Number Detected !"
100
+ for i in range(cnt):
101
+ license[f"vehicle {i + 1}"] = license_plate[i]
102
+ box[f"vehicle {i + 1}"] = rectangles[i]
103
+ pro[f"vehicle {i + 1}"] = scores[i]
104
+
105
+ response = jsonify({"result": result, "plate number": license, "coordinate": box, "score": pro})
106
+
107
+ response.status_code = 200
108
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
109
+ return response
110
+
111
+ @app.route('/alpr_base64', methods=['POST'])
112
+ def alpr_base64():
113
+
114
+ result = "None"
115
+ license = {}
116
+ box = {}
117
+ pro = {}
118
+
119
+ content = request.get_json()
120
+
121
+ try:
122
+ imageBase64 = content['base64']
123
+ image_data = base64.b64decode(imageBase64)
124
+ np_array = np.frombuffer(image_data, np.uint8)
125
+ image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
126
+ image = cv2.resize(image, (1024, 640))
127
+ except:
128
+ result = "Failed to open file1"
129
+ response = jsonify({"result": result, "plate number": license, "coordinate": box, "score": pro})
130
+
131
+ response.status_code = 200
132
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
133
+ return response
134
+
135
+
136
+ img_byte = mat_to_bytes(image)
137
+
138
+ recog_array = (c_int * 1024)() # Assuming a maximum of 256 rectangles
139
+ score_array = (c_float * 1024)() # Assuming a maximum of 256 rectangles
140
+
141
+ license_plate_ptr = POINTER(c_char_p)()
142
+ cnt = getLicensePlate(img_byte, len(img_byte), byref(license_plate_ptr), recog_array)
143
+
144
+ license_plate = [license_plate_ptr[i].decode('utf-8') for i in range(cnt)]
145
+ rectangles = [
146
+ (recog_array[i * 4], recog_array[i * 4 + 1], recog_array[i * 4 + 2], recog_array[i * 4 + 3])
147
+ for i in range(cnt)]
148
+ scores = [score_array[i] for i in range(cnt)]
149
+
150
+ freeLicenseResults(license_plate_ptr, cnt)
151
+
152
+ # print("number: ", cnt, rectangles, license_plate)
153
+ if cnt == 0:
154
+ result = "Nothing Detected !"
155
+ response = jsonify({"result": result, "plate number": license, "coordinate": box, "score": pro})
156
+
157
+ response.status_code = 200
158
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
159
+ return response
160
+
161
+ result = "License Plate Number Detected !"
162
+ for i in range(cnt):
163
+ license[f"vehicle {i + 1}"] = license_plate[i]
164
+ box[f"vehicle {i + 1}"] = rectangles[i]
165
+ pro[f"vehicle {i + 1}"] = scores[i]
166
+
167
+ response = jsonify({"result": result, "plate number": license, "coordinate": box, "score": pro})
168
+
169
+ response.status_code = 200
170
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
171
+ return response
172
+
173
+ if __name__ == '__main__':
174
+ port = int(os.environ.get("PORT", 8080))
175
+ app.run(host='0.0.0.0', port=port)
demo.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from PIL import Image
4
+ import io
5
+ import cv2
6
+ import numpy as np
7
+
8
+ alpr_count = 0
9
+ def plot_one_box(x, img, color=None, label=None, score=None, line_thickness=3):
10
+ # Plots one bounding box on image img
11
+ tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness
12
+ color = color
13
+ c1, c2 = (int(x[0]), int(x[1])), (int(x[2])+int(x[0]), int(x[3])+int(x[1]))
14
+ cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
15
+ if label:
16
+ tf = max(tl - 1, 1) # font thickness
17
+ t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
18
+ c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
19
+ cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled
20
+ cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [0, 0, 0], thickness=tf, lineType=cv2.LINE_AA)
21
+
22
+ pro = f"{score:.3f}"
23
+ t_size = cv2.getTextSize(pro, 0, fontScale=tl / 3, thickness=tf)[0]
24
+ c1 = c2
25
+ c2 = c1[0] + t_size[0], c1[1] + t_size[1] + 3
26
+ cv2.rectangle(img, c1, c2, [0, 255, 255], -1, cv2.LINE_AA) # filled
27
+ cv2.putText(img, pro, (c1[0], c2[1] - 2), 0, tl / 3, [0, 0, 0], thickness=tf, lineType=cv2.LINE_AA)
28
+ return img
29
+
30
+ def alpr(frame):
31
+ global alpr_count
32
+
33
+ alpr_count = alpr_count + 1
34
+ print("alpr_count", alpr_count)
35
+ url = "http://127.0.0.1:8080/alpr"
36
+ file = {'file': open(frame, 'rb')}
37
+
38
+ r = requests.post(url=url, files=file)
39
+
40
+ alpr_output = None
41
+ result = r.json().get('result')
42
+ plate_number = r.json().get('plate number')
43
+ box = r.json().get('coordinate')
44
+ pro = r.json().get('score')
45
+ # print("\n number: ", plate_number)
46
+ # print("\n coordinate: ", box)
47
+ # print("\n score: ", pro)
48
+ try:
49
+ image = cv2.imread(frame, cv2.IMREAD_COLOR)
50
+ if image is None:
51
+ print('image is null')
52
+ sys.exit()
53
+ image = cv2.resize(image, (1024, 640))
54
+ for alpr in plate_number:
55
+ # print(plate_number)
56
+ image = plot_one_box(box[alpr], image, label=plate_number[alpr], score=pro[alpr], color=[0, 255, 0], line_thickness=1)
57
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
58
+
59
+ alpr_output = image.copy()
60
+
61
+ except:
62
+ pass
63
+
64
+ return alpr_output
65
+
66
+ with gr.Blocks() as demo:
67
+ gr.Markdown(
68
+ """
69
+ # KBY-AI License Plate Recognition
70
+ We offer SDKs for face recognition, liveness detection(anti-spoofing), ID card recognition and ID document liveness detection.
71
+ We also specialize in providing outsourcing services with a variety of technical stacks like AI(Computer Vision/Machine Learning), mobile apps, and web apps.
72
+
73
+ ##### KYC Verification Demo - https://github.com/kby-ai/KYC-Verification-Demo-Android
74
+ ##### ID Capture Web Demo - https://cap.kby-ai.com
75
+ """
76
+ )
77
+
78
+ with gr.TabItem("License Plate Recognition"):
79
+ gr.Markdown(
80
+ """
81
+ ##### Docker Hub - https://hub.docker.com/r/kbyai/license-plate-recognition
82
+ ```bash
83
+ sudo docker pull kbyai/license-plate-recognition:latest
84
+ sudo docker run -v ./license.txt:/home/openvino/kby-ai-alpr/license.txt -p 8081:8080 -p 9001:9000 kbyai/license-plate-recognition:latest
85
+ ```
86
+ """
87
+ )
88
+ with gr.Row():
89
+ with gr.Column():
90
+ alpr_image_input = gr.Image(type='filepath', height=300)
91
+ gr.Examples(['alpr_examples/test4.jpeg', 'alpr_examples/test5.jpeg', 'alpr_examples/test3.jpg', 'alpr_examples/test2.jpg'],
92
+ inputs=alpr_image_input)
93
+ alpr_confirmation_button = gr.Button("Confirm")
94
+ with gr.Column():
95
+ alpr_output = gr.Image(type="numpy")
96
+
97
+ alpr_confirmation_button.click(alpr, inputs=alpr_image_input, outputs=alpr_output)
98
+ gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fweb.kby-ai.com%2F"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fweb.kby-ai.com%2F&label=VISITORS&countColor=%23263759" /></a>')
99
+
100
+ demo.launch(server_name="0.0.0.0", server_port=7860)
libalpr.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f3be69622148aa0539513515d4697fdbf9cea831a5f9b4ab65c42afa78503e72
3
+ size 3286871
libopencv.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8845c1412c45c484e054235269944e2ac43c90a148ce3444215fe52049cf7479
3
+ size 61014815
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ flask
2
+ flask-cors
3
+ gradio==3.50.2
4
+ datadog_api_client
5
+ opencv-python
run.sh ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # cd /home/openvino/kby-ai-alpr
2
+ exec python3 demo.py &
3
+ exec python3 app.py