将下面代码简洁化:def split_dataset(img_path, target_folder_path, output_path): filename = [] total_imgs = os.listdir(img_path) #for root, dirs, files in os.walk(img_path): for img in total_imgs: filename.append(img) np.random.shuffle(filename) train = filename[:int(len(filename) * 0.9)] test = filename[int(len(filename) * 0.9):] out_images = os.path.join(output_path, 'imgs') if not os.path.exists(out_images): os.makedirs(out_images) out_images_train = os.path.join(out_images, 'training') if not os.path.exists(out_images_train): os.makedirs(out_images_train) out_images_test = os.path.join(out_images, 'test') if not os.path.exists(out_images_test): os.makedirs(out_images_test) out_annotations = os.path.join(output_path, 'annotations') if not os.path.exists(out_annotations): os.makedirs(out_annotations) out_annotations_train = os.path.join(out_annotations, 'training') if not os.path.exists(out_annotations_train): os.makedirs(out_annotations_train) out_annotations_test = os.path.join(out_annotations, 'test') if not os.path.exists(out_annotations_test): os.makedirs(out_annotations_test) for i in train: print(os.path.join(img_path, i)) print(os.path.join(out_images_train, i)) shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_train, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_train, annotations_name)) for i in test: shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_test, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_test, annotations_name))

时间: 2024-04-11 14:32:44 浏览: 84
def split_dataset(img_path, target_folder_path, output_path): filename = os.listdir(img_path) np.random.shuffle(filename) train = filename[:int(len(filename) * 0.9)] test = filename[int(len(filename) * 0.9):] out_images = os.path.join(output_path, 'imgs') os.makedirs(out_images, exist_ok=True) out_images_train = os.path.join(out_images, 'training') os.makedirs(out_images_train, exist_ok=True) out_images_test = os.path.join(out_images, 'test') os.makedirs(out_images_test, exist_ok=True) out_annotations = os.path.join(output_path, 'annotations') os.makedirs(out_annotations, exist_ok=True) out_annotations_train = os.path.join(out_annotations, 'training') os.makedirs(out_annotations_train, exist_ok=True) out_annotations_test = os.path.join(out_annotations, 'test') os.makedirs(out_annotations_test, exist_ok=True) for i in train: img_src = os.path.join(img_path, i) img_dst = os.path.join(out_images_train, i) shutil.copyfile(img_src, img_dst) annotations_name = "gt_" + i[:-3] + 'txt' annotations_src = os.path.join(target_folder_path, annotations_name) annotations_dst = os.path.join(out_annotations_train, annotations_name) shutil.copyfile(annotations_src, annotations_dst) for i in test: img_src = os.path.join(img_path, i) img_dst = os.path.join(out_images_test, i) shutil.copyfile(img_src, img_dst) annotations_name = "gt_" + i[:-3] + 'txt' annotations_src = os.path.join(target_folder_path, annotations_name) annotations_dst = os.path.join(out_annotations_test, annotations_name) shutil.copyfile(annotations_src, annotations_dst)

相关推荐

import os import numpy as np from osgeo import gdal input_folder = 'G:/xianlinhotel/xlh632envi' output_folder = "G:/xianlinhotel/xlh_nir_rg_632envicai" target_width = 1230 target_height = 910 for filename in os.listdir(input_folder): if filename.endswith(".tif"): tif_path = os.path.join(input_folder, filename) tif_dataset = gdal.Open(tif_path) if tif_dataset is not None and tif_dataset.RasterXSize == 1280 and tif_dataset.RasterYSize == 960: data = tif_dataset.ReadAsArray() x_offset = (tif_dataset.RasterXSize - target_width) // 2 y_offset = (tif_dataset.RasterYSize - target_height) // 2 new_data = data[:, y_offset:y_offset+target_height, x_offset:x_offset+target_width] output_path = os.path.join(output_folder, filename) driver = gdal.GetDriverByName("GTiff") new_dataset = driver.Create(output_path, target_width, target_height, tif_dataset.RasterCount, tif_dataset.GetRasterBand(1).DataType) geotransform = tif_dataset.GetGeoTransform() new_geotransform = (geotransform[0] + x_offset * geotransform[1], geotransform[1], geotransform[2], geotransform[3] + y_offset * geotransform[5], geotransform[4], geotransform[5]) new_dataset.SetGeoTransform(new_geotransform) new_dataset.SetProjection(tif_dataset.GetProjection()) for i in range(1, tif_dataset.RasterCount + 1): new_dataset.GetRasterBand(i).WriteArray(new_data[i - 1]) new_dataset = None # 关闭数据集以保存文件和释放资源 print(f"Saved {filename} to {output_path}") else: print(f"{filename} has invalid size or is not a TIFF file.") tif_dataset = None # 关闭数据集以释放资源 详细解释

最新推荐

recommend-type

关于在labelme批量转化json文件时PermissionError: [Errno 13] Permission denied: ‘F:/zkx/list_path[i]’的解决

使用`utils.shapes_to_label`函数将形状数据转换为图像的标签层,然后用`imgviz.label2rgb`将标签层转换为彩色图像,便于可视化。 出现`PermissionError`的常见原因可能包括: 1. **文件正在被其他进程使用**:...
recommend-type

YOLOv5_DOTA_OBB-master-Windows运行环境配置.pdf

- 添加CUDA和cuDNN的环境变量:右键“我的电脑”,选择“属性”,进入“高级系统设置”,在“系统变量”的“Path”中添加CUDA和cuDNN的路径,如CUPTI\libx64和cudnn\bin。 4. **PyTorch安装**: - 访问PyTorch...
recommend-type

解决Tensorflow2.0 tf.keras.Model.load_weights() 报错处理问题

然而,在实际操作中,可能会遇到一些报错,本文将针对这些问题提供解决方案和相关知识。 首先,让我们回顾一下报错的场景: 1. 模型保存: ```python model.save_weights('./model.h5') ``` 2. 重新启动脚本并...
recommend-type

基于白冠鸡优化算法COOT-Kmean-Transformer-GRU实现数据回归预测算法研究Matlab代码.rar

1.版本:matlab2014/2019a/2021a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。 5.作者介绍:某大厂资深算法工程师,从事Matlab算法仿真工作10年;擅长智能优化算法、神经网络预测、信号处理、元胞自动机等多种领域的算法仿真实验,更多仿真源码、数据集定制私信+。 替换数据可以直接使用,注释清楚,适合新手
recommend-type

计算机系统基石:深度解析与优化秘籍

深入理解计算机系统(原书第2版)是一本备受推崇的计算机科学教材,由卡耐基梅隆大学计算机学院院长,IEEE和ACM双院院士推荐,被全球超过80所顶级大学选作计算机专业教材。该书被誉为“价值超过等重量黄金”的无价资源,其内容涵盖了计算机系统的核心概念,旨在帮助读者从底层操作和体系结构的角度全面掌握计算机工作原理。 本书的特点在于其起点低但覆盖广泛,特别适合大三或大四的本科生,以及已经完成基础课程如组成原理和体系结构的学习者。它不仅提供了对计算机原理、汇编语言和C语言的深入理解,还包含了诸如数字表示错误、代码优化、处理器和存储器系统、编译器的工作机制、安全漏洞预防、链接错误处理以及Unix系统编程等内容,这些都是提升程序员技能和理解计算机系统内部运作的关键。 通过阅读这本书,读者不仅能掌握系统组件的基本工作原理,还能学习到实用的编程技巧,如避免数字表示错误、优化代码以适应现代硬件、理解和利用过程调用、防止缓冲区溢出带来的安全问题,以及解决链接时的常见问题。这些知识对于提升程序的正确性和性能至关重要,使读者具备分析和解决问题的能力,从而在计算机行业中成为具有深厚技术实力的专家。 《深入理解计算机系统(原书第2版)》是一本既能满足理论学习需求,又能提供实践经验指导的经典之作,无论是对在校学生还是职业程序员,都是提升计算机系统知识水平的理想读物。如果你希望深入探究计算机系统的世界,这本书将是你探索之旅的重要伴侣。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

PHP数据库操作实战:手把手教你掌握数据库操作精髓,提升开发效率

![PHP数据库操作实战:手把手教你掌握数据库操作精髓,提升开发效率](https://img-blog.csdn.net/20180928141511915?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzE0NzU5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70) # 1. PHP数据库操作基础** PHP数据库操作是使用PHP语言与数据库交互的基础,它允许开发者存储、检索和管理数据。本章将介绍PHP数据库操作的基本概念和操作,为后续章节奠定基础。
recommend-type

vue-worker

Vue Worker是一种利用Web Workers技术的 Vue.js 插件,它允许你在浏览器的后台线程中运行JavaScript代码,而不影响主线程的性能。Vue Worker通常用于处理计算密集型任务、异步I/O操作(如文件读取、网络请求等),或者是那些需要长时间运行但不需要立即响应的任务。 通过Vue Worker,你可以创建一个新的Worker实例,并将Vue实例的数据作为消息发送给它。Worker可以在后台执行这些数据相关的操作,然后返回结果到主页面上,实现了真正的非阻塞用户体验。 Vue Worker插件提供了一个简单的API,让你能够轻松地在Vue组件中管理worker实例
recommend-type

《ThinkingInJava》中文版:经典Java学习宝典

《Thinking in Java》中文版是由知名编程作家Bruce Eckel所著的经典之作,这本书被广泛认为是学习Java编程的必读书籍。作为一本面向对象的编程教程,它不仅适合初学者,也对有一定经验的开发者具有启发性。本书的核心目标不是传授Java平台特定的理论,而是教授Java语言本身,着重于其基本语法、高级特性和最佳实践。 在内容上,《Thinking in Java》涵盖了Java 1.2时期的大部分关键特性,包括Swing GUI框架和新集合类库。作者通过清晰的讲解和大量的代码示例,帮助读者深入理解诸如网络编程、多线程处理、虚拟机性能优化以及与其他非Java代码交互等高级概念。书中提供了320个实用的Java程序,超过15000行代码,这些都是理解和掌握Java语言的宝贵资源。 作为一本获奖作品,Thinking in Java曾荣获1995年的Software Development Jolt Award最佳书籍大奖,体现了其在业界的高度认可。Bruce Eckel不仅是一位经验丰富的编程专家,还是C++领域的权威,他拥有20年的编程经历,曾在世界各地教授对象编程,包括C++和Java。他的著作还包括Thinking in C++,该书同样广受好评。 作者不仅是一位技术导师,还是一位教育家,他善于用易于理解的方式阐述复杂的编程概念,使读者能够领略到编程中的“智慧”。与其他Java教材相比,《Thinking in Java》以其成熟、连贯、严谨的风格,赢得了读者的一致赞誉,被誉为最全面且实例恰当的编程指南,是学习Java过程中不可或缺的参考资料。 此外,本书还提供了配套的CD,包含15小时的语音授课,以及可以从Bruce Eckel的官方网站www.BruceEckel.com免费获取的源码和电子版更新,确保读者能够跟随最新的技术发展保持同步。无论你是Java新手还是进阶者,《Thinking in Java》都是一次深入探索Java世界的重要旅程。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依