justin2341 commited on
Commit
1c3792f
·
verified ·
1 Parent(s): 5a62201

Upload 8 files

Browse files
Files changed (9) hide show
  1. .gitattributes +1 -0
  2. Dockerfile +43 -0
  3. alprsdk.py +26 -0
  4. app.py +159 -0
  5. demo.py +159 -0
  6. libalpr.so +3 -0
  7. libopencv.zip +3 -0
  8. requirements.txt +5 -0
  9. run.sh +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ libalpr.so filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ./alprsdk.py .
29
+ COPY ./requirements.txt .
30
+ COPY ./license.txt .
31
+ COPY ./run.sh .
32
+ COPY ./model ./model
33
+
34
+ # Install Python dependencies
35
+
36
+ RUN pip3 install --no-cache-dir -r requirements.txt
37
+ # RUN chmod +x ./run.sh
38
+ # USER openvino
39
+ # Set up entrypoint
40
+ CMD ["bash", "./run.sh"]
41
+
42
+ # Expose ports
43
+ 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)]
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,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ try:
21
+ with open(licensePath, 'r') as file:
22
+ license = file.read().strip()
23
+ except IOError as exc:
24
+ print("failed to open license.txt: ", exc.errno)
25
+
26
+ print("\nlicense: ", license)
27
+
28
+ ret = setActivation(license.encode('utf-8'))
29
+ print("\nactivation: ", ret)
30
+
31
+ ret = initSDK()
32
+ print("init: ", ret)
33
+
34
+ app = Flask(__name__)
35
+
36
+ def mat_to_bytes(mat):
37
+ """
38
+ Convert cv::Mat image data (NumPy array in Python) to raw bytes.
39
+ """
40
+ # Encode cv::Mat as PNG bytes
41
+ is_success, buffer = cv2.imencode(".png", mat)
42
+ if not is_success:
43
+ raise ValueError("Failed to encode cv::Mat image")
44
+ return buffer.tobytes()
45
+
46
+ @app.route('/alpr', methods=['POST'])
47
+ def alpr():
48
+ result = "None"
49
+ license = {}
50
+ box = {}
51
+
52
+ file = request.files['file']
53
+
54
+ try:
55
+ image = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
56
+
57
+ except:
58
+ result = "Failed to open file1"
59
+ response = jsonify({"result": result, "plate number": license, "coordinate": box})
60
+
61
+ response.status_code = 200
62
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
63
+ return response
64
+
65
+
66
+ img_byte = mat_to_bytes(image)
67
+
68
+ recog_array = (c_int * 1024)() # Assuming a maximum of 256 rectangles
69
+
70
+ license_plate_ptr = POINTER(c_char_p)()
71
+ cnt = getLicensePlate(img_byte, len(img_byte), byref(license_plate_ptr), recog_array)
72
+
73
+ license_plate = [license_plate_ptr[i].decode('utf-8') for i in range(cnt)]
74
+ rectangles = [
75
+ (recog_array[i * 4], recog_array[i * 4 + 1], recog_array[i * 4 + 2], recog_array[i * 4 + 3])
76
+ for i in range(cnt)]
77
+
78
+ freeLicenseResults(license_plate_ptr, cnt)
79
+
80
+ # print("number: ", cnt, rectangles, license_plate)
81
+ if cnt == 0:
82
+ result = "Nothing Detected !"
83
+ response = jsonify({"result": result, "plate number": license, "coordinate": box})
84
+
85
+ response.status_code = 200
86
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
87
+ return response
88
+
89
+ result = "License Plate Number Detected !"
90
+ for i in range(cnt):
91
+ license[f"vehicle {i + 1}"] = license_plate[i]
92
+ box[f"vehicle {i + 1}"] = rectangles[i]
93
+
94
+ response = jsonify({"result": result, "plate number": license, "coordinate": box})
95
+
96
+ response.status_code = 200
97
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
98
+ return response
99
+
100
+ @app.route('/alpr_base64', methods=['POST'])
101
+ def alpr_base64():
102
+
103
+ result = "None"
104
+ license = {}
105
+ box = {}
106
+
107
+ content = request.get_json()
108
+
109
+ try:
110
+ imageBase64 = content['base64']
111
+ image_data = base64.b64decode(imageBase64)
112
+ np_array = np.frombuffer(image_data, np.uint8)
113
+ image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
114
+ except:
115
+ result = "Failed to open file1"
116
+ response = jsonify({"result": result, "plate number": license, "coordinate": box})
117
+
118
+ response.status_code = 200
119
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
120
+ return response
121
+
122
+
123
+ img_byte = mat_to_bytes(image)
124
+
125
+ recog_array = (c_int * 1024)() # Assuming a maximum of 256 rectangles
126
+
127
+ license_plate_ptr = POINTER(c_char_p)()
128
+ cnt = getLicensePlate(img_byte, len(img_byte), byref(license_plate_ptr), recog_array)
129
+
130
+ license_plate = [license_plate_ptr[i].decode('utf-8') for i in range(cnt)]
131
+ rectangles = [
132
+ (recog_array[i * 4], recog_array[i * 4 + 1], recog_array[i * 4 + 2], recog_array[i * 4 + 3])
133
+ for i in range(cnt)]
134
+
135
+ freeLicenseResults(license_plate_ptr, cnt)
136
+
137
+ # print("number: ", cnt, rectangles, license_plate)
138
+ if cnt == 0:
139
+ result = "Nothing Detected !"
140
+ response = jsonify({"result": result, "plate number": license, "coordinate": box})
141
+
142
+ response.status_code = 200
143
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
144
+ return response
145
+
146
+ result = "License Plate Number Detected !"
147
+ for i in range(cnt):
148
+ license[f"vehicle {i + 1}"] = license_plate[i]
149
+ box[f"vehicle {i + 1}"] = rectangles[i]
150
+
151
+ response = jsonify({"result": result, "plate number": license, "coordinate": box})
152
+
153
+ response.status_code = 200
154
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
155
+ return response
156
+
157
+ if __name__ == '__main__':
158
+ port = int(os.environ.get("PORT", 8080))
159
+ app.run(host='0.0.0.0', port=port)
demo.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ try:
21
+ with open(licensePath, 'r') as file:
22
+ license = file.read().strip()
23
+ except IOError as exc:
24
+ print("failed to open license.txt: ", exc.errno)
25
+
26
+ print("\nlicense: ", license)
27
+
28
+ ret = setActivation(license.encode('utf-8'))
29
+ print("\nactivation: ", ret)
30
+
31
+ ret = initSDK()
32
+ print("init: ", ret)
33
+
34
+ app = Flask(__name__)
35
+
36
+ def mat_to_bytes(mat):
37
+ """
38
+ Convert cv::Mat image data (NumPy array in Python) to raw bytes.
39
+ """
40
+ # Encode cv::Mat as PNG bytes
41
+ is_success, buffer = cv2.imencode(".png", mat)
42
+ if not is_success:
43
+ raise ValueError("Failed to encode cv::Mat image")
44
+ return buffer.tobytes()
45
+
46
+ @app.route('/alpr', methods=['POST'])
47
+ def alpr():
48
+ result = "None"
49
+ license = {}
50
+ box = {}
51
+
52
+ file = request.files['file']
53
+
54
+ try:
55
+ image = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
56
+
57
+ except:
58
+ result = "Failed to open file1"
59
+ response = jsonify({"result": result, "plate number": license, "coordinate": box})
60
+
61
+ response.status_code = 200
62
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
63
+ return response
64
+
65
+
66
+ img_byte = mat_to_bytes(image)
67
+
68
+ recog_array = (c_int * 1024)() # Assuming a maximum of 256 rectangles
69
+
70
+ license_plate_ptr = POINTER(c_char_p)()
71
+ cnt = getLicensePlate(img_byte, len(img_byte), byref(license_plate_ptr), recog_array)
72
+
73
+ license_plate = [license_plate_ptr[i].decode('utf-8') for i in range(cnt)]
74
+ rectangles = [
75
+ (recog_array[i * 4], recog_array[i * 4 + 1], recog_array[i * 4 + 2], recog_array[i * 4 + 3])
76
+ for i in range(cnt)]
77
+
78
+ freeLicenseResults(license_plate_ptr, cnt)
79
+
80
+ # print("number: ", cnt, rectangles, license_plate)
81
+ if cnt == 0:
82
+ result = "Nothing Detected !"
83
+ response = jsonify({"result": result, "plate number": license, "coordinate": box})
84
+
85
+ response.status_code = 200
86
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
87
+ return response
88
+
89
+ result = "License Plate Number Detected !"
90
+ for i in range(cnt):
91
+ license[f"vehicle {i + 1}"] = license_plate[i]
92
+ box[f"vehicle {i + 1}"] = rectangles[i]
93
+
94
+ response = jsonify({"result": result, "plate number": license, "coordinate": box})
95
+
96
+ response.status_code = 200
97
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
98
+ return response
99
+
100
+ @app.route('/alpr_base64', methods=['POST'])
101
+ def alpr_base64():
102
+
103
+ result = "None"
104
+ license = {}
105
+ box = {}
106
+
107
+ content = request.get_json()
108
+
109
+ try:
110
+ imageBase64 = content['base64']
111
+ image_data = base64.b64decode(imageBase64)
112
+ np_array = np.frombuffer(image_data, np.uint8)
113
+ image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
114
+ except:
115
+ result = "Failed to open file1"
116
+ response = jsonify({"result": result, "plate number": license, "coordinate": box})
117
+
118
+ response.status_code = 200
119
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
120
+ return response
121
+
122
+
123
+ img_byte = mat_to_bytes(image)
124
+
125
+ recog_array = (c_int * 1024)() # Assuming a maximum of 256 rectangles
126
+
127
+ license_plate_ptr = POINTER(c_char_p)()
128
+ cnt = getLicensePlate(img_byte, len(img_byte), byref(license_plate_ptr), recog_array)
129
+
130
+ license_plate = [license_plate_ptr[i].decode('utf-8') for i in range(cnt)]
131
+ rectangles = [
132
+ (recog_array[i * 4], recog_array[i * 4 + 1], recog_array[i * 4 + 2], recog_array[i * 4 + 3])
133
+ for i in range(cnt)]
134
+
135
+ freeLicenseResults(license_plate_ptr, cnt)
136
+
137
+ # print("number: ", cnt, rectangles, license_plate)
138
+ if cnt == 0:
139
+ result = "Nothing Detected !"
140
+ response = jsonify({"result": result, "plate number": license, "coordinate": box})
141
+
142
+ response.status_code = 200
143
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
144
+ return response
145
+
146
+ result = "License Plate Number Detected !"
147
+ for i in range(cnt):
148
+ license[f"vehicle {i + 1}"] = license_plate[i]
149
+ box[f"vehicle {i + 1}"] = rectangles[i]
150
+
151
+ response = jsonify({"result": result, "plate number": license, "coordinate": box})
152
+
153
+ response.status_code = 200
154
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
155
+ return response
156
+
157
+ if __name__ == '__main__':
158
+ port = int(os.environ.get("PORT", 8080))
159
+ app.run(host='0.0.0.0', port=port)
libalpr.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2f090f94e75de52c565f24df6c102bd6b21d81fdb004400dfd6579987db27e69
3
+ size 3287551
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