윤영준 윤영준 05-24
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
+++ inference_endpoint.py
@@ -100,7 +100,7 @@
             self.mask = cv2.resize(self.mask, (image.shape[0], image.shape[1]))
             self.mask_blob = cv2.imencode('.png', self.mask)
             self.mask_blob = self.mask.tobytes()
-            self.seg_image = overlay_mask(image, self.mask[0], color=(0, 255, 0), alpha=0.3)
+            self.seg_image = overlay_mask(image, self.mask[:,:,0], color=(0, 255, 0), alpha=0.3)
             self.area_percent = np.sum(self.mask) / image.shape[0] * image.shape[1]
         else :
             self.area_percent = 0
@@ -113,7 +113,7 @@
 
     def send_result(self):
         time_sent = datetime.now(self.time_zone).strftime("%Y-%m-%dT%H:%M:%SZ")
-        print(str(self.flag_detected))
+        # print(str(self.flag_detected))
         header = {
             'X-Time-Sent': time_sent,
             'X-CCTV-Name': base64.b64encode(str(self.cctv_name).encode('utf-8')).decode('ascii'),
@@ -149,6 +149,7 @@
             )
             header["Content-Type"] = multipart_data.content_type
             response = session.post(self.endpoint, headers=header, data=multipart_data)
+            print(response)
         else:
             multipart_data = MultipartEncoder(
                 fields={
@@ -161,6 +162,7 @@
             )
             header["Content-Type"] = multipart_data.content_type
             response = session.post(self.endpoint, headers=header, data=multipart_data)
+            print(response)
 
         # except Exception as e:
         #     print(e)
postprocess_draft.py
--- postprocess_draft.py
+++ postprocess_draft.py
@@ -1,5 +1,5 @@
 import numpy as np
-from flask import Flask, request
+from flask import Flask, request, jsonify
 from flask_restx import Api, Resource, fields
 import os
 from datetime import datetime
@@ -15,7 +15,16 @@
 
 app = Flask(__name__)
 api = Api(app, version='1.0', title='CCTV Image Upload API',
-          description='A postprocessing and adaptive rate mainserver pusher')
+          description='A postprocessing and adaptive rate mainserver data pusher')
+
+image_upload_model = api.model('ImageUpload', {
+    'image': fields.String(required=True, description='Image file', dt='File'),
+    'x-cctv-info': fields.String(required=False, description='CCTV identifier'),
+    'x-time-sent': fields.String(required=False, description='Time image was sent'),
+    'x-cctv-latitude': fields.String(required=False, description='Latitude of CCTV'),
+    'x-cctv-longitude': fields.String(required=False, description='Longitude of CCTV'),
+    'X-Flag-Detected' : fields.String(required=True, description='If detected')
+})
 
 # Namespace definition
 ns = api.namespace('postprocess', description='Postprocessing of inference results')
@@ -127,40 +136,57 @@
     @ns.response(200, 'Success')
     @ns.response(400, 'Validation Error')
     def post(self):
-        self.image_type = request.headers.get('Content-Type')
-        self.cctv_name = base64.b64decode(request.headers.get('x-cctv-name', '')).decode('UTF-8')
-        self.time_sent = request.headers.get('x-time-sent', '')
-        self.cctv_latitude = request.headers.get('x-cctv-latitude', 'Not provided')
-        self.cctv_longitude = request.headers.get('x-cctv-longitude', 'Not provided')
-        self.detected = request.headers.get('x-flag-detected')
-        if self.detected == "True":
-            self.detected = True
-        elif self.detected == "False":
-            self.detected = False
-        else:
-            raise ValueError(f"Invalided value for x-flag_detected : {self.detected}")
-        print(self.detected)
-        self.area_percent = request.headers.get('x-area-percentage')
-        self.area_percent = float(self.area_percent)
-        self.image = request.files['image']
-        self.image_type = request.headers.get('Content-Type')
-        self.mask = request.files['mask']
-        self.seg_image = request.files['seg_mask']
-        self.time_sent = time.time()
+        try:
+            self.image_type = request.headers.get('Content-Type')
+            self.cctv_name = base64.b64decode(request.headers.get('x-cctv-name', '')).decode('UTF-8')
+            self.time_sent = request.headers.get('x-time-sent', '')
+            self.cctv_latitude = request.headers.get('x-cctv-latitude', 'Not provided')
+            self.cctv_longitude = request.headers.get('x-cctv-longitude', 'Not provided')
+            self.detected = request.headers.get('X-Flag-Detected')
 
-        self.cctv_info = {
-            'cctv_name' : self.cctv_name,
-            'cctv_latitude' : self.cctv_latitude,
-            'cctv_longitude' : self.cctv_longitude,
-            'source_frame' : self.image,
-            'frame_mask' : self.mask,
-            'seg_frame' : self.seg_image,
-            'time_sent' : self.time_sent
-        }
-        self.memory[self.cctv_info['cctv_name']] = self.cctv_info
-        pass_fail = self.pass_fail()
-        self.memory.add_status(self.cctv_name, pass_fail)
-        pass
+            if self.detected == "True":
+                self.detected = True
+            elif self.detected == "False":
+                self.detected = False
+            else:
+                raise ValueError(f"Invalid value for x-flag-detected: {self.detected}")
+
+            self.area_percent = request.headers.get('x-area-percentage')
+            try:
+                self.area_percent = float(self.area_percent)
+            except (TypeError, ValueError) as e:
+                raise ValueError(f"Invalid value for x-area-percentage: {self.area_percent}")
+            print(len(request.files))
+            self.image = request.files.get('image')
+            self.mask = request.files.get('mask')
+            self.seg_image = request.files.get('seg_mask')
+            self.image.save(f"network_test/image_p{time.time()}.png")
+            self.mask.save(f"network_test/mask_p{time.time()}.png")
+            self.seg_image.save(f"network_test/seg_p{time.time()}.png")
+
+            if not self.image or not self.mask or not self.seg_image:
+                raise ValueError("Missing one or more required files: 'image', 'mask', 'seg_mask'")
+
+            self.time_sent = time.time()
+
+            self.cctv_info = {
+                'cctv_name': self.cctv_name,
+                'cctv_latitude': self.cctv_latitude,
+                'cctv_longitude': self.cctv_longitude,
+                'source_frame': self.image,
+                'frame_mask': self.mask,
+                'seg_frame': self.seg_image,
+                'time_sent': self.time_sent
+            }
+
+            self.memory[self.cctv_info['cctv_name']] = self.cctv_info
+            pass_fail = self.pass_fail()
+            self.memory.add_status(self.cctv_name, pass_fail)
+
+        except ValueError as e:
+            print(e)
+        except Exception as e:
+            print(e)
 
     def pass_fail(self):
         thres = 0.1
yoloseg/inference_.py
--- yoloseg/inference_.py
+++ yoloseg/inference_.py
@@ -263,7 +263,7 @@
     # Example for displaying first mask if available:
     if mask_maps is not None:
 
-        seg_image = overlay_mask(img, mask_maps[0], color=(0, 255, 0), alpha=0.3)
+        seg_image = overlay_mask(img, mask_maps[:,:,0], color=(0, 255, 0), alpha=0.3)
         cv2.imshow("segmentation", seg_image)
         cv2.waitKey(0)
         cv2.destroyAllWindows()
@@ -315,4 +315,4 @@
 
 if __name__ == "__main__":
     pass
-    # test2()
(파일 끝에 줄바꿈 문자 없음)
+    test()
(파일 끝에 줄바꿈 문자 없음)
Add a comment
List