File size: 5,710 Bytes
f7d745e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d44034
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import sys
sys.path.append('.')

import os
import base64
import json
from ctypes import *
from firesdk import *
import cv2
import numpy as np
from flask import Flask, request, jsonify


licensePath = "license.txt"
license = ""

machineCode = getMachineCode()
print("\nmachineCode: ", machineCode.decode('utf-8'))

# Get a specific environment variable by name
license = os.environ.get("LICENSE")

# Check if the variable exists
if license is not None:
    print("Value of LICENSE:")
else:
    license = ""
    try:
        with open(licensePath, 'r') as file:
            license = file.read().strip()
    except IOError as exc:
        print("failed to open license.txt: ", exc.errno)
    print("license: ", license)
    
ret = setActivation(license.encode('utf-8'))
print("\nactivation: ", ret)

ret = initSDK()
print("init: ", ret)

app = Flask(__name__) 

def mat_to_bytes(mat):
    """

    Convert cv::Mat image data (NumPy array in Python) to raw bytes.

    """
    # Encode cv::Mat as PNG bytes
    is_success, buffer = cv2.imencode(".png", mat)
    if not is_success:
        raise ValueError("Failed to encode cv::Mat image")
    return buffer.tobytes()

@app.route('/fire', methods=['POST'])
def fire():
    result = "None"
    object_name = {}
    box = {}
    pro = {}
    
    file = request.files['file']
    
    try:
        image = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
        # image = cv2.resize(image, (1024, 640))
    except:
        result = "Failed to open file"
        response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})

        response.status_code = 200
        response.headers["Content-Type"] = "application/json; charset=utf-8"
        return response

    img_byte = mat_to_bytes(image)
    
    box_array = (c_int * 1024)()  # Assuming a maximum of 256 rectangles
    score_array = (c_float * 1024)()  # Assuming a maximum of 256 rectangles
    label_array = (c_int * 1024)()
    
    cnt = getFireDetection(img_byte, len(img_byte), label_array, box_array, score_array)

    rectangles = [
    (box_array[i * 4], box_array[i * 4 + 1], box_array[i * 4 + 2], box_array[i * 4 + 3])
    for i in range(cnt)]
    scores = [score_array[i] for i in range(cnt)]
    labels = [label_array[i] for i in range(cnt)]

    # print(f"detection number: {cnt}, box: {rectangles}, labels: {labels}, scores: {scores} \n")

    if cnt == 0:
        result = "Nothing Detected !"
        response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})

        response.status_code = 200
        response.headers["Content-Type"] = "application/json; charset=utf-8"
        return response
    
    result = "Fire or Smoke Detected !"
    for i in range(cnt):
        if labels[i] == 0:
            object_name[f"id {i + 1}"] = "fire"
        else:
            object_name[f"id {i + 1}"] = "smoke"
        box[f"id {i + 1}"] = rectangles[i]
        pro[f"id {i + 1}"] = scores[i]
    
    response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})

    response.status_code = 200
    response.headers["Content-Type"] = "application/json; charset=utf-8"
    return response

@app.route('/fire_base64', methods=['POST'])
def fire_base64():

    result = "None"
    object_name = {}
    box = {}
    pro = {}
    
    content = request.get_json()

    try:
        imageBase64 = content['base64']
        image_data = base64.b64decode(imageBase64) 
        np_array = np.frombuffer(image_data, np.uint8)
        image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)  
        # image = cv2.resize(image, (1024, 640)) 
    except:
        result = "Failed to open file1"
        response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})

        response.status_code = 200
        response.headers["Content-Type"] = "application/json; charset=utf-8"
        return response
    

    img_byte = mat_to_bytes(image)
    
    box_array = (c_int * 1024)()  # Assuming a maximum of 256 rectangles
    score_array = (c_float * 1024)()  # Assuming a maximum of 256 rectangles
    label_array = (c_int * 1024)()
    
    cnt = getFireDetection(img_byte, len(img_byte), label_array, box_array, score_array)

    rectangles = [
    (box_array[i * 4], box_array[i * 4 + 1], box_array[i * 4 + 2], box_array[i * 4 + 3])
    for i in range(cnt)]
    scores = [score_array[i] for i in range(cnt)]
    labels = [label_array[i] for i in range(cnt)]

    # print(f"detection number: {cnt}, box: {rectangles}, labels: {labels}, scores: {scores} \n")

    if cnt == 0:
        result = "Nothing Detected !"
        response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})

        response.status_code = 200
        response.headers["Content-Type"] = "application/json; charset=utf-8"
        return response
    
    result = "Fire or Smoke Detected !"
    for i in range(cnt):
        if labels[i] == 0:
            object_name[f"id {i + 1}"] = "fire"
        else:
            object_name[f"id {i + 1}"] = "smoke"
        box[f"id {i + 1}"] = rectangles[i]
        pro[f"id {i + 1}"] = scores[i]
    
    response = jsonify({"result": result, "class": object_name, "coordinate": box, "score": pro})

    response.status_code = 200
    response.headers["Content-Type"] = "application/json; charset=utf-8"
    return response

if __name__ == '__main__':
    port = int(os.environ.get("PORT", 8080))
    app.run(host='0.0.0.0', port=port)