justin2341 commited on
Commit
2bd7989
·
verified ·
1 Parent(s): 6891211

Upload 2 files

Browse files
Files changed (2) hide show
  1. Dockerfile +9 -9
  2. app.py +174 -170
Dockerfile CHANGED
@@ -10,12 +10,11 @@ RUN rm -rf /var/lib/apt/lists/* && apt update && apt install -y unzip \
10
  libtbb-dev \
11
  libopenexr-dev \
12
  libgl1-mesa-glx \
13
- libglib2.0-0 \
14
- libgomp1
15
 
16
  # Set up working directory
17
- RUN mkdir -p /home/openvino/kby-ai-fire
18
- WORKDIR /home/openvino/kby-ai-fire
19
 
20
  # Copy shared libraries and application files
21
  COPY ./libopencv.zip .
@@ -24,20 +23,21 @@ RUN cp -f libopencv/* /usr/local/lib/
24
  RUN ldconfig
25
 
26
  # Copy Python and application files
27
- COPY ./libfire.so .
28
  COPY ./app.py .
29
- COPY ./firesdk.py .
 
30
  COPY ./requirements.txt .
 
31
  COPY ./run.sh .
32
- COPY ./ncnn.zip .
33
- RUN unzip ncnn.zip
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
 
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 .
 
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
app.py CHANGED
@@ -1,171 +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 firesdk import *
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('/fire', methods=['POST'])
47
- def fire():
48
- result = "None"
49
- object_name = {}
50
- box = {}
51
- pro = {}
52
-
53
- file = request.files['file']
54
-
55
- try:
56
- image = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
57
- # image = cv2.resize(image, (1024, 640))
58
- except:
59
- result = "Failed to open file"
60
- response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})
61
-
62
- response.status_code = 200
63
- response.headers["Content-Type"] = "application/json; charset=utf-8"
64
- return response
65
-
66
- img_byte = mat_to_bytes(image)
67
-
68
- box_array = (c_int * 1024)() # Assuming a maximum of 256 rectangles
69
- score_array = (c_float * 1024)() # Assuming a maximum of 256 rectangles
70
- label_array = (c_int * 1024)()
71
-
72
- cnt = getFireDetection(img_byte, len(img_byte), label_array, box_array, score_array)
73
-
74
- rectangles = [
75
- (box_array[i * 4], box_array[i * 4 + 1], box_array[i * 4 + 2], box_array[i * 4 + 3])
76
- for i in range(cnt)]
77
- scores = [score_array[i] for i in range(cnt)]
78
- labels = [label_array[i] for i in range(cnt)]
79
-
80
- # print(f"detection number: {cnt}, box: {rectangles}, labels: {labels}, scores: {scores} \n")
81
-
82
- if cnt == 0:
83
- result = "Nothing Detected !"
84
- response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})
85
-
86
- response.status_code = 200
87
- response.headers["Content-Type"] = "application/json; charset=utf-8"
88
- return response
89
-
90
- result = "Fire or Smoke Detected !"
91
- for i in range(cnt):
92
- if labels[i] == 0:
93
- object_name[f"id {i + 1}"] = "fire"
94
- else:
95
- object_name[f"id {i + 1}"] = "smoke"
96
- box[f"id {i + 1}"] = rectangles[i]
97
- pro[f"id {i + 1}"] = scores[i]
98
-
99
- response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})
100
-
101
- response.status_code = 200
102
- response.headers["Content-Type"] = "application/json; charset=utf-8"
103
- return response
104
-
105
- @app.route('/fire_base64', methods=['POST'])
106
- def fire_base64():
107
-
108
- result = "None"
109
- object_name = {}
110
- box = {}
111
- pro = {}
112
-
113
- content = request.get_json()
114
-
115
- try:
116
- imageBase64 = content['base64']
117
- image_data = base64.b64decode(imageBase64)
118
- np_array = np.frombuffer(image_data, np.uint8)
119
- image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
120
- # image = cv2.resize(image, (1024, 640))
121
- except:
122
- result = "Failed to open file1"
123
- response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})
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
- box_array = (c_int * 1024)() # Assuming a maximum of 256 rectangles
133
- score_array = (c_float * 1024)() # Assuming a maximum of 256 rectangles
134
- label_array = (c_int * 1024)()
135
-
136
- cnt = getFireDetection(img_byte, len(img_byte), label_array, box_array, score_array)
137
-
138
- rectangles = [
139
- (box_array[i * 4], box_array[i * 4 + 1], box_array[i * 4 + 2], box_array[i * 4 + 3])
140
- for i in range(cnt)]
141
- scores = [score_array[i] for i in range(cnt)]
142
- labels = [label_array[i] for i in range(cnt)]
143
-
144
- # print(f"detection number: {cnt}, box: {rectangles}, labels: {labels}, scores: {scores} \n")
145
-
146
- if cnt == 0:
147
- result = "Nothing Detected !"
148
- response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})
149
-
150
- response.status_code = 200
151
- response.headers["Content-Type"] = "application/json; charset=utf-8"
152
- return response
153
-
154
- result = "Fire or Smoke Detected !"
155
- for i in range(cnt):
156
- if labels[i] == 0:
157
- object_name[f"id {i + 1}"] = "fire"
158
- else:
159
- object_name[f"id {i + 1}"] = "smoke"
160
- box[f"id {i + 1}"] = rectangles[i]
161
- pro[f"id {i + 1}"] = scores[i]
162
-
163
- response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})
164
-
165
- response.status_code = 200
166
- response.headers["Content-Type"] = "application/json; charset=utf-8"
167
- return response
168
-
169
- if __name__ == '__main__':
170
- port = int(os.environ.get("PORT", 8080))
 
 
 
 
171
  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
+ 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)