From eab0294f5bcd3b669b2cb200b727a501a54327ac Mon Sep 17 00:00:00 2001 From: bluepulsar Date: Thu, 25 Dec 2025 16:43:09 +0800 Subject: [PATCH] update parameter to json --- example2_config.py | 4 +--- src/orisys/sensor.py | 42 ++++++++++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/example2_config.py b/example2_config.py index 76971ea..1db59f2 100644 --- a/example2_config.py +++ b/example2_config.py @@ -9,9 +9,7 @@ def main(): global start_time # 打开视频 - sensor1 = Sensor("./s1.mp4",isstitch=True,cuda = True,verbose = True) - config = sensor1.get_config() - print(config) + sensor1 = Sensor("./s1.mp4",isstitch=True,cuda = True,verbose = True, config_name="prototype") # 打开摄像头 # sensor1 = Sensor(0,isstitch=True,cuda = True,verbose = True) diff --git a/src/orisys/sensor.py b/src/orisys/sensor.py index 5457263..b28f792 100644 --- a/src/orisys/sensor.py +++ b/src/orisys/sensor.py @@ -43,10 +43,13 @@ class Sensor: else: print("User must specify a video source.") self.vid_src, self.isstitch, self.cuda, self.verbose = vid_src, isstitch, cuda, verbose - self.index = np.array([1,2,3,6,7,8,11,12,13]) + self.config_name = config_name + # Initialize Sensor Configuration + self.get_config() + # Initialize components - self.__stitcher = Stitcher(isstitch=isstitch, index=self.index) + self.__stitcher = Stitcher(isstitch=isstitch, index=self.config_index) self.tracker = Tracker(adaptive=True, cuda=cuda) self.hhd = None @@ -56,9 +59,9 @@ class Sensor: # Setup video capture self.cap = cv2.VideoCapture(vid_src) self.cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G')) - self.cap.set(cv2.CAP_PROP_FPS, 30) - self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) - self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) + self.cap.set(cv2.CAP_PROP_FPS, self.config_fps) + self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.config_resolution_raw[0]) + self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.config_resolution_raw[1]) # Wait for first valid frame if self.verbose: @@ -86,8 +89,7 @@ class Sensor: self.img_filtered = None # For temporal noise reduction self.fnormal=self.fshearx=self.fsheary=self.ftor=0.0 self.imgupdate = self.vupdate = False - self.pos_default = [(260, 56), (577, 62), (897, 69), (1214, 75), (1530, 81), (256, 391), (570, 397), (889, 403), (1208, 409), (1523, 416), (248, 724), (563, 730), (882, 738), (1201, 743), (1516, 751)] - + # Logging state self.last_log_time = time.time() self.fps = 0 @@ -112,7 +114,19 @@ class Sensor: DEPTH = auto() def get_config(self): - return configs.load_config("prototype") + """ + Load and return sensor configuration from JSON file. + + Returns: + dict: Dictionary containing sensor configuration + """ + self.config = configs.load_config(self.config_name) + self.config_resolution_raw = self.config["camera"]["resolution_raw"] + self.config_fps = self.config["camera"]["fps"] + self.config_units = self.config["camera"]["units"] + self.config_resolution_stitch = self.config["camera"]["resolution_stitch"] + self.config_index = self.config["camera"]["index"] + self.config_pos_default = self.config["camera"]["pos_default"] def get_img(self): """ @@ -133,7 +147,7 @@ class Sensor: self.__get_tiles(gray_frame) img = np.ascontiguousarray(self.__stitcher.stitch_rt_optimized()) - img_resized = cv2.resize(img, (400, 400), interpolation=cv2.INTER_CUBIC) + img_resized = cv2.resize(img, (self.config_resolution_stitch[0], self.config_resolution_stitch[1]), interpolation=cv2.INTER_CUBIC) # Apply temporal noise reduction using exponential moving average # Blend current frame with previous filtered frame to reduce temporal noise @@ -280,7 +294,7 @@ class Sensor: start_time = cv2.getTickCount() # print("Waiting for camera stabilization...") - while n_boxes < 15: + while n_boxes < self.config_units: ret, frame = self.cap.read() v_mean = np.sum(cv2.mean(frame)) @@ -297,7 +311,7 @@ class Sensor: # print("Camera stabilized.") # print("Checking validabity for stitching...") self.n_boxes = n_boxes - self.stitch_valid = self.isstitch and n_boxes == 15 + self.stitch_valid = self.isstitch and n_boxes == self.config_units if self.isstitch: @@ -420,7 +434,7 @@ class Sensor: # Arrow rooted at original position (x_axis, y_axis), head points to new position (end_x, end_y) thickness = max(1, int(2 * magnitudes[i] / magnitudes.max())) if magnitudes.max() > 0 else 2 cv2.arrowedLine(image, (x_axis[i], y_axis[i]), (end_x[i], end_y[i]), color, thickness=thickness, tipLength=0.3) - out_img = cv2.resize(image, (400, 400), interpolation=cv2.INTER_CUBIC) + out_img = cv2.resize(image, (self.config_resolution_stitch[0], self.config_resolution_stitch[1]), interpolation=cv2.INTER_CUBIC) # if fps: # out_img = self.draw_fps(image=out_img) @@ -454,7 +468,7 @@ class Sensor: if flow_field is None: # Return blank image if no flow data - return np.zeros((400, 400, 3), dtype=np.uint8) + return np.zeros((self.config_resolution_stitch[0], self.config_resolution_stitch[1], 3), dtype=np.uint8) # Compute magnitude at each point magnitude = np.sqrt(flow_field[:, :, 0]**2 + flow_field[:, :, 1]**2) @@ -480,7 +494,7 @@ class Sensor: magnitude_colored = cv2.applyColorMap(magnitude_norm, colormap) # Resize to output size - out_img = cv2.resize(magnitude_colored, (400, 400), interpolation=cv2.INTER_CUBIC) + out_img = cv2.resize(magnitude_colored, (self.config_resolution_stitch[0], self.config_resolution_stitch[1]), interpolation=cv2.INTER_CUBIC) return out_img