gpt4 book ai didi

python - 如何将一组图像转换为单个 data_test.npz 文件?

转载 作者:太空宇宙 更新时间:2023-11-03 19:55:57 24 4
gpt4 key购买 nike

我正在 build 一辆自动驾驶遥控汽车。我有 100 张从 pi 相机拍摄的图像,每一张都被命名为 Direction.jpg 。如何将这些图像转换为单个 .npz 文件,以便我可以训练神经网络。

下面的代码是一个简单的 Python 脚本,它使我的汽车能够在按键时移动并每秒拍摄照片。

#!/usr/bin/env python
"""
def interactive_control():
"""Runs the interactive control"""
setup_interactive_control()
clock = pygame.time.Clock()
with picamera.PiCamera() as camera:
""" camera code """
command = 'idle'
duty_cycle = configuration.INITIAL_PWM_DUTY_CYCLE
while True:
up_key, down, left, right, change, accelerate, decelerate, stop = get_keys()

print(command)
stream = io.BytesIO()
camera.capture(stream, format='jpeg', use_video_port=True)
image_helper.save_image_with_direction(stream, command)
stream.flush()
pygame.quit()
def setup_interactive_control():
"""Setup the Pygame Interactive Control Screen"""
def main():
"""Main function"""
if __name__ == '__main__':
main()

请帮忙!

最佳答案

这会将名为 Direction.jpg 的图像保存为 npz 的:

from PIL import Image
import os
import numpy as np

path_to_files = "/my/path/"
array_of_images = []

for _, file in enumerate(os.listdir(path_to_files)):
if "direction.jpg" in file: # to check if file has a certain name
single_im = Image.open(file)
single_array = np.array(im)
array_of_images.append(single_array)
np.savez("all_images.npz",array_of_images) # save all in one file

有人对 png 做了同样的事情: https://gist.github.com/PaulGwamanda/f91ce9fc9d392c4bcc99c085fd726a34

关于python - 如何将一组图像转换为单个 data_test.npz 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59530880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com