我正在 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
我是一名优秀的程序员,十分优秀!