From bf539743fa0a58cc8dfadadd34f473f1a9650424 Mon Sep 17 00:00:00 2001 From: march3 Date: Sat, 11 Nov 2023 18:00:57 +0800 Subject: [PATCH] =?UTF-8?q?Python=E8=B6=85=E4=BA=BA-=E5=AE=87=E5=AE=99?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sim_lab/earth_moon_reality_bak.py | 5 +- sim_scenes/func.py | 9 +- sim_scenes/solar_system/earth_moon_reality.py | 6 +- sim_scenes/solar_system/halley_comet_lib.py | 18 +++- sim_scenes/solar_system/halley_comet_sim.py | 6 +- .../solar_system/solar_system_reality.py | 5 +- simulators/ursina/entities/video_recorder.py | 84 ++++++++++--------- 7 files changed, 81 insertions(+), 52 deletions(-) diff --git a/sim_lab/earth_moon_reality_bak.py b/sim_lab/earth_moon_reality_bak.py index 12675b4..e1df7d9 100644 --- a/sim_lab/earth_moon_reality_bak.py +++ b/sim_lab/earth_moon_reality_bak.py @@ -95,8 +95,11 @@ class SolarSystemRealitySim: if self.clock_position_center: position, origin = (0, .25), (0, 0) else: + from ursina import window + aspect_ratio = window.aspect_ratio + position, origin = (0.5 * aspect_ratio - 0.3, -0.465), (-0.5, 0.5), # position, origin = (0.60, -0.465), (-0.5, 0.5) - position, origin = (-0.60, -0.465), (-0.5, 0.5) + # position, origin = (-0.60, -0.465), (-0.5, 0.5) ControlUI.current_ui.show_message(dt.strftime('%Y-%m-%d %H:%M:%S'), position=position, diff --git a/sim_scenes/func.py b/sim_scenes/func.py index efb41ff..f78e3ad 100644 --- a/sim_scenes/func.py +++ b/sim_scenes/func.py @@ -411,16 +411,19 @@ def create_3d_card(left=-.885, top=0.495, width=0.02, height=0.02): def create_text_panel(width=0.35, height=.5): # 创建一个 Panel 组件 - from ursina import Text, Panel, color, camera, Vec3 + from ursina import Text, Panel, color, camera, Vec3, window from simulators.ursina.ursina_config import UrsinaConfig - panel = Panel( + aspect_ratio = window.aspect_ratio + # 1920, 1080 1.777777777 position=(-.88, 0.3, 0), # 1.77 + # 2376, 1080 2.2 position=(-.99, 0.3, 0), # 1.77 + panel = Panel( # 2 parent=None, model='quad', # texture='white_cube', color=color.black, origin=(-.48, .48, -.48), scale=(width, height), - position=(-.88, 0.3, 0), + position=(-.49 * aspect_ratio, 0.3, 0), alpha=0.5 ) diff --git a/sim_scenes/solar_system/earth_moon_reality.py b/sim_scenes/solar_system/earth_moon_reality.py index e0c0a85..2908996 100644 --- a/sim_scenes/solar_system/earth_moon_reality.py +++ b/sim_scenes/solar_system/earth_moon_reality.py @@ -95,8 +95,10 @@ class SolarSystemRealitySim: if self.clock_position_center: position, origin = (0, .25), (0, 0) else: - # position, origin = (0.60, -0.465), (-0.5, 0.5) - position, origin = (-0.60, -0.465), (-0.5, 0.5) + from ursina import window + aspect_ratio = window.aspect_ratio + position, origin = (0.5 * aspect_ratio - 0.3, -0.465), (-0.5, 0.5), + # position, origin = (-0.60, -0.465), (-0.5, 0.5) ControlUI.current_ui.show_message(dt.strftime('%Y-%m-%d %H:%M:%S'), position=position, diff --git a/sim_scenes/solar_system/halley_comet_lib.py b/sim_scenes/solar_system/halley_comet_lib.py index 8e99ea6..b3d445a 100644 --- a/sim_scenes/solar_system/halley_comet_lib.py +++ b/sim_scenes/solar_system/halley_comet_lib.py @@ -68,9 +68,20 @@ class HalleyCometSimBase: self.pluto = None self.bodies = [] - def set_window_size(self, size=(1536, 684)): + def set_window_size(self, size=(1536, 684), fullscreen=False): from ursina import window + if fullscreen: + # 设置窗口为全屏模式 + window.fullscreen = True + + # 设置窗口的宽度和高度 window.size = size + # self.set_window_size((3500, 1024)) + # r = 1 + # self.set_window_size((1920*2, 1080*2)) + # self.set_window_size((int(1920 * r), int(1080 * r))) + # self.set_window_size((2376, 1080)) + # self.set_window_size((520, 540), fullscreen=False) def build_solar_system(self, ignore_gravity=False, start_time=None): # region 构建太阳系 @@ -126,8 +137,9 @@ class HalleyCometSimBase: # if self.clock_position_center: # position, origin = (0, .25), (0, 0), # else: - position, origin = (0.60, -0.465), (-0.5, 0.5), - + from ursina import window + aspect_ratio = window.aspect_ratio + position, origin = (0.5 * aspect_ratio - 0.3, -0.465), (-0.5, 0.5), ControlUI.current_ui.show_message(dt.strftime('%Y-%m-%d %H:%M:%S'), position=position, origin=origin, diff --git a/sim_scenes/solar_system/halley_comet_sim.py b/sim_scenes/solar_system/halley_comet_sim.py index f6a4755..ab828ef 100644 --- a/sim_scenes/solar_system/halley_comet_sim.py +++ b/sim_scenes/solar_system/halley_comet_sim.py @@ -72,8 +72,8 @@ class HalleyCometSim(HalleyCometSimBase): # UrsinaConfig.trail_length = 180 UrsinaConfig.trail_factor = 3 - # self.set_window_size((3500, 1024)) - # self.set_window_size((1920, 1080)) + r = 1 + self.set_window_size((int(1920 * r), int(1080 * r)), True) # camera.clip_plane_near = 0.1 camera.clip_plane_far = 1000000 @@ -349,7 +349,7 @@ if __name__ == '__main__': show_trail=True, # bg_music='sounds/no_glory.mp3', show_camera_info=False, - # save_as_video=True, + save_as_video=True, show_control_info=False, timer_enabled=True, show_grid=False diff --git a/sim_scenes/solar_system/solar_system_reality.py b/sim_scenes/solar_system/solar_system_reality.py index 3813d70..b351c5d 100644 --- a/sim_scenes/solar_system/solar_system_reality.py +++ b/sim_scenes/solar_system/solar_system_reality.py @@ -100,7 +100,10 @@ class SolarSystemRealitySim: if self.clock_position_center: position, origin = (0, .25), (0, 0), else: - position, origin = (0.60, -0.465), (-0.5, 0.5), + from ursina import window + aspect_ratio = window.aspect_ratio + position, origin = (0.5 * aspect_ratio - 0.3, -0.465), (-0.5, 0.5), + # position, origin = (0.60, -0.465), (-0.5, 0.5), ControlUI.current_ui.show_message(dt.strftime('%Y-%m-%d %H:%M:%S'), position=position, diff --git a/simulators/ursina/entities/video_recorder.py b/simulators/ursina/entities/video_recorder.py index 35e9872..313d430 100644 --- a/simulators/ursina/entities/video_recorder.py +++ b/simulators/ursina/entities/video_recorder.py @@ -64,10 +64,10 @@ class VideoRecorder(Entity): for key, value in kwargs.items(): setattr(self, key, value) - self.max_frames = int(self.duration * self.fps) - self.frames = [] + # self.max_frames = int(self.duration * self.fps) + # self.frames = [] self.save_name = "video_temp.mp4" - self.video = self.create_video(864, 1536) + self.video = None def create_video(self, height, width): fourcc = cv2.VideoWriter_fourcc(*'mp4v') @@ -75,11 +75,11 @@ class VideoRecorder(Entity): return video def start_recording(self): - print('start recording,', self.duration, self.file_path) + print('start recording,', self.file_path) window.fps_counter.enabled = False window.exit_button.visible = False - self.frames = [] - self.max_frames = self.duration * self.fps + # self.frames = [] + # self.max_frames = self.duration * self.fps if not self.file_path.exists(): self.file_path.mkdir() # base.movie(namePrefix=f'\\video_temp\\{self.video_name}', duration=2.0, fps=30, format='png', sd=4) @@ -96,35 +96,35 @@ class VideoRecorder(Entity): def convert_to_mp4(self): print("视频保存中") - for frame in self.frames: - self.video.write(frame) + # for frame in self.frames: + # self.video.write(frame) self.video.release() cv2.destroyAllWindows() # crop('video.mp4') print("视频保存完成") print(self.save_name) - def screenshot_to_opencv_img(self, texture): - # 将Texture对象转换为numpy数组 - array = texture.to_array() - # 将数组reshape为4D,以便opencv可以正确读取 - array = np.reshape(array, (texture.height, texture.width, -1)) - # 转换为opencv读取的格式,注意需要先安装opencv库 - img = cv2.cvtColor(array, - cv2.COLOR_RGB2BGR) # 这里的cv2.COLOR_RGB2BGR是将RGB格式转换为BGR格式,因为Ursina使用的是RGB格式,而opencv使用的是BGR格式 - return img + # def screenshot_to_opencv_img(self, texture): + # # 将Texture对象转换为numpy数组 + # array = texture.to_array() + # # 将数组reshape为4D,以便opencv可以正确读取 + # array = np.reshape(array, (texture.height, texture.width, -1)) + # # 转换为opencv读取的格式,注意需要先安装opencv库 + # img = cv2.cvtColor(array, + # cv2.COLOR_RGB2BGR) # 这里的cv2.COLOR_RGB2BGR是将RGB格式转换为BGR格式,因为Ursina使用的是RGB格式,而opencv使用的是BGR格式 + # return img def update(self): if not self.recording: return - self.t += time.dt - if self.t >= 1 / 30: - # base.screenshot( - # namePrefix='\\video_temp\\' + self.video_name + '_' + str(self.i).zfill(4) + '.png', - # defaultFilename=0, - # ) - self.t = 0 + # self.t += time.dt + # if self.t >= 1 / 30: + # # base.screenshot( + # # namePrefix='\\video_temp\\' + self.video_name + '_' + str(self.i).zfill(4) + '.png', + # # defaultFilename=0, + # # ) + # self.t = 0 # image = deepcopy(camera.render_texture) # self.frames.append(self.renderToPNM()) from PIL import Image @@ -136,7 +136,12 @@ class VideoRecorder(Entity): data = image.getRamImageAs("BGR").getData() np_data = np.frombuffer(data, dtype=np.uint8) # - img_data = np.reshape(np_data, (864, 1536, 3)) + if self.video is None: + self.video_height = image.y_size # 864 + self.video_width = image.x_size # 1536 + self.video = self.create_video(self.video_height, self.video_width) + + img_data = np.reshape(np_data, (self.video_height, self.video_width, 3)) # img = Image.fromarray(img_data) # img = self.screenshot_to_opencv_img(image) # img = np.array(data) @@ -158,12 +163,13 @@ class VideoRecorder(Entity): # # image = deepcopy(camera.render_texture) # self.frames.append(data) - self.i += 1 + # self.i += 1 # img_bgr = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR) # if imageNum < self.fps * args.total_time: # show_image(frame) - self.frames.append(normal_image) + # self.frames.append(normal_image) + self.video.write(normal_image) # store screenshot in memory # def renderToPNM(self): # base.graphicsEngine.renderFrame() @@ -175,18 +181,18 @@ class VideoRecorder(Entity): # # win.setupRenderTexture() # return None - def convert_to_gif(self): - import imageio - images = [] - if not os.path.exists(self.file_path): - return - - for filename in os.listdir(self.file_path): - images.append(imageio.imread(self.file_path / filename)) - - imageio.mimsave(Path(f'{self.file_path.parent}/{self.video_name}.gif'), images) - shutil.rmtree(self.file_path) # delete temp folder - print('saved gif to:', Path(f'{self.file_path.parent}/{self.video_name}.gif')) + # def convert_to_gif(self): + # import imageio + # images = [] + # if not os.path.exists(self.file_path): + # return + # + # for filename in os.listdir(self.file_path): + # images.append(imageio.imread(self.file_path / filename)) + # + # imageio.mimsave(Path(f'{self.file_path.parent}/{self.video_name}.gif'), images) + # shutil.rmtree(self.file_path) # delete temp folder + # print('saved gif to:', Path(f'{self.file_path.parent}/{self.video_name}.gif')) class VideoRecorderUI(WindowPanel): -- GitLab