윤영준 윤영준 05-22
minor code cleanup and added comments
@13a770493a1883335f65d552e56bf59e6b8ca00d
hls_streaming/hls.py
--- hls_streaming/hls.py
+++ hls_streaming/hls.py
@@ -9,6 +9,7 @@
 from datetime import datetime
 from threading import Lock, Thread, Event
 
+DEBUG = False
 
 
 class FrameCapturer:
@@ -38,7 +39,7 @@
         self.buffer_size = buffer_size
         self.frame_buffer = []
         self.current_frame = []
-        self.frame_buffer_lock = Lock() # for no memory sharing between receive_stream_packet and process_frames
+        self.frame_buffer_lock = Lock()  # for no memory sharing between receive_stream_packet and process_frames
         self.captured_frame_count = 0
         self.last_capture_time = 0
         self.start_time = time.time()
@@ -59,7 +60,6 @@
     def __call__(self, *args, **kwargs):
         return self.current_frame
 
-
     # ```receive_stream_packet``` and ```process_frames``` work asynchronously (called with Thread)
     #  so that it always run as intended (for every '''interval''' sec, send a photo)
     #  regardless of how you buffer frames as long as there are enough buffer.
@@ -71,6 +71,9 @@
                     self.frame_buffer.append(frame)
                 time.sleep(self.capture_interval)
 
+    #TODO Although there is no problems with how it works and how it was intended, because there seems to be an
+    # unexpected behaivor on "if current_time - self.start_time >= self.buffer_duration:" line,
+    # more inspection should be done
     def process_frames(self):
         while not self.stop_event.is_set():
             current_time = time.time()
@@ -88,10 +91,13 @@
                             img_binary = cv2.imencode('.png', self.current_frame)
                             img_binary = img_binary[1].tobytes()
                             self.send_image_to_server(img_binary)
-                            cv2.imwrite(f'hls_streaming/captured_frame_/{self.cctvid}_{datetime.now()}_{frame_name}', self.current_frame)
+                            if DEBUG:
+                                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")
-                            self.captured_frame_count +=1
+                            self.captured_frame_count += 1
 
             time.sleep(0.1)
 
@@ -100,20 +106,20 @@
         header = {
             'Content-Type': f'image/{image_type}',
             'x-time-sent': time_sent,
-            # Why are you encoding string? because post method has a problem with sending not english chars.
+            # Why are you encoding string? because post method has a problem with sending non-english chars.
             # (It defaults to latin-1 encoding and not straight forward to change)
             'x-cctv-name': base64.b64encode(str(self.cctvid).encode('utf-8')).decode('ascii'),
-            'x-cctv-latitude' : str(self.lat),
-            'x-cctv-longitude' : str(self.lon),
+            'x-cctv-latitude': str(self.lat),
+            'x-cctv-longitude': str(self.lon),
         }
         session = requests.Session()
         try:
             multipart_data = MultipartEncoder(
-                fields = {
+                fields={
                     'file': (f'frame_{self.cctvid}.{image_type}',
-                                  image,
-                                  f'image/{image_type}')
-                        }
+                             image,
+                             f'image/{image_type}')
+                }
             )
             header["Content-Type"] = multipart_data.content_type
             response = session.post(self.endpoint, headers=header, data=multipart_data)
@@ -151,5 +157,5 @@
         del capturer
         t2 = time.time()
         with open("result.txt", "w") as file:
-            file.write(f'{t2-t1} seconds before terminating')
+            file.write(f'{t2 - t1} seconds before terminating')
         exit()
run_image_anal_backend.sh
--- run_image_anal_backend.sh
+++ run_image_anal_backend.sh
@@ -20,8 +20,30 @@
 cleanup() {
     echo "Terminating all processes..."
     for pid in "${pids[@]}"; do
+        # Attempt to terminate the process
         kill -9 $pid
+
+        # Wait a bit for the process to terminate
+        sleep 1
+
+        # Check if the process is still running
+        if kill -0 $pid 2>/dev/null; then
+            echo "Process $pid did not terminate, trying again..."
+            kill -9 $pid
+            sleep 1  # Wait a bit before checking again
+
+            # Final check - if it still didn't terminate, report failure
+            if kill -0 $pid 2>/dev/null; then
+                echo "Failed to terminate process $pid."
+            else
+                echo "Process $pid terminated successfully after retry."
+            fi
+        else
+            echo "Process $pid terminated successfully."
+        fi
     done
+    echo "Exiting script."
+    exit 0  # Exit the script
 }
 
 # Trap keyboard interrupt and call cleanup
streaming_process.py
--- streaming_process.py
+++ streaming_process.py
@@ -20,10 +20,22 @@
         return api_result.loc[0]["cctvurl"]
 
     args = argparse.ArgumentParser()
-    args.add_argument("--cctv_num", type=int, help="index number of cctv, see cctv_list.csv")
+    args.add_argument("--cctv_num", type=int,
+                      help="Index number of cctv, to view or edit the list of cctv, see cctv_list.csv")
+    args.add_argument("--interval", type=float, default=5,
+                      help="Interval of frame extract of the process, unit is second, default is 5 seconds")
+    args.add_argument("--video_buffer", type=float, default=15,
+                      help="How may seconds of video frame buffer will be there, "
+                           "default value is 15 seconds, which matches ITS video streaming packet response interval")
+
     args = args.parse_args()
 
+    # This is a must, because if you refer to variables inside args directly,
+    #  when ERROR happens out of that variables or related, python can not
+    #  direct you to where it happens and instead unintelligible garbage
     cctv_ind = args.cctv_num
+    frame_extract_interval = args.interval
+    video_buffer = args.video_buffer
 
     cctv_info = pd.read_csv("config_files/cctv_list.csv")
     cctv_info = cctv_info.iloc[cctv_ind]
@@ -41,7 +53,7 @@
 
         cctv_url = refresh_hls_address(lat, lon, )
         print(name)
-        hls_streaming = FrameCapturer(cctv_url, name, lat, lon, 5, 15, 300, "Asia/Seoul",
+        hls_streaming = FrameCapturer(cctv_url, name, lat, lon, frame_extract_interval, video_buffer, 300, "Asia/Seoul",
                                       endpoint= "http://localhost:12345/cctv/infer")
         hls_streaming.start()
 
yoloseg/inference_.py
--- yoloseg/inference_.py
+++ yoloseg/inference_.py
@@ -142,6 +142,8 @@
         all_mask = full_masks.sum(axis=0)
         all_mask = np.clip(all_mask, 0, 1)
         # Append a dimension so that cv2 can understand ```all_mask``` argument as an image.
+        # This is because for this particular application, there is only single class ```water_body```
+        # However, if that is not the case, you must modify this part.
         all_mask = all_mask.reshape((image_shape[0], image_shape[1], 1))
         return all_mask.astype(np.uint8)
 
@@ -200,6 +202,9 @@
     Returns:
         np.ndarray: The image_binary with the overlay.
     """
+    assert alpha <= 1 and 0 <= alpha, (f"Error! invalid alpha value, it must be float, inbetween including 0 to 1, "
+                                       f"\n given alpha : {alpha}")
+
     # Ensure the mask is a binary mask
     mask = (mask > 0).astype(np.uint8)  # Convert mask to binary if not already
 
Add a comment
List