윤영준 윤영준 05-30
Feature : * Uploading results to the server
* Uploading results to the server
@33dd35952df8d58c0634c343f9834fd74e31a0fc
ITS/api.py
--- ITS/api.py
+++ ITS/api.py
@@ -80,7 +80,7 @@
                 # df.to_csv(f"result/pohang/listofcctv_포항_{x}_{y}.csv", index=False)
             time.sleep(1)
             print(f"{i}, {j}")
-    # all_data_df.to_csv(f"result/{xmin}_{xmax}_y{ymin}_{ymax}.csv")
+    all_data_df.to_csv(f"result/{xmin}_{xmax}_y{ymin}_{ymax}.csv")
     return all_data_df
 
 def get_jpeg(url):
@@ -92,6 +92,6 @@
 
 
 if __name__ == "__main__":
-    df = gather_cctv_list(129.2, 129.3, 35.9, 36.07, 1, "its", 1)
+    df = gather_cctv_list(127.9, 128.3, 35.9, 36.07, 1, "its", 1)
     pass
     # get_jpeg("http://cctvsec.ktict.co.kr:8090/74236/IM2NQs4/uHZcgnvJo3V/mjo3tswwgUj87kpcYZfR/BPxaQ4lk9agnl8ARIB9lhlgOD87VBx6RDHFl423kLkqHQ==")
(파일 끝에 줄바꿈 문자 없음)
ITS_api_test.py
--- ITS_api_test.py
+++ ITS_api_test.py
@@ -8,7 +8,7 @@
 lat = df.loc[0]["coordx"]
 lon = df.loc[0]["coordy"]
 apiKey = os.getenv("ITS_API")
-lat_lon_interval = 0.000001
+lat_lon_interval = 0.00001
 api_result = gather_cctv_list(xmin=lat-lat_lon_interval, ymin=lon-lat_lon_interval, xmax=lat+lat_lon_interval, ymax=lon+lat_lon_interval, intervals=1, roadType="its", cctvType=1)
 
 name_of_requested = api_result.loc[0]["cctvname"]
config_files/cctv_list.csv
--- config_files/cctv_list.csv
+++ config_files/cctv_list.csv
@@ -1,3 +1,5 @@
-,roadsectionid,coordx,coordy,cctvresolution,filecreatetime,cctvtype,cctvformat,cctvname,cctvurl
-0,,129.245312,35.920346,,,1,HLS,[국도 7호선] 모아초교,http://cctvsec.ktict.co.kr/4463/6SqphQj32sxeB2nYIMerulhkk0HdDKP/aSEgp7hHbUY9iwTDb+UQZfTbag9pXmM/yo3a7l1usm64GwBH77/SCVEel3JF3g9BZDc+ws1es2w=
-1,,129.2005,35.921825,,,1,HLS,[국도20호선] 검단산업단지,http://cctvsec.ktict.co.kr/71187/bWDrL7fpStZDeDZgCybpJH8gagWJOynbaA/l91ExpmUPKzc3bCsHJtIblDkzG3TfmkwkLj7+PPdYAHSYBXxem4SZJpaAYFU0CtDtr5rz7DY=
(파일 끝에 줄바꿈 문자 없음)
+roadsectionid,coordx,coordy,cctvresolution,filecreatetime,cctvtype,cctvformat,cctvname,cctvurl
+,129.245312,35.920346,,,1,HLS,[국도 7호선] 모아초교,http://cctvsec.ktict.co.kr/4463/6SqphQj32sxeB2nYIMerulhkk0HdDKP/aSEgp7hHbUY9iwTDb+UQZfTbag9pXmM/yo3a7l1usm64GwBH77/SCVEel3JF3g9BZDc+ws1es2w=
+,129.2005,35.921825,,,1,HLS,[국도20호선] 검단산업단지,http://cctvsec.ktict.co.kr/71187/bWDrL7fpStZDeDZgCybpJH8gagWJOynbaA/l91ExpmUPKzc3bCsHJtIblDkzG3TfmkwkLj7+PPdYAHSYBXxem4SZJpaAYFU0CtDtr5rz7DY=
+,128.06396,36.047108,,,1,HLS,[국도3호선] 송죽교차로,http://cctvsec.ktict.co.kr/71208/Z9Acztz6CYwzuUXQFreRR0Kdewba8Ml4CNxR0Jsp9uSCWmvL9W/BO1w2XIPxtxs9kn58qVvtHREs6vU305JaRa40F1m/P6f13gpiz9C+46M=
+,128.2671,35.91457,,,1,HLS,[국도33호선] 대흥교차로,http://cctvsec.ktict.co.kr/72593/mTZfGWopuLSKGZsBABx2whdCW4FLmBl6wi4dCvp0MxMpKF6c6ydtf40G8dVXQcAGlDug1DIE/E4MVUyoa3UuD18aYxP6ZCMD5zISEiDxXTQ=
(파일 끝에 줄바꿈 문자 없음)
inference_endpoint.py
--- inference_endpoint.py
+++ inference_endpoint.py
@@ -1,3 +1,5 @@
+import copy
+
 import numpy as np
 import os
 import cv2
@@ -83,15 +85,15 @@
 
         # timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
 
-        image = self.image.read()
+        self.image = self.image.read()
         # self.image.seek(0)
-        self.image_binary = image
-        image = np.frombuffer(image, np.uint8)
-        image = cv2.imdecode(image, cv2.IMREAD_COLOR)
+        self.image_binary = copy.deepcopy(self.image)
+        self.image = np.frombuffer(self.image, np.uint8)
+        self.image = cv2.imdecode(self.image, cv2.IMREAD_COLOR)
         # filename = f"{timestamp}_{self.cctv_name}.png"
 
         t1 = time.time()
-        detections, self.mask = inference_engine.run_inference(cv2.resize(image, model_input_shape))
+        detections, self.mask = inference_engine.run_inference(cv2.resize(self.image, model_input_shape))
         t2 = time.time()
         print(t2 - t1)
 
@@ -103,13 +105,13 @@
         if self.flag_detected:
             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 = cv2.resize(self.mask, (self.image.shape[1], self.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)
-            self.area_percent = np.sum(self.mask) / image.shape[0] * image.shape[1]
+            self.seg_image = overlay_mask(self.image, self.mask[:,:,0], color=(0, 255, 0), alpha=0.3)
+            self.area_percent = np.sum(self.mask) / self.image.shape[0] * self.image.shape[1]
         else :
             self.area_percent = 0
 
@@ -131,7 +133,8 @@
             'X-Flag-Detected' : str(self.flag_detected)  #"True" or "False"
         }
         session = requests.Session()
-
+        image_binary = cv2.imencode('.png', self.image)
+        image_binary = image_binary[1].tobytes()
         try:
             if self.flag_detected:
                 seg_binary = cv2.imencode('.png', self.seg_image)
@@ -140,14 +143,14 @@
                     fields={
                         'image': (
                             f'frame_{self.cctv_name}.{self.image_type}',
-                            self.image_binary,
+                            image_binary,
                             f'image/{self.image_type}'
                         ),
-                        'mask' : (
-                            f'frame_mask_{self.cctv_name}.{self.image_type}',
-                            self.mask_blob,
-                            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,
@@ -163,14 +166,18 @@
                     fields={
                         'image': (
                             f'frame_{self.cctv_name}.{self.image_type}',
-                            self.image_binary,
+                            image_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)
+                # def print_request(req):
+                #     from requests_toolbelt.utils import dump
+                #     data = dump.dump_all(req)
+                #     print(data.decode('utf-8'))
+                # print_request(response)
 
         except Exception as e:
             print(e)
postprocess_draft.py
--- postprocess_draft.py
+++ postprocess_draft.py
@@ -1,7 +1,9 @@
+import random
+
 import numpy as np
 from flask import Flask, request, jsonify
 from flask_restx import Api, Resource, fields
-import os
+import datetime
 import psycopg2
 import time
 import base64
@@ -97,23 +99,23 @@
     def __call__(self):
         return self.sources
 
-    def add_status(self, source, status, cctv_info, image, seg_image):
-        assert status in ["OK", "FAIL"],\
-            f"Invalid status was given!, status must be one of 'OK' or 'FAIL', but given '{status}'!"
-        
+    def add_status(self, source, status, cctv_info):
+        assert status in ["N", "Y"],\
+            f"Invalid status was given!, status must be one of 'N'(== 'OK') or 'Y'(==== '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}")
 
         flag_send_event = False
 
-        status_value = 1 if status == "OK" else 0
+        status_value = 1 if status == "N" else 0
 
         self.cache_cctv_info = cctv_info
 
         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)
-        print(len(self.sources[source]["status_counts"]))
+        # print(len(self.sources[source]["status_counts"]))
 
         if self.sources[source]["force_send_mode"]:
             seek_n_recent_memory = min(len(self.sources[source]["status_counts"]), self.failure_mode_check_past_n)
@@ -143,15 +145,15 @@
                 self.sources[source]["normal_to_failure_mode_change_alert"] = True
 
             # regular interval message logic
-            if self.sources[source]["last_send_before"] > self.normal_send_interval:
+            if self.sources[source]["last_send_before"] >= self.normal_send_interval:
                 flag_send_event =True
             else :
                 self.sources[source]["last_send_before"] += 1
 
         print(f"ok_counts : {self.sources[source]['ok_counts']}")
+        print(f"last_send_before : {self.sources[source]['last_send_before']}")
+
         if flag_send_event:
-            self.sources[source]["most_recent_image"] = image
-            self.sources[source]["most_recent_seg_image"] = seg_image
             self.send_event(source)
 
         # alert alarms only once
@@ -164,57 +166,57 @@
     def send_event(self, source):
         print(f"{source} is now sending data!")
         source_data = self.sources[source]
-        try:
-            # Connect to the database
-            conn = psycopg2.connect(**db_config)
-            cursor = conn.cursor()
 
-            # Set the search path for the schema
-            cursor.execute("SET search_path TO ai_camera_v0_1;")
+        # Connect to the database
+        conn = psycopg2.connect(**db_config)
+        cursor = conn.cursor()
 
-            # Prepare the SQL query
-            insert_sql = """
-            INSERT INTO flooding_detect_event (
-                ocrn_dt, 
-                eqpmn_nm, 
-                flooding_result, 
-                flooding_per, 
-                image, 
-                image_seg, 
-                eqpmn_lat, 
-                eqpmn_lon, 
-                norm_to_alert_flag, 
-                alert_to_norm_flag
-            ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
-            """
+        # Set the search path for the schema
+        cursor.execute("SET search_path TO ai_camera_v0_1;")
 
-            # Prepare data to insert
-            data_tuple = (
-                time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(source_data["last_send_before"])),
-                source_data["cctv_info"]["cctv_name"],
-                "FAIL" if source_data["failure_counts"] >= self.failure_mode_thres else "OK",
-                source_data["cctv_info"]["area_percent"],
-                source_data["most_recent_image"],
-                source_data["most_recent_seg_image"],
-                source_data["cctv_info"]["cctv_latitude"],
-                source_data["cctv_info"]["cctv_longitude"],
-                source_data["normal_to_failure_mode_change_alert"],
-                source_data["failure_to_normal_mode_change_alert"]
-            )
+        # Prepare the SQL query
+        insert_sql = """
+        INSERT INTO flooding_detect_event (
+            ocrn_dt, 
+            eqpmn_nm, 
+            flooding_result, 
+            flooding_per, 
+            image, 
+            image_seg, 
+            eqpmn_lat, 
+            eqpmn_lon, 
+            norm_to_alert_flag, 
+            alert_to_norm_flag
+        ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
+        """
+        # Prepare data to insert
+        print(self.cache_cctv_info["time_sent"])
+        data_tuple = (
+            self.cache_cctv_info["time_sent"],
+            source_data["cctv_info"]["cctv_name"],
+            self.cache_cctv_info["detected"],
+            self.cache_cctv_info["area_percent"],
+            self.cache_cctv_info["source_frame"],
+            self.cache_cctv_info["seg_frame"],
+            source_data["cctv_info"]["cctv_latitude"],
+            source_data["cctv_info"]["cctv_longitude"],
+            "Y" if source_data["normal_to_failure_mode_change_alert"] else "N",
+            "Y" if source_data["failure_to_normal_mode_change_alert"] else "N",
+        )
+        # print(hash(self.cache_cctv_info["source_frame"]))
+        # print(self.cache_cctv_info["source_frame"][:100])
+        # print(self.cache_cctv_info["source_frame"][-100:])
 
-            # Execute the query
-            cursor.execute(insert_sql, data_tuple)
-            conn.commit()
+        # Execute the query
+        cursor.execute(insert_sql, data_tuple)
+        conn.commit()
 
-            print(f"EVENT: Sent for {source} - Data inserted successfully.")
+        print(f"EVENT: Sent for {source} - Data inserted successfully.")
 
-        except Exception as e:
-            print(f"Database operation failed: {e}")
-        finally:
-            if cursor:
-                cursor.close()
-            if conn:
-                conn.close()
+        if cursor:
+            cursor.close()
+        if conn:
+            conn.close()
 
         # Reset the image data after sending to avoid re-sending the same image
         source_data["most_recent_image"] = None
@@ -224,7 +226,7 @@
 
 memory = StreamSources(
             buffer_size=15,
-            normal_send_interval=10,
+            normal_send_interval=1,
             failure_mode_thres=8,
             failure_mode_check_past_n=12,
             normal_mode_thres=8,
@@ -237,7 +239,7 @@
     image = cv2.imdecode(image, cv2.IMREAD_COLOR)
     _, image = cv2.imencode('.jpg', image)
     image = image.tobytes()
-    image = base64.b64encode(image)
+    image = base64.b64encode(image).decode('utf-8')
     return image
 
 
@@ -262,53 +264,58 @@
     @ns.response(200, 'Success')
     @ns.response(400, 'Validation Error')
     def post(self):
+        # try:
+        # Gathering values
+        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.time_sent = datetime.datetime.strptime(self.time_sent, '%Y-%m-%dT%H:%M:%SZ')
+        # self.time_sent = self.time_sent.timestamp()
+        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"Invalid value for x-flag-detected: {self.detected}")
+
+        self.area_percent = request.headers.get('x-area-percentage')
         try:
-            # Gathering values
-            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.area_percent = float(self.area_percent)
+        except (TypeError, ValueError) as e:
+            raise ValueError(f"Invalid value for x-area-percentage: {self.area_percent}")
 
-            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}")
+        # gathering files
+        try:
+            self.image = request.files['image'].read()
+        except:
+            raise ValueError("Error reading 'image!'")
 
-            self.area_percent = request.headers.get('x-area-percentage')
+
+        if self.detected:
             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}")
-
-            # gathering files
-            try:
-                self.image = request.files.get('image')
+                self.mask = request.files['mask'].read()
+                self.seg_image = request.files['seg_image'].read()
             except:
-                raise ValueError("Error reading 'image!'")
+                raise ValueError("Error reading 'mask' and 'seg_mask'")
 
-            if self.detected:
-                try:
-                    self.mask = request.files.get('mask')
-                    self.seg_image = request.files.get('seg_mask')
-                except:
-                    raise ValueError("Error reading 'mask' and 'seg_mask'")
+        if debug:
+            pass
+            # 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")
 
+        image_b64 = get_base64_encoded_image_from_file_binary(self.image)
+        if self.detected:
+            seg_image_b64 = get_base64_encoded_image_from_file_binary(self.seg_image)
 
-            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")
+        pass_fail = self.pass_fail()
 
-            image_b64 = get_base64_encoded_image_from_file_binary(self.image)
-            seg_image_b64 = get_base64_encoded_image_from_file_binary(self.seg_image.read())
-
-            self.time_sent = time.time()
-
+        if self.detected:
             self.cctv_info = {
                 'cctv_name': self.cctv_name,
                 'cctv_latitude': self.cctv_latitude,
@@ -316,32 +323,51 @@
                 'source_frame': image_b64,
                 # 'frame_mask': self.mask,
                 'seg_frame': seg_image_b64,
-                'time_sent': self.time_sent
+                'time_sent': self.time_sent,
+                'area_percent' : self.area_percent,
+                'detected' : pass_fail
             }
-            # if self.cctv_name in memory:
-            try :
-                memory[self.cctv_info['cctv_name']] = self.cctv_info
-            except :
-                pass
-            pass_fail = self.pass_fail()
+        else :
+            self.cctv_info = {
+                'cctv_name': self.cctv_name,
+                'cctv_latitude': self.cctv_latitude,
+                'cctv_longitude': self.cctv_longitude,
+                'source_frame': image_b64,
+                # 'frame_mask': self.mask,
+                'seg_frame': None,
+                'time_sent': self.time_sent,
+                'area_percent': self.area_percent,
+                'detected': pass_fail
+            }
+        # if self.cctv_name in memory:
 
-            memory.add_status(self.cctv_name, pass_fail, self.cctv_info, image_b64, seg_image_b64)
+        try :
+            memory[self.cctv_info['cctv_name']] = self.cctv_info
+        except:
+            pass
 
-            if debug:
-                print(memory())
+        if self.detected:
+            memory.add_status(self.cctv_name, pass_fail, self.cctv_info)
+        else :
+            memory.add_status(self.cctv_name, pass_fail, self.cctv_info)
 
-        except ValueError as e:
-            print(e)
-        except Exception as e:
-            print(e)
+        # if debug:
+        #     print(memory())
+        #
+        # except ValueError as e:
+        #     print(e)
+        # except Exception as e:
+        #     print(e)
 
     def pass_fail(self):
         thres = 0.1
         #TODO temporal pass_fail threshold
         if self.area_percent > thres:
-            ret = 'FAIL'
+            # ret = 'FAIL'
+            ret = 'Y'
         else:
-            ret = 'OK'
+            # ret = 'OK'
+            ret = 'N'
         return ret
 
 
streaming_url_updator.py
--- streaming_url_updator.py
+++ streaming_url_updator.py
@@ -9,7 +9,7 @@
 from flask_cors import CORS
 from apscheduler.schedulers.background import BackgroundScheduler
 from apscheduler.triggers.interval import IntervalTrigger
-from requests_toolbelt.utils import dump
+
 
 API_ENDPOINT = "http://165.229.169.148:8080/EquipmentUrlChanger.json"
 
@@ -26,6 +26,7 @@
 
 
 def print_request(req):
+    from requests_toolbelt.utils import dump
     data = dump.dump_all(req)
     print(data.decode('utf-8'))
 
Add a comment
List