批量移动没有对应label的图片

Python 批量移动没有对应label的图片

  • 调用系统命令批量移动文件
  • 对于Yolo数据集格式,本程序可方便地筛选出未打标的图片
  • 将str list 转为 str ,例如: [“hello”, “world”] ----> “hello world”
import os
# import shutil

### yolo格式数据集文件夹根目录
root_path = "/home/yolov5/dataset"

img_path = os.path.join(root_path, "images/train/")
label_path = os.path.join(root_path, "labels/train/")

# 暂存文件夹
temp_path = os.path.join(img_path,"temp_image/")
if not os.path.exists(temp_path):
    os.mkdir(temp_path)

### 读取labels
label_list = os.listdir(label_path)
img_names = []
for label in label_list:
    img_names.append(label.replace("txt","jpg"))

# shutil 只能循环单个处理
# shutil.move(img_names,temp_path)

### 将已打标的图片移动到暂存文件夹
# str list to str
string = " "
img_name = string.join(img_names)

# 此处针对linux, win请自行研究
# sh: mv img_name temp_path
str1 = "cd " + img_path
str2 = "mv " + img_name + " " + temp_path
sh_str = str1 + "&&" +str2
os.system(sh_str)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值