Spaces:
Running
Running
Upload 2 files
Browse files- Dockerfile +45 -44
- 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 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
RUN
|
23 |
-
RUN
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
COPY ./
|
28 |
-
COPY ./
|
29 |
-
COPY ./
|
30 |
-
COPY ./requirements.txt .
|
31 |
-
COPY ./
|
32 |
-
COPY ./
|
33 |
-
COPY ./
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
#
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
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 |
+
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
|
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('/
|
54 |
-
def
|
55 |
-
result = "None"
|
56 |
-
|
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
|
67 |
-
response = jsonify({"result": result, "
|
68 |
-
|
69 |
-
response.status_code = 200
|
70 |
-
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
71 |
-
return response
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
for i in range(cnt)]
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
response =
|
94 |
-
|
95 |
-
response
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
response.
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
image = cv2.
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
response.
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
rectangles = [
|
146 |
-
(
|
147 |
-
for i in range(cnt)]
|
148 |
-
scores = [score_array[i] for i in range(cnt)]
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
if cnt == 0:
|
154 |
-
result = "Nothing Detected !"
|
155 |
-
response = jsonify({"result": result, "
|
156 |
-
|
157 |
-
response.status_code = 200
|
158 |
-
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
159 |
-
return response
|
160 |
-
|
161 |
-
result = "
|
162 |
-
for i in range(cnt):
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
response
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
|
|
|
|
|
|
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)
|