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

标签 python numpy pygame hardware pygame-surface

我正在 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/

相关文章:

python - Django 中过滤内的正则表达式

python - 如何从 Python 3 导入 FileNotFoundError?

python - 如何使用 pandas 中的正则表达式来屏蔽具有某些 nan 值的列?

python - 选择索引周围的 NumPy 值

python - 如何使用 SWIG 将 Fortran 有序 2d numpy 数组传递到 C++

python - 如何显示Python字符串中的特定字母?

python - 使用boto3 python计算s3中csv文件的行数

python - 找不到 postgresql 9 plpython2.dll

python - 如何使用 pygame 随机移动迷宫中的物体?

python - 使用向量叉积隐藏 3D 人脸