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

Upload 2 files

Browse files
Files changed (2) hide show
  1. Dockerfile +45 -44
  2. app.py +177 -174
Dockerfile CHANGED
@@ -1,44 +1,45 @@
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
 
 
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
+ 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 .
22
+ RUN unzip libopencv.zip
23
+ 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 ./demo.py .
33
+ COPY ./ncnn.zip .
34
+ RUN unzip ncnn.zip
35
+
36
+ # Install Python dependencies
37
+
38
+ RUN pip3 install --no-cache-dir -r requirements.txt
39
+ # RUN chmod +x ./run.sh
40
+ # USER openvino
41
+ # Set up entrypoint
42
+ CMD ["bash", "./run.sh"]
43
+
44
+ # Expose ports
45
+ EXPOSE 8080 9000
app.py CHANGED
@@ -1,175 +1,178 @@
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)
 
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
+ # 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('/fire', methods=['POST'])
54
+ def fire():
55
+ result = "None"
56
+ object_name = {}
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 file"
67
+ response = jsonify({"result": result, "class": object_name, "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
+ img_byte = mat_to_bytes(image)
74
+
75
+ box_array = (c_int * 1024)() # Assuming a maximum of 256 rectangles
76
+ score_array = (c_float * 1024)() # Assuming a maximum of 256 rectangles
77
+ label_array = (c_int * 1024)()
78
+
79
+ cnt = getFireDetection(img_byte, len(img_byte), label_array, box_array, score_array)
80
+
81
+ rectangles = [
82
+ (box_array[i * 4], box_array[i * 4 + 1], box_array[i * 4 + 2], box_array[i * 4 + 3])
83
+ for i in range(cnt)]
84
+ scores = [score_array[i] for i in range(cnt)]
85
+ labels = [label_array[i] for i in range(cnt)]
86
+
87
+ # print(f"detection number: {cnt}, box: {rectangles}, labels: {labels}, scores: {scores} \n")
88
+
89
+ if cnt == 0:
90
+ result = "Nothing Detected !"
91
+ response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})
92
+
93
+ response.status_code = 200
94
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
95
+ return response
96
+
97
+ result = "Fire or Smoke Detected !"
98
+ for i in range(cnt):
99
+ if labels[i] == 0:
100
+ object_name[f"id {i + 1}"] = "fire"
101
+ else:
102
+ object_name[f"id {i + 1}"] = "smoke"
103
+ box[f"id {i + 1}"] = rectangles[i]
104
+ pro[f"id {i + 1}"] = scores[i]
105
+
106
+ response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})
107
+
108
+ response.status_code = 200
109
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
110
+ return response
111
+
112
+ @app.route('/fire_base64', methods=['POST'])
113
+ def fire_base64():
114
+
115
+ result = "None"
116
+ object_name = {}
117
+ box = {}
118
+ pro = {}
119
+
120
+ content = request.get_json()
121
+
122
+ try:
123
+ imageBase64 = content['base64']
124
+ image_data = base64.b64decode(imageBase64)
125
+ np_array = np.frombuffer(image_data, np.uint8)
126
+ image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
127
+ # image = cv2.resize(image, (1024, 640))
128
+ except:
129
+ result = "Failed to open file1"
130
+ response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})
131
+
132
+ response.status_code = 200
133
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
134
+ return response
135
+
136
+
137
+ img_byte = mat_to_bytes(image)
138
+
139
+ box_array = (c_int * 1024)() # Assuming a maximum of 256 rectangles
140
+ score_array = (c_float * 1024)() # Assuming a maximum of 256 rectangles
141
+ label_array = (c_int * 1024)()
142
+
143
+ cnt = getFireDetection(img_byte, len(img_byte), label_array, box_array, score_array)
144
+
145
+ rectangles = [
146
+ (box_array[i * 4], box_array[i * 4 + 1], box_array[i * 4 + 2], box_array[i * 4 + 3])
147
+ for i in range(cnt)]
148
+ scores = [score_array[i] for i in range(cnt)]
149
+ labels = [label_array[i] for i in range(cnt)]
150
+
151
+ # print(f"detection number: {cnt}, box: {rectangles}, labels: {labels}, scores: {scores} \n")
152
+
153
+ if cnt == 0:
154
+ result = "Nothing Detected !"
155
+ response = jsonify({"result": result, "class": object_name, "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 = "Fire or Smoke Detected !"
162
+ for i in range(cnt):
163
+ if labels[i] == 0:
164
+ object_name[f"id {i + 1}"] = "fire"
165
+ else:
166
+ object_name[f"id {i + 1}"] = "smoke"
167
+ box[f"id {i + 1}"] = rectangles[i]
168
+ pro[f"id {i + 1}"] = scores[i]
169
+
170
+ response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})
171
+
172
+ response.status_code = 200
173
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
174
+ return response
175
+
176
+ if __name__ == '__main__':
177
+ port = int(os.environ.get("PORT", 8080))
178
  app.run(host='0.0.0.0', port=port)