Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,159 +1,166 @@
|
|
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 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
print("
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
app.run(host='0.0.0.0', port=port)
|
|
|
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 |
+
|
59 |
+
file = request.files['file']
|
60 |
+
|
61 |
+
try:
|
62 |
+
image = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
|
63 |
+
|
64 |
+
except:
|
65 |
+
result = "Failed to open file1"
|
66 |
+
response = jsonify({"result": result, "plate number": license, "coordinate": box})
|
67 |
+
|
68 |
+
response.status_code = 200
|
69 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
70 |
+
return response
|
71 |
+
|
72 |
+
|
73 |
+
img_byte = mat_to_bytes(image)
|
74 |
+
|
75 |
+
recog_array = (c_int * 1024)() # Assuming a maximum of 256 rectangles
|
76 |
+
|
77 |
+
license_plate_ptr = POINTER(c_char_p)()
|
78 |
+
cnt = getLicensePlate(img_byte, len(img_byte), byref(license_plate_ptr), recog_array)
|
79 |
+
|
80 |
+
license_plate = [license_plate_ptr[i].decode('utf-8') for i in range(cnt)]
|
81 |
+
rectangles = [
|
82 |
+
(recog_array[i * 4], recog_array[i * 4 + 1], recog_array[i * 4 + 2], recog_array[i * 4 + 3])
|
83 |
+
for i in range(cnt)]
|
84 |
+
|
85 |
+
freeLicenseResults(license_plate_ptr, cnt)
|
86 |
+
|
87 |
+
# print("number: ", cnt, rectangles, license_plate)
|
88 |
+
if cnt == 0:
|
89 |
+
result = "Nothing Detected !"
|
90 |
+
response = jsonify({"result": result, "plate number": license, "coordinate": box})
|
91 |
+
|
92 |
+
response.status_code = 200
|
93 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
94 |
+
return response
|
95 |
+
|
96 |
+
result = "License Plate Number Detected !"
|
97 |
+
for i in range(cnt):
|
98 |
+
license[f"vehicle {i + 1}"] = license_plate[i]
|
99 |
+
box[f"vehicle {i + 1}"] = rectangles[i]
|
100 |
+
|
101 |
+
response = jsonify({"result": result, "plate number": license, "coordinate": box})
|
102 |
+
|
103 |
+
response.status_code = 200
|
104 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
105 |
+
return response
|
106 |
+
|
107 |
+
@app.route('/alpr_base64', methods=['POST'])
|
108 |
+
def alpr_base64():
|
109 |
+
|
110 |
+
result = "None"
|
111 |
+
license = {}
|
112 |
+
box = {}
|
113 |
+
|
114 |
+
content = request.get_json()
|
115 |
+
|
116 |
+
try:
|
117 |
+
imageBase64 = content['base64']
|
118 |
+
image_data = base64.b64decode(imageBase64)
|
119 |
+
np_array = np.frombuffer(image_data, np.uint8)
|
120 |
+
image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
|
121 |
+
except:
|
122 |
+
result = "Failed to open file1"
|
123 |
+
response = jsonify({"result": result, "plate number": license, "coordinate": box})
|
124 |
+
|
125 |
+
response.status_code = 200
|
126 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
127 |
+
return response
|
128 |
+
|
129 |
+
|
130 |
+
img_byte = mat_to_bytes(image)
|
131 |
+
|
132 |
+
recog_array = (c_int * 1024)() # Assuming a maximum of 256 rectangles
|
133 |
+
|
134 |
+
license_plate_ptr = POINTER(c_char_p)()
|
135 |
+
cnt = getLicensePlate(img_byte, len(img_byte), byref(license_plate_ptr), recog_array)
|
136 |
+
|
137 |
+
license_plate = [license_plate_ptr[i].decode('utf-8') for i in range(cnt)]
|
138 |
+
rectangles = [
|
139 |
+
(recog_array[i * 4], recog_array[i * 4 + 1], recog_array[i * 4 + 2], recog_array[i * 4 + 3])
|
140 |
+
for i in range(cnt)]
|
141 |
+
|
142 |
+
freeLicenseResults(license_plate_ptr, cnt)
|
143 |
+
|
144 |
+
# print("number: ", cnt, rectangles, license_plate)
|
145 |
+
if cnt == 0:
|
146 |
+
result = "Nothing Detected !"
|
147 |
+
response = jsonify({"result": result, "plate number": license, "coordinate": box})
|
148 |
+
|
149 |
+
response.status_code = 200
|
150 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
151 |
+
return response
|
152 |
+
|
153 |
+
result = "License Plate Number Detected !"
|
154 |
+
for i in range(cnt):
|
155 |
+
license[f"vehicle {i + 1}"] = license_plate[i]
|
156 |
+
box[f"vehicle {i + 1}"] = rectangles[i]
|
157 |
+
|
158 |
+
response = jsonify({"result": result, "plate number": license, "coordinate": box})
|
159 |
+
|
160 |
+
response.status_code = 200
|
161 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
162 |
+
return response
|
163 |
+
|
164 |
+
if __name__ == '__main__':
|
165 |
+
port = int(os.environ.get("PORT", 8080))
|
166 |
app.run(host='0.0.0.0', port=port)
|