윤영준 윤영준 05-24
rewriting of event handling class, StreamSources
@5f24d8f0d2e8818c5de668674d481e215cf53fe8
inference_endpoint.py
--- inference_endpoint.py
+++ inference_endpoint.py
@@ -97,7 +97,11 @@
             self.flag_detected = False
 
         if self.flag_detected:
-            self.mask = cv2.resize(self.mask, (image.shape[0], image.shape[1]))
+            print(image.shape)
+            print(self.mask.shape)
+            self.mask = cv2.resize(self.mask, (image.shape[1], image.shape[0])) # cv2 saves image with w,h order
+            self.mask = self.mask[..., np.newaxis]
+            print(self.mask.shape)
             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)
@@ -124,50 +128,50 @@
         }
         session = requests.Session()
 
-        # try:
-        if self.flag_detected:
-            seg_binary = cv2.imencode('.png', self.seg_image)
-            seg_binary = seg_binary[1].tobytes()
-            multipart_data = MultipartEncoder(
-                fields={
-                    'image': (
-                        f'frame_{self.cctv_name}.{self.image_type}',
-                        self.image,
-                        f'image/{self.image_type}'
-                    ),
-                    'mask' : (
-                        f'frame_mask_{self.cctv_name}.{self.image_type}',
-                        self.mask_blob,
-                        f'image/{self.image_type}'
-                    ),
-                    'seg_mask' : (
-                        f'frame_seg_{self.cctv_name}.{self.image_type}',
-                        seg_binary,
-                        f'image/{self.image_type}'
-                    )
-                }
-            )
-            header["Content-Type"] = multipart_data.content_type
-            response = session.post(self.endpoint, headers=header, data=multipart_data)
-            print(response)
-        else:
-            multipart_data = MultipartEncoder(
-                fields={
-                    'image': (
-                        f'frame_{self.cctv_name}.{self.image_type}',
-                        self.image,
-                        f'image/{self.image_type}'
-                    ),
-                }
-            )
-            header["Content-Type"] = multipart_data.content_type
-            response = session.post(self.endpoint, headers=header, data=multipart_data)
-            print(response)
+        try:
+            if self.flag_detected:
+                seg_binary = cv2.imencode('.png', self.seg_image)
+                seg_binary = seg_binary[1].tobytes()
+                multipart_data = MultipartEncoder(
+                    fields={
+                        'image': (
+                            f'frame_{self.cctv_name}.{self.image_type}',
+                            self.image,
+                            f'image/{self.image_type}'
+                        ),
+                        'mask' : (
+                            f'frame_mask_{self.cctv_name}.{self.image_type}',
+                            self.mask_blob,
+                            f'image/{self.image_type}'
+                        ),
+                        'seg_mask' : (
+                            f'frame_seg_{self.cctv_name}.{self.image_type}',
+                            seg_binary,
+                            f'image/{self.image_type}'
+                        )
+                    }
+                )
+                header["Content-Type"] = multipart_data.content_type
+                response = session.post(self.endpoint, headers=header, data=multipart_data)
+                print(response)
+            else:
+                multipart_data = MultipartEncoder(
+                    fields={
+                        'image': (
+                            f'frame_{self.cctv_name}.{self.image_type}',
+                            self.image,
+                            f'image/{self.image_type}'
+                        ),
+                    }
+                )
+                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)
-        #     print("Can not connect to the postprocessing server. Check the endpoint address or connection.\n"
-        #           f"Can not connect to : {self.endpoint}")
+        except Exception as e:
+            print(e)
+            print("Can not connect to the postprocessing server. Check the endpoint address or connection.\n"
+                  f"Can not connect to : {self.endpoint}")
 
 
 if __name__ == '__main__':
postprocess_draft.py
--- postprocess_draft.py
+++ postprocess_draft.py
@@ -13,6 +13,8 @@
 
 # from config_files import API_ENDPOINT_MAIN
 
+debug = True
+
 app = Flask(__name__)
 api = Api(app, version='1.0', title='CCTV Image Upload API',
           description='A postprocessing and adaptive rate mainserver data pusher')
@@ -52,8 +54,11 @@
         self.failure_mode_thres = failure_mode_thres
         self.failure_mode_check_past_n = failure_mode_check_past_n
         self.normal_mode_thres = normal_mode_thres
+        self.normal_mode_check_past_n = normal_mode_check_past_n
+
 
     def __setitem__(self, key, value):
+        print(self.sources.keys)
         if key not in self.sources:
             self.sources[key] = {
                 "status_counts": [],
@@ -62,7 +67,10 @@
                 "most_recent_image" : None,
                 "most_recent_mask" : None,
                 "most_recent_seg_iamge" : None,
-                "cctv_name" : value,
+                "cctv_info" : value,
+                "last_send_before" : 0,
+                "normal_to_failure_mode_change_alert" : False,
+                "failure_to_normal_mode_change_alert" : False
             }
         else : 
             raise KeyError(f"Error! Source {key} already initialized.")
@@ -71,42 +79,78 @@
     def __getitem__(self, key):
         return self.sources[key]
 
+    def __call__(self):
+        return self.sources
+
     def add_status(self, source, status):
         assert status in ["OK", "FAIL"], f"Invalid status was given!, status must be one of 'OK' or 'FAIL', but given '{status}'!"
         
         if source not in self.sources:
             raise ValueError(f"No key found for source. Did you forgot to add it? \n source : {source}")
 
-        self.sources[source]["status_counts"].append(status)
+        flag_send_event = False
+
+        status_value = 1 if status == "OK" else 0
+        self.sources[source]["status_counts"].append(status_value)
         if len(self.sources[source]["status_counts"]) > self.buffer_size:
             self.sources[source]["status_counts"].pop(0)
-        
-        # Your existing logic for updating counts and checking statuses
-        if status == 'OK':
-            self.sources[source]["ok_counts"] += 1
-            if self.sources[source]["force_send_mode"] and self.sources[source]["ok_counts"] >= self.normal_mode_thres:
+
+
+        if self.sources[source]["force_send_mode"]:
+            seek_n_recent_memory = min(len(self.sources[source]["status_counts"]), self.failure_mode_check_past_n)
+            failure_counts = (self.failure_mode_check_past_n
+                              - sum(self.sources[source]["status_counts"][seek_n_recent_memory]))
+            ok_counts = self.failure_mode_check_past_n - failure_counts
+            flag_send_event = True
+
+            # mode switching condition check
+            if ok_counts >= self.normal_mode_thres:
                 self.sources[source]["force_send_mode"] = False
-                self.send_message(source, "NORMAL SEND")
+                flag_send_event = False
+                self.sources[source]["failure_to_normal_mode_change_alert"] = True
+
+
         else:
-            self.sources[source]["ok_counts"] = 0  # Reset on FAIL
-            self.check_failures(source)
+            seek_n_recent_memory = min(len(self.sources[source]["status_counts"]), self.normal_mode_check_past_n)
+            failure_counts = (self.normal_mode_check_past_n
+                              - sum(self.sources[source]["status_counts"][:seek_n_recent_memory]))
+            # ok_counts = self.normal_mode_check_past_n - failure_counts
 
-    def check_failures(self, source):
-        if self.switching_fail_consecutive_mode:
-            if (len(self.sources[source]["status_counts"]) >= self.failure_mode_thres
-                    and all(status == 'FAIL' for status in self.sources[source]["status_counts"][-self.failure_mode_thres:])):
-                print(f"Source {source} has 5 consecutive FAILs!")
+            # mode switching condition check
+            if failure_counts >= self.normal_mode_thres:
                 self.sources[source]["force_send_mode"] = True
-                self.send_message(source, "FORCE SEND")
-        else :
-            pass
+                flag_send_event = True
+                self.sources[source]["normal_to_failure_mode_change_alert"] = True
 
-    def send_message(self, source, message_type):
-        print(f"Sending message for {source} - Status: {message_type}")
-        # Reset the count after sending message
-        self.sources[source]["ok_counts"] = 0
+            if self.sources[source]["last_send_before"] > self.normal_send_interval:
+                flag_send_event =True
+            else :
+                self.sources[source]["last_send_before"] += 1
+
+        if flag_send_event:
+            self.send_event(source)
+
+        # alert only alarms once
+        if self.sources[source]["failure_to_normal_mode_change_alert"]:
+            self.sources[source]["failure_to_normal_mode_change_alert"] = False
+
+        if self.sources[source]["normal_to_failure_mode_change_alert"]:
+            self.sources[source]["normal_to_failure_mode_change_alert"] = False
+
+    def send_event(self, source):
+        self.sources[source]["last_send_before"] = 0
+        print(f"EVENT : SENDING {source}!!")
+        pass
 
 
+memory = StreamSources(
+            buffer_size=15,
+            normal_send_interval=10,
+            failure_mode_thres=8,
+            failure_mode_check_past_n=12,
+            normal_mode_thres=8,
+            normal_mode_check_past_n=12,
+        )
 @ns.route('/postprocess', )
 class PostProcesser(Resource):
     def __init__(self, *args, **kargs):
@@ -123,14 +167,6 @@
         self.seg_image = None
         self.area_percent = 0
         self.detected = False
-        self.memory = StreamSources(
-            buffer_size=15,
-            normal_send_interval=10,
-            failure_mode_thres=8,
-            failure_mode_check_past_n=12,
-            normal_mode_thres=8,
-            normal_mode_check_past_n=12,
-        )
         pass
 
     @ns.response(200, 'Success')
@@ -160,12 +196,12 @@
             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'")
+            if debug:
+                self.image.save(f"network_test/image_p{time.time()}.png")
+                if self.detected :
+                    self.mask.save(f"network_test/mask_p{time.time()}.png")
+                    self.seg_image.save(f"network_test/seg_p{time.time()}.png")
 
             self.time_sent = time.time()
 
@@ -178,10 +214,15 @@
                 'seg_frame': self.seg_image,
                 'time_sent': self.time_sent
             }
-
-            self.memory[self.cctv_info['cctv_name']] = self.cctv_info
+            # if self.cctv_name in memory:
+            try :
+                memory[self.cctv_info['cctv_name']] = self.cctv_info
+            except :
+                pass
             pass_fail = self.pass_fail()
-            self.memory.add_status(self.cctv_name, pass_fail)
+            memory.add_status(self.cctv_name, pass_fail)
+            if debug:
+                print(memory())
 
         except ValueError as e:
             print(e)
yoloseg/inference_.py
--- yoloseg/inference_.py
+++ yoloseg/inference_.py
@@ -12,7 +12,7 @@
         self.cuda_enabled = run_with_cuda
         self.letter_box_for_square = True
         self.model_score_threshold = 0.3
-        self.model_nms_threshold = 0.5
+        self.model_nms_threshold = 0.6
         self.classes = []
 
         self.load_onnx_network()
@@ -222,7 +222,8 @@
     # Path to your ONNX model and classes text file
     model_path = 'yoloseg/weight/best.onnx'
     classes_txt_file = 'config_files/yolo_config.txt'
-    image_path = 'yoloseg/img3.jpg'
+    # image_path = 'yoloseg/img3.jpg'
+    image_path = 'testing.png'
 
     model_input_shape = (640, 640)
     inference_engine = Inference(
@@ -261,7 +262,7 @@
 
     # If you also want to display segmentation maps, you would need additional handling here
     # Example for displaying first mask if available:
-    if mask_maps is not None:
+    if len(mask_maps) != 0:
 
         seg_image = overlay_mask(img, mask_maps[:,:,0], color=(0, 255, 0), alpha=0.3)
         cv2.imshow("segmentation", seg_image)
Add a comment
List