删除数据集中多余的图片

import os
dataset_root_path = ''        #数据集地址
imglist = os.listdir(dataset_root_path)
d = 0
for item in imglist:
    if item.endswith('.jpg'):
        if item and f'{item[:-4]}.json' in imglist:
            print(d)
            d+=1
        else:
            c = dataset_root_path + item
            os.remove(c)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用哈希值算法来检测和删除数据集的重复图片。一种常用的哈希算法是感知哈希算法(Perceptual Hashing),它可以将图片转换成一个唯一的哈希值。以下是使用OpenCV和Python进行大批量删除重复图片的基本步骤: 1. 导入所需的库: ```python import os import cv2 import imagehash from PIL import Image ``` 2. 定义一个函数来计算图片的哈希值: ```python def calculate_hash(image_path): image = Image.open(image_path) image_hash = imagehash.average_hash(image) return str(image_hash) ``` 3. 创建一个字典来存储每个哈希值及其对应的图片路径: ```python hash_dict = {} ``` 4. 遍历数据集的每张图片,计算其哈希值,并将哈希值及其对应的图片路径存储在字典: ```python dataset_path = 'your_dataset_path' for root, dirs, files in os.walk(dataset_path): for file in files: image_path = os.path.join(root, file) image_hash = calculate_hash(image_path) if image_hash in hash_dict: hash_dict[image_hash].append(image_path) else: hash_dict[image_hash] = [image_path] ``` 5. 遍历字典的每个哈希值,如果该哈希值对应的图片路径数量大于1,则删除重复图片(保留一张即可): ```python for image_hash, image_paths in hash_dict.items(): if len(image_paths) > 1: for i in range(1, len(image_paths)): os.remove(image_paths[i]) ``` 注意:在删除图片之前,请务必备份你的数据集,以防止误删。 这是一个基本的示例,你可以根据自己的需求进行相应的修改和优化。希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值