윤영준 윤영준 05-21
fixed a critical bug
: OUTPUT mask의 dimension의 감지된 bbox 수 만큼 많아지는 문제
@e379e608a9884498ed3ec514af26c72d735fe4ee
inference_endpoint.py
--- inference_endpoint.py
+++ inference_endpoint.py
@@ -9,7 +9,7 @@
 import base64
 import requests
 from requests_toolbelt import MultipartEncoder
-from config_files import API_ENDPOINT_MAIN
+# from config_files import API_ENDPOINT_MAIN
 
 app = Flask(__name__)
 api = Api(app, version='1.0', title='CCTV Image Upload API',
@@ -47,8 +47,8 @@
 @ns.route('/infer', )
 class ImageUpload(Resource):
     # @ns.expect(image_upload_model, validate=True)
-    def __init__(self):
-        super().__init__(api)
+    def __init__(self, *args, **kargs):
+        super().__init__(*args, **kargs)
         self.time_sent = None
         self.cctv_latitude = None
         self.cctv_longitude = None
@@ -78,12 +78,15 @@
         # filename = f"{timestamp}_{self.cctv_info}.png"
 
         t1 = time.time()
-        detections, self.mask = inference_engine.run_inference(image)
-        self.mask_blob = cv2.imencode(self.mask)
-        self.mask_blob = self.mask.tobytes()
+        detections, self.mask = inference_engine.run_inference(cv2.resize(image, model_input_shape))
         t2 = time.time()
+        if len(self.mask) > 0:
+            print(self.mask.shape)
+            self.mask_blob = cv2.imencode('.png', self.mask)
+            self.mask_blob = self.mask.tobytes()
 
-        print(t2 - t1)
+
+        # print(t2 - t1)
 
         if len(self.mask) != 0:
             seg_image = overlay_mask(image, self.mask[0], color=(0, 255, 0), alpha=0.3)
@@ -91,7 +94,9 @@
         else :
             self.area_percent = np.sum(self.mask) / image.shape[0] * image.shape[1]
 
-        self.send_result()
+
+
+        # self.send_result()
         # write another post request for pushing a detection result
         return {"message": f"Image {self.mask} uploaded successfully!"}
 
 
postprocess_draft_pingpong.py (added)
+++ postprocess_draft_pingpong.py
@@ -0,0 +1,78 @@
+from flask import Flask, request, jsonify
+
+app = Flask(__name__)
+
+# Dictionary to store the status counts for each source
+status_counts = {
+    "source1": [],
+    "source2": [],
+    "source3": [],
+    "source4": [],
+    "source5": []
+}
+
+# Dictionary to keep track of OK counts for each source
+ok_counts = {
+    "source1": 0,
+    "source2": 0,
+    "source3": 0,
+    "source4": 0,
+    "source5": 0
+}
+
+# Dictionary to track if a source is in force send mode
+force_send_mode = {
+    "source1": False,
+    "source2": False,
+    "source3": False,
+    "source4": False,
+    "source5": False
+}
+
+
+# Function to check if there are 5 consecutive failures
+def check_consecutive_failures(source, status_list):
+    if len(status_list) >= 5 and status_list[-5:] == ['FAIL'] * 5:
+        print(f"Source {source} has 5 consecutive FAILs!")
+        force_send_mode[source] = True
+        send_message(source, force_send=True)
+
+
+# Function to send a message
+def send_message(source, force_send=False):
+    if force_send or ok_counts[source] >= 10:
+        print(f"Sending message for {source} - Status: {'FORCE SEND' if force_send else 'OK SEND'}")
+        ok_counts[source] = 0  # Reset counter after sending
+
+
+# Route to receive data from sources
[email protected]('/status', methods=['POST'])
+def receive_status():
+    data = request.json
+    source = data.get('source')
+    status = data.get('status')
+
+    if source in status_counts:
+        status_counts[source].append(status)
+        # Ensure we keep only the latest 5 statuses to check for consecutive FAILs
+        if len(status_counts[source]) > 5:
+            status_counts[source].pop(0)
+
+        if status == 'OK':
+            ok_counts[source] += 1
+            if force_send_mode[source] and ok_counts[source] >= 10:
+                # Revert to normal mode after 10 consecutive OKs
+                force_send_mode[source] = False
+                ok_counts[source] = 0
+            elif not force_send_mode[source]:
+                send_message(source)
+        else:
+            ok_counts[source] = 0  # Reset counter on FAIL
+
+        check_consecutive_failures(source, status_counts[source])
+
+    return jsonify({"message": "Status received"}), 200
+
+
+if __name__ == '__main__':
+    app.run(debug=True, port=5000)
 
postprocess_draft_test.py (added)
+++ postprocess_draft_test.py
@@ -0,0 +1,65 @@
+import requests
+import time
+
+# Base URL for the server
+BASE_URL = 'http://localhost:5000/status'
+
+# Function to send a POST request with status data
+def send_status(source, status):
+    response = requests.post(BASE_URL, json={"source": source, "status": status})
+    print(f"Sent {status} for {source}, Response: {response.json()}")
+
+# Function to test normal and force send modes for multiple sources
+def test_server():
+    sources = ['source1', 'source2', 'source3', 'source4', 'source5']
+
+    # Scenario 1: Normal behavior with source1
+    print("\nScenario 1: Normal behavior with source1")
+    for _ in range(4):
+        send_status('source1', 'OK')
+    for _ in range(5):
+        send_status('source1', 'FAIL')
+    for _ in range(10):
+        send_status('source1', 'OK')
+    for _ in range(10):
+        send_status('source1', 'OK')
+
+    # Scenario 2: Force send and revert with source2
+    print("\nScenario 2: Force send and revert with source2")
+    for _ in range(5):
+        send_status('source2', 'FAIL')
+    for _ in range(10):
+        send_status('source2', 'OK')
+    for _ in range(10):
+        send_status('source2', 'OK')
+
+    # Scenario 3: Random mix with source3
+    print("\nScenario 3: Random mix with source3")
+    mix = ['OK', 'FAIL', 'OK', 'FAIL', 'OK', 'FAIL', 'OK', 'OK', 'OK', 'OK']
+    for status in mix:
+        send_status('source3', status)
+    for _ in range(5):
+        send_status('source3', 'FAIL')
+    for _ in range(10):
+        send_status('source3', 'OK')
+
+    # Scenario 4: Source4 fails and recovers
+    print("\nScenario 4: Source4 fails and recovers")
+    for _ in range(5):
+        send_status('source4', 'FAIL')
+    for _ in range(10):
+        send_status('source4', 'OK')
+    for _ in range(10):
+        send_status('source4', 'OK')
+
+    # Scenario 5: Source5 normal operation
+    print("\nScenario 5: Source5 normal operation")
+    for _ in range(10):
+        send_status('source5', 'OK')
+    for _ in range(10):
+        send_status('source5', 'OK')
+
+if __name__ == '__main__':
+    # Give some time for the server to start
+    time.sleep(2)
+    test_server()
postprocessing.py
--- postprocessing.py
+++ postprocessing.py
@@ -40,5 +40,5 @@
         self.image_type = request.headers.get('Content-Type')
         self.area_percent = request.headers.get('x-area-percentage')
 
-
-
+    async def data_ping_pong(self):
+        pass
run_image_anal_backend.sh
--- run_image_anal_backend.sh
+++ run_image_anal_backend.sh
@@ -11,7 +11,7 @@
 python streaming_process.py --cctv_num 1 &
 pids+=($!)
 
-
+#python test.py
 python inference_endpoint.py
 #gunicorn --workers=6 inference_endpoint:app
 pids+=($!)
yoloseg/inference_.py
--- yoloseg/inference_.py
+++ yoloseg/inference_.py
@@ -139,14 +139,17 @@
 
             # Place the mask in the corresponding location on a full-sized mask image_binary
             full_mask = np.zeros((image_shape[0], image_shape[1]), dtype=np.uint8)
-            print(final_mask.shape)
-            print(full_mask[y1:y2, x1:x2].shape)
+            # print("---------")
+            # print(f"x1 : {x1}, y1 : {y1}, w: {w}, h: {h}")
+            # print(f"x2: {x2}, y2 : {y2}")
+            # print(final_mask.shape)
+            # print(full_mask[y1:y2, x1:x2].shape)
             full_mask[y1:y2, x1:x2] = final_mask
 
             # Combine the mask with the masks of other detections
             full_masks[idx] = full_mask
-
-        return full_masks
+        all_mask = full_masks.sum(axis=0)
+        return all_mask
 
     def load_classes_from_file(self):
         with open(self.classes_path, 'r') as f:
Add a comment
List