윤영준 윤영준 05-21
추론 서버 코드 작성
@6e76cad9442bcff0c1da5a92c1f42df9dfea3a68
hls_streaming/hls.py
--- hls_streaming/hls.py
+++ hls_streaming/hls.py
@@ -90,7 +90,7 @@
                             self.send_image_to_server(img_binary)
                             cv2.imwrite(f'hls_streaming/captured_frame_/{self.cctvid}_{datetime.now()}_{frame_name}', self.current_frame)
                             self.last_capture_time = current_time
-                            print(f"Captured {frame_name} of {self.cctvid} at time: {current_time - self.start_time:.2f}s")
+                            # print(f"Captured {frame_name} of {self.cctvid} at time: {current_time - self.start_time:.2f}s")
                             self.captured_frame_count +=1
 
             time.sleep(0.1)
inference_endpoint.py
--- inference_endpoint.py
+++ inference_endpoint.py
@@ -69,8 +69,15 @@
 
         if len(mask_maps) != 0:
             seg_image = overlay_mask(image, mask_maps[0], color=(0, 255, 0), alpha=0.3)
+            area_percent = 0
+        else :
+            area_percent = np.sum(mask_maps) / image.shape[0] * image.shape[1]
 
+        # write another post request for pushing a detection result
         return {"message": f"Image {filename} uploaded successfully!"}
 
+    def send_result(self):
+        pass
+
 if __name__ == '__main__':
     app.run(debug=True, port=12345)
 
postprocessing.py (added)
+++ postprocessing.py
@@ -0,0 +1,17 @@
+import numpy as np
+from flask import Flask, request
+from flask_restx import Api, Resource, fields
+import os
+from datetime import datetime
+from yoloseg.inference_ import Inference, overlay_mask
+import cv2
+import time
+import base64
+
+
+app = Flask(__name__)
+api = Api(app, version='1.0', title='Detection postprocessing component',
+          description='A postprocessing that dynamically decides how frequently send image and detection result')
+
+# Namespace definition
+ns = api.namespace('cctv', description='CCTV operations')(파일 끝에 줄바꿈 문자 없음)
run_image_anal_backend.sh
--- run_image_anal_backend.sh
+++ run_image_anal_backend.sh
@@ -11,7 +11,9 @@
 python streaming_process.py --cctv_num 1 &
 pids+=($!)
 
-python test.py
+
+python inference_endpoint.py
+#gunicorn --workers=6 inference_endpoint:app
 pids+=($!)
 
 # Function to kill all processes
yoloseg/inference_.py
--- yoloseg/inference_.py
+++ yoloseg/inference_.py
@@ -58,8 +58,13 @@
             confidence = scores_classification[class_id]
 
             thres = self.model_score_threshold
+            w_thres = 20
+            h_thres = 20
             if confidence > thres:
                 x, y, w, h = detection[:4]
+                # if bboxes are too small, it just skips, and it is not a bad idea since we do not need to detect small areas
+                if w < w_thres or h < h_thres:
+                    continue
                 left = int((x - 0.5 * w) * x_factor)
                 top = int((y - 0.5 * h) * y_factor)
                 width = int(w * x_factor)
@@ -97,19 +102,25 @@
         for idx, det in enumerate(detections):
             box = det['box']
             x1, y1, w, h = box
+            print(f"x1 : {x1}, y1 : {y1}, w: {w}, h: {h}")
+
+            x1, y1, x2, y2 = x1, y1, x1 + w, y1 + h
 
             #... why the model outputs ... negative values?...
             if x1 <= 0 :
+                w += x1
                 x1 = 0
             if y1 <= 0 :
+                h += y1
                 y1 = 0
-            x1, y1, x2, y2 = x1, y1, x1 + w, y1 + h
 
-            # To handle edge cases where you get bboxes that pass beyond the original image_binary
+            # To handle edge cases where you get bboxes that pass beyond the original size of image_binary
             if y2 > image_shape[1]:
-                h = h + image_shape[1] - h - y1
+                h = image_shape[1] - y1
             if x2 > image_shape[0]:
-                w = w + image_shape[1] - w - y1
+                w = image_shape[1] - y1
+
+            print(f"x2: {x2}, y2 : {y2}")
 
             # Get the corresponding mask coefficients for this detection
             coeffs = det["mask_coefficients"]
Add a comment
List