Critical Bug fix : Dimension Error on putting detected mask over source image ( the dimension of mask has changed into (1,x,y) into (x,y,1)
@5f551bd697f479beb78757335e30904dde07e050
--- inference_endpoint.py
+++ inference_endpoint.py
... | ... | @@ -100,7 +100,7 @@ |
100 | 100 |
self.mask = cv2.resize(self.mask, (image.shape[0], image.shape[1])) |
101 | 101 |
self.mask_blob = cv2.imencode('.png', self.mask) |
102 | 102 |
self.mask_blob = self.mask.tobytes() |
103 |
- self.seg_image = overlay_mask(image, self.mask[0], color=(0, 255, 0), alpha=0.3) |
|
103 |
+ self.seg_image = overlay_mask(image, self.mask[:,:,0], color=(0, 255, 0), alpha=0.3) |
|
104 | 104 |
self.area_percent = np.sum(self.mask) / image.shape[0] * image.shape[1] |
105 | 105 |
else : |
106 | 106 |
self.area_percent = 0 |
... | ... | @@ -113,7 +113,7 @@ |
113 | 113 |
|
114 | 114 |
def send_result(self): |
115 | 115 |
time_sent = datetime.now(self.time_zone).strftime("%Y-%m-%dT%H:%M:%SZ") |
116 |
- print(str(self.flag_detected)) |
|
116 |
+ # print(str(self.flag_detected)) |
|
117 | 117 |
header = { |
118 | 118 |
'X-Time-Sent': time_sent, |
119 | 119 |
'X-CCTV-Name': base64.b64encode(str(self.cctv_name).encode('utf-8')).decode('ascii'), |
... | ... | @@ -149,6 +149,7 @@ |
149 | 149 |
) |
150 | 150 |
header["Content-Type"] = multipart_data.content_type |
151 | 151 |
response = session.post(self.endpoint, headers=header, data=multipart_data) |
152 |
+ print(response) |
|
152 | 153 |
else: |
153 | 154 |
multipart_data = MultipartEncoder( |
154 | 155 |
fields={ |
... | ... | @@ -161,6 +162,7 @@ |
161 | 162 |
) |
162 | 163 |
header["Content-Type"] = multipart_data.content_type |
163 | 164 |
response = session.post(self.endpoint, headers=header, data=multipart_data) |
165 |
+ print(response) |
|
164 | 166 |
|
165 | 167 |
# except Exception as e: |
166 | 168 |
# print(e) |
--- postprocess_draft.py
+++ postprocess_draft.py
... | ... | @@ -1,5 +1,5 @@ |
1 | 1 |
import numpy as np |
2 |
-from flask import Flask, request |
|
2 |
+from flask import Flask, request, jsonify |
|
3 | 3 |
from flask_restx import Api, Resource, fields |
4 | 4 |
import os |
5 | 5 |
from datetime import datetime |
... | ... | @@ -15,7 +15,16 @@ |
15 | 15 |
|
16 | 16 |
app = Flask(__name__) |
17 | 17 |
api = Api(app, version='1.0', title='CCTV Image Upload API', |
18 |
- description='A postprocessing and adaptive rate mainserver pusher') |
|
18 |
+ description='A postprocessing and adaptive rate mainserver data pusher') |
|
19 |
+ |
|
20 |
+image_upload_model = api.model('ImageUpload', { |
|
21 |
+ 'image': fields.String(required=True, description='Image file', dt='File'), |
|
22 |
+ 'x-cctv-info': fields.String(required=False, description='CCTV identifier'), |
|
23 |
+ 'x-time-sent': fields.String(required=False, description='Time image was sent'), |
|
24 |
+ 'x-cctv-latitude': fields.String(required=False, description='Latitude of CCTV'), |
|
25 |
+ 'x-cctv-longitude': fields.String(required=False, description='Longitude of CCTV'), |
|
26 |
+ 'X-Flag-Detected' : fields.String(required=True, description='If detected') |
|
27 |
+}) |
|
19 | 28 |
|
20 | 29 |
# Namespace definition |
21 | 30 |
ns = api.namespace('postprocess', description='Postprocessing of inference results') |
... | ... | @@ -127,40 +136,57 @@ |
127 | 136 |
@ns.response(200, 'Success') |
128 | 137 |
@ns.response(400, 'Validation Error') |
129 | 138 |
def post(self): |
130 |
- self.image_type = request.headers.get('Content-Type') |
|
131 |
- self.cctv_name = base64.b64decode(request.headers.get('x-cctv-name', '')).decode('UTF-8') |
|
132 |
- self.time_sent = request.headers.get('x-time-sent', '') |
|
133 |
- self.cctv_latitude = request.headers.get('x-cctv-latitude', 'Not provided') |
|
134 |
- self.cctv_longitude = request.headers.get('x-cctv-longitude', 'Not provided') |
|
135 |
- self.detected = request.headers.get('x-flag-detected') |
|
136 |
- if self.detected == "True": |
|
137 |
- self.detected = True |
|
138 |
- elif self.detected == "False": |
|
139 |
- self.detected = False |
|
140 |
- else: |
|
141 |
- raise ValueError(f"Invalided value for x-flag_detected : {self.detected}") |
|
142 |
- print(self.detected) |
|
143 |
- self.area_percent = request.headers.get('x-area-percentage') |
|
144 |
- self.area_percent = float(self.area_percent) |
|
145 |
- self.image = request.files['image'] |
|
146 |
- self.image_type = request.headers.get('Content-Type') |
|
147 |
- self.mask = request.files['mask'] |
|
148 |
- self.seg_image = request.files['seg_mask'] |
|
149 |
- self.time_sent = time.time() |
|
139 |
+ try: |
|
140 |
+ self.image_type = request.headers.get('Content-Type') |
|
141 |
+ self.cctv_name = base64.b64decode(request.headers.get('x-cctv-name', '')).decode('UTF-8') |
|
142 |
+ self.time_sent = request.headers.get('x-time-sent', '') |
|
143 |
+ self.cctv_latitude = request.headers.get('x-cctv-latitude', 'Not provided') |
|
144 |
+ self.cctv_longitude = request.headers.get('x-cctv-longitude', 'Not provided') |
|
145 |
+ self.detected = request.headers.get('X-Flag-Detected') |
|
150 | 146 |
|
151 |
- self.cctv_info = { |
|
152 |
- 'cctv_name' : self.cctv_name, |
|
153 |
- 'cctv_latitude' : self.cctv_latitude, |
|
154 |
- 'cctv_longitude' : self.cctv_longitude, |
|
155 |
- 'source_frame' : self.image, |
|
156 |
- 'frame_mask' : self.mask, |
|
157 |
- 'seg_frame' : self.seg_image, |
|
158 |
- 'time_sent' : self.time_sent |
|
159 |
- } |
|
160 |
- self.memory[self.cctv_info['cctv_name']] = self.cctv_info |
|
161 |
- pass_fail = self.pass_fail() |
|
162 |
- self.memory.add_status(self.cctv_name, pass_fail) |
|
163 |
- pass |
|
147 |
+ if self.detected == "True": |
|
148 |
+ self.detected = True |
|
149 |
+ elif self.detected == "False": |
|
150 |
+ self.detected = False |
|
151 |
+ else: |
|
152 |
+ raise ValueError(f"Invalid value for x-flag-detected: {self.detected}") |
|
153 |
+ |
|
154 |
+ self.area_percent = request.headers.get('x-area-percentage') |
|
155 |
+ try: |
|
156 |
+ self.area_percent = float(self.area_percent) |
|
157 |
+ except (TypeError, ValueError) as e: |
|
158 |
+ raise ValueError(f"Invalid value for x-area-percentage: {self.area_percent}") |
|
159 |
+ print(len(request.files)) |
|
160 |
+ self.image = request.files.get('image') |
|
161 |
+ self.mask = request.files.get('mask') |
|
162 |
+ self.seg_image = request.files.get('seg_mask') |
|
163 |
+ self.image.save(f"network_test/image_p{time.time()}.png") |
|
164 |
+ self.mask.save(f"network_test/mask_p{time.time()}.png") |
|
165 |
+ self.seg_image.save(f"network_test/seg_p{time.time()}.png") |
|
166 |
+ |
|
167 |
+ if not self.image or not self.mask or not self.seg_image: |
|
168 |
+ raise ValueError("Missing one or more required files: 'image', 'mask', 'seg_mask'") |
|
169 |
+ |
|
170 |
+ self.time_sent = time.time() |
|
171 |
+ |
|
172 |
+ self.cctv_info = { |
|
173 |
+ 'cctv_name': self.cctv_name, |
|
174 |
+ 'cctv_latitude': self.cctv_latitude, |
|
175 |
+ 'cctv_longitude': self.cctv_longitude, |
|
176 |
+ 'source_frame': self.image, |
|
177 |
+ 'frame_mask': self.mask, |
|
178 |
+ 'seg_frame': self.seg_image, |
|
179 |
+ 'time_sent': self.time_sent |
|
180 |
+ } |
|
181 |
+ |
|
182 |
+ self.memory[self.cctv_info['cctv_name']] = self.cctv_info |
|
183 |
+ pass_fail = self.pass_fail() |
|
184 |
+ self.memory.add_status(self.cctv_name, pass_fail) |
|
185 |
+ |
|
186 |
+ except ValueError as e: |
|
187 |
+ print(e) |
|
188 |
+ except Exception as e: |
|
189 |
+ print(e) |
|
164 | 190 |
|
165 | 191 |
def pass_fail(self): |
166 | 192 |
thres = 0.1 |
--- yoloseg/inference_.py
+++ yoloseg/inference_.py
... | ... | @@ -263,7 +263,7 @@ |
263 | 263 |
# Example for displaying first mask if available: |
264 | 264 |
if mask_maps is not None: |
265 | 265 |
|
266 |
- seg_image = overlay_mask(img, mask_maps[0], color=(0, 255, 0), alpha=0.3) |
|
266 |
+ seg_image = overlay_mask(img, mask_maps[:,:,0], color=(0, 255, 0), alpha=0.3) |
|
267 | 267 |
cv2.imshow("segmentation", seg_image) |
268 | 268 |
cv2.waitKey(0) |
269 | 269 |
cv2.destroyAllWindows() |
... | ... | @@ -315,4 +315,4 @@ |
315 | 315 |
|
316 | 316 |
if __name__ == "__main__": |
317 | 317 |
pass |
318 |
- # test2()(파일 끝에 줄바꿈 문자 없음) |
|
318 |
+ test()(파일 끝에 줄바꿈 문자 없음) |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?