提交 3a39a517 编写于 作者: 三月三net's avatar 三月三net

Python超人-宇宙模拟器

上级 032adce8
...@@ -370,7 +370,7 @@ if __name__ == '__main__': ...@@ -370,7 +370,7 @@ if __name__ == '__main__':
cosmic_bg='', cosmic_bg='',
show_trail=True, show_trail=True,
# bg_music='sounds/no_glory.mp3', # bg_music='sounds/no_glory.mp3',
# show_camera_info=False, show_camera_info=False,
save_as_video=True, save_as_video=True,
show_control_info=False, show_control_info=False,
timer_enabled=True, timer_enabled=True,
......
...@@ -2,6 +2,7 @@ from ursina import * ...@@ -2,6 +2,7 @@ from ursina import *
import os, shutil import os, shutil
import builtins import builtins
import numpy as np import numpy as np
import cv2
class CubeMapVideoRecorder(Entity): class CubeMapVideoRecorder(Entity):
...@@ -65,6 +66,13 @@ class VideoRecorder(Entity): ...@@ -65,6 +66,13 @@ class VideoRecorder(Entity):
self.max_frames = int(self.duration * self.fps) self.max_frames = int(self.duration * self.fps)
self.frames = [] self.frames = []
self.save_name = "video_temp.mp4"
self.video = self.create_video(864, 1536)
def create_video(self, height, width):
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(self.save_name, fourcc, self.fps, (width, height))
return video
def start_recording(self): def start_recording(self):
print('start recording,', self.duration, self.file_path) print('start recording,', self.duration, self.file_path)
...@@ -74,40 +82,85 @@ class VideoRecorder(Entity): ...@@ -74,40 +82,85 @@ class VideoRecorder(Entity):
self.max_frames = self.duration * self.fps self.max_frames = self.duration * self.fps
if not self.file_path.exists(): if not self.file_path.exists():
self.file_path.mkdir() self.file_path.mkdir()
base.movie(namePrefix=f'\\video_temp\\{self.video_name}', duration=2.0, fps=30, format='png', sd=4) # base.movie(namePrefix=f'\\video_temp\\{self.video_name}', duration=2.0, fps=30, format='png', sd=4)
self.recording = True self.recording = True
invoke(self.stop_recording, delay=self.duration) # invoke(self.stop_recording, delay=self.duration)
def stop_recording(self): def stop_recording(self):
self.recording = False self.recording = False
window.fps_counter.enabled = True window.fps_counter.enabled = True
window.exit_button.visible = True window.exit_button.visible = True
print('stop recording') print('stop recording')
self.convert_to_gif() self.convert_to_mp4()
def convert_to_mp4(self):
print("视频保存中")
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 update(self): def update(self):
if not self.recording: if not self.recording:
return return
self.t += time.dt self.t += time.dt
if self.t >= 1 / 30: if self.t >= 1 / 30:
base.screenshot( # base.screenshot(
namePrefix='\\video_temp\\' + self.video_name + '_' + str(self.i).zfill(4) + '.png', # namePrefix='\\video_temp\\' + self.video_name + '_' + str(self.i).zfill(4) + '.png',
defaultFilename=0, # defaultFilename=0,
) # )
self.t = 0 self.t = 0
# # self.frames.append(self.renderToPNM()) # image = deepcopy(camera.render_texture)
# image = base.win.getScreenshot() # self.frames.append(self.renderToPNM())
# data = image.getRamImageAs("RGB").getData() from PIL import Image
# # from PIL import Image import io
# # image = Image.fromarray(data) import imageio
# # img = data.convert("RGBA") image = base.win.getScreenshot()
# data = np.array(data) # image = image.get_internal_image()
data = image.getRamImageAs("BGR").getData()
np_data = np.frombuffer(data, dtype=np.uint8)
# #
img_data = np.reshape(np_data, (864, 1536, 3))
# img = Image.fromarray(img_data)
# img = self.screenshot_to_opencv_img(image)
# img = np.array(data)
# image = Image.open(io.BytesIO(data))
# image = Image.fromarray(np.array(data))
# img = data.convert("RGBA")
# data = np.array(data)
# 获取图片的宽和高
width = img_data.shape[1]
height = img_data.shape[0]
# 创建一个空的数组,用于存储还原后的图像
normal_image = np.zeros((height, width, 3), dtype=np.uint8)
# 将颠倒图像的像素还原到正常图像中
for i in range(height):
for j in range(width):
normal_image[i, j] = img_data[height - i - 1, j]
# # image = deepcopy(camera.render_texture) # # image = deepcopy(camera.render_texture)
# self.frames.append(data) # 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.video.write(normal_image)
# store screenshot in memory # store screenshot in memory
# def renderToPNM(self): # def renderToPNM(self):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册