首页 > 其他分享 >(私人lora数据准备)MJ数据转lora训练的处理流程

(私人lora数据准备)MJ数据转lora训练的处理流程

时间:2023-11-20 22:11:56浏览次数:25  
标签:files os lora MJ file directory path txt 数据

1.【删除乱码并打标签】删前缀(用户名),删后缀(乱码),加统一标签,并打开excel微调。(输入项为1.单个文件夹地址 2.需要文件夹内加上的标签名)

*注意:此时若要加多个标签,请用英文逗号“,”隔开。

 

import os
import openpyxl
import re

UNWANTED_UNITS = ["undefined", "皮皮", "zly324"]


# 第一步:删名称
def rename_files(path):
    files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
    renamed_files = []

    counter = 1
    for file in files:
        filename, ext = os.path.splitext(file)

        # 乱码类
        if re.search(r'[a-f0-9]{32}', filename) or not '_' in filename:
            renamed = f"({counter})"
            counter += 1
        # AI出图类
        else:
            parts = re.split(r'[_]+', filename)
            parts.pop(0)  # 删除第一个单元

            # 删除特定的单元
            parts = [part for part in parts if part not in UNWANTED_UNITS]

            # 删除所有带数字的单元
            parts = [part for part in parts if not any(char.isdigit() for char in part)]

            # 结尾规则
            # 删除UUID风格数字
            while parts and re.search(r'^[a-f0-9\-]{32,}$', parts[-1]):
                parts.pop(-1)
            # 删除长度小于等于4的部分
            while parts and len(parts[-1]) <= 4:
                parts.pop(-1)

            renamed = '_'.join(parts)

        renamed_files.append(renamed + ext)

    return renamed_files


# 第二步:增名称
def add_prefix(files, prefix):
    prefixed_files = [f"{prefix}_{file}" if not file.startswith(prefix) else file for file in files]

    # 删除特定的单元
    prefixed_files = ['_'.join([part for part in re.split(r'[_]+', name) if part not in UNWANTED_UNITS]) for name in
                      prefixed_files]

    return prefixed_files


# 第三步:创建Excel并自动打开
def create_and_open_excel(files, renamed_files, path):
    wb = openpyxl.Workbook()
    ws = wb.active

    for original, renamed in zip(files, renamed_files):
        ws.append([original, renamed])

    excel_path = os.path.join(path, os.path.basename(path) + ".xlsx")
    wb.save(excel_path)

    # 打开Excel文件
    os.system(f'start "" "{excel_path}"')

    return excel_path


# 第五步:读取Excel并重命名文件
def rename_files_from_excel(path, excel_path):
    wb = openpyxl.load_workbook(excel_path)
    ws = wb.active

    for row in ws.iter_rows(values_only=True):
        original_name, new_name = row
        target_path = os.path.join(path, new_name)

        # 检查原文件是否存在
        if os.path.exists(os.path.join(path, original_name)):
            # 如果目标文件名已存在,则添加一个编号
            counter = 1
            base_name, ext = os.path.splitext(new_name)
            while os.path.exists(target_path):
                new_name = f"{base_name} ({counter}){ext}"
                target_path = os.path.join(path, new_name)
                counter += 1

            os.rename(os.path.join(path, original_name), target_path)

    print("重命名完成。")


# 主函数
def main():
    path = input("请输入文件夹地址: ")
    files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
    renamed_files = rename_files(path)

    prefix = input("请输入需要批量命名的词: ")
    prefixed_files = add_prefix(renamed_files, prefix)

    excel_path = create_and_open_excel(files, prefixed_files, path)
    print(f"Excel文件已保存为:{excel_path}")
    print("请在Excel里微调B列数据,然后保存和关闭Excel文件。完成后按Enter键继续...")

    input()

    # 重命名文件
    rename_files_from_excel(path, excel_path)


if __name__ == "__main__":
    main()

2.【处理下划线】统一只保留第一个下划线,删除后面的下划线(输入项为:总地址)

import os
import shutil

def copy_directory(src, dst):
    """复制 src 目录到 dst 目录。"""
    try:
        shutil.copytree(src, dst)
    except FileExistsError:
        print(f"备份目录 '{dst}' 已存在。")

def rename_image_files(directory):
    """重命名图片文件,保留第一个下划线,将其他下划线替换为空格,并在需要时添加递增编号。"""
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
                # 找到第一个下划线的位置
                first_underscore = file.find('_')
                if first_underscore != -1:
                    # 保留第一个下划线,将后续下划线替换为空格
                    before_first_underscore = file[:first_underscore + 1]
                    after_first_underscore = file[first_underscore + 1:].replace('_', ' ')
                    new_file_name = before_first_underscore + after_first_underscore
                else:
                    new_file_name = file

                if new_file_name != file:
                    original_file_path = os.path.join(root, file)
                    new_file_path = os.path.join(root, new_file_name)
                    increment = 1
                    # 循环直到找到不冲突的文件名
                    while os.path.exists(new_file_path):
                        # 分离文件名和扩展名
                        file_name, file_extension = os.path.splitext(new_file_name)
                        # 添加递增编号
                        new_file_name = f"{file_name} ({increment}){file_extension}"
                        new_file_path = os.path.join(root, new_file_name)
                        increment += 1
                    os.rename(original_file_path, new_file_path)
                    print(f"文件 {original_file_path} 已重命名为 {new_file_path}")

def main():
    # 请求用户输入要处理的目录路径
    input_directory = input("请输入要处理的目录路径: ")

    # 检查目录是否存在
    if not os.path.exists(input_directory):
        print(f"指定的目录 {input_directory} 不存在。")
        return

    # 创建目录的备份
    backup_directory = os.path.join(input_directory, "_backup")
    print(f"正在创建备份目录: {backup_directory}")
    copy_directory(input_directory, backup_directory)

    # 在备份目录中重命名图片文件
    print("正在重命名备份目录中的图片文件...")
    rename_image_files(backup_directory)
    print("重命名操作完成。")

if __name__ == "__main__":
    main()

3.【逗号转下划线】大地址下图片文件名所有逗号转换为下划线,适用于输入多条标签情况。(输入项为大地址)

import os

def replace_commas_in_filenames_interactive():
    """
    交互式地从用户处获取目录路径,然后遍历该路径及其子目录,
    并将图片文件名中的逗号替换为下划线。
    """
    path = input("请输入目录路径: ")
    modified_files = []
    # 支持的图片文件扩展名
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp']

    for root, dirs, files in os.walk(path):
        for file in files:
            if any(file.endswith(ext) for ext in image_extensions):
                # 替换逗号为下划线
                new_file = file.replace(',', '_').replace(',', '_')
                if new_file != file:
                    os.rename(os.path.join(root, file), os.path.join(root, new_file))
                    modified_files.append(os.path.join(root, new_file))

    return modified_files

# 运行这个函数
modified_files = replace_commas_in_filenames_interactive()
for f in modified_files:
    print(f"Modified file: {f}")

4.步骤1-3参照MJ转大模型训练,不同点在于,接下来需要的是:生成左右对称的镜像(防止图片不够)

import os
from PIL import Image

def flip_and_duplicate_image(image_path, output_path):
    """
    Flip an image horizontally and save a copy with a suffix.
    """
    try:
        with Image.open(image_path) as img:
            flipped_img = img.transpose(Image.FLIP_LEFT_RIGHT)
            flipped_img.save(output_path)
        return True
    except Exception as e:
        print(f"无法处理图像 {image_path}: {e}")
        return False

def process_images_in_directory(directory):
    """
    Process all images in a given directory and its subdirectories.
    """
    total_files = 0
    processed_files = 0

    # 预先计算总文件数以显示进度
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
                total_files += 1

    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
                original_path = os.path.join(root, file)
                filename, file_extension = os.path.splitext(file)
                output_path = os.path.join(root, f"{filename}(1){file_extension}")
                if not os.path.exists(output_path):  # 避免覆盖已存在的文件
                    if flip_and_duplicate_image(original_path, output_path):
                        processed_files += 1
                # 打印处理进度
                print(f"处理进度: {processed_files}/{total_files} ({(processed_files/total_files)*100:.2f}%)")

def main():
    directory = input("请输入要处理的文件夹的路径: ")
    if os.path.exists(directory) and os.path.isdir(directory):
        process_images_in_directory(directory)
        print("图片处理完成。")
    else:
        print("提供的路径无效或不是一个目录。")

# 运行脚本
main()

5.根据文件名生成文本,并在在文本内去掉多余元素,只保留描述语

import os
import re

def create_txt_from_image():
    # 请求用户输入文件夹地址
    root_folder = input("请输入图片所在文件夹的完整路径:")

    # 判断路径是否存在
    if not os.path.exists(root_folder):
        print("路径不存在,请检查输入的地址。")
        return

    # 用于存储创建的txt文件路径的列表
    created_txt_files = []

    # 使用os.walk遍历文件夹及其所有子文件夹
    for folder_path, dirs, files in os.walk(root_folder):
        for file in files:
            # 检查文件是否为图片(这里我们检查几种常见的图片格式)
            if file.endswith(('.jpg', '.png', '.jpeg')):
                # 获取不带扩展名的文件名
                base_name = os.path.splitext(file)[0]

                # 创建同名的txt文件路径
                txt_path = os.path.join(folder_path, base_name + '.txt')
                created_txt_files.append(txt_path)

                # 将图片文件名(不包括后缀)写入到txt文件中
                with open(txt_path, 'w', encoding='utf-8') as txt_file:
                    txt_file.write(base_name)

    print("所有图片对应的txt文件已创建完毕。")

    # 编译正则表达式以匹配括号及括号内的数字
    bracket_pattern = re.compile(r'\(\d+\)')
    underscore_pattern = "_" # 不需要正则表达式匹配单个字符

    # 遍历所有txt文件,进行内容修改
    for txt_file_path in created_txt_files:
        with open(txt_file_path, 'r', encoding='utf-8') as file:
            content = file.read()

        # 删除内容中的括号及括号内的数字
        content = bracket_pattern.sub('', content)
        # 把下划线替换成英文逗号
        content = content.replace(underscore_pattern, ",")

        with open(txt_file_path, 'w', encoding='utf-8') as file:
            file.write(content)

    print("所有txt文件内容中的括号及括号内的数字已删除,并且所有下划线已转换为英文逗号。")

# 运行函数
create_txt_from_image()

 

标签:files,os,lora,MJ,file,directory,path,txt,数据
From: https://www.cnblogs.com/zly324/p/17845028.html

相关文章

  • rsync+inotify数据传输
    rsync+inotify数据传输说明与名称IP应用操作系统源端:server192.168.58.146rsyncinotify-tools脚本centos7目标:node192.168.58.152rsynccentos7一、在node服务器操作1.关闭防火墙与selinuxsystemctlstopfirewalldsystemctldisablefirewalldsed......
  • 如何在 PHP 中使用 while 循环按 ID 列出节中的数据?
    要在PHP中使用while循环按ID列出数据库中的数据,您需要遵循以下步骤:创建数据库连接:首先,您需要使用适当的凭据来连接到数据库。您可以使用mysqli或PDO等库来实现数据库连接。$dbHost='localhost';$dbUsername='your_username';$dbPassword='your_password';$dbNam......
  • SQL 中的 NULL 值:定义、测试和处理空数据,以及 SQL UPDATE 语句的使用
    SQLNULL值什么是NULL值?NULL值是指字段没有值的情况。如果表中的字段是可选的,那么可以插入新记录或更新记录而不向该字段添加值。此时,该字段将保存为NULL值。需要注意的是,NULL值与零值或包含空格的字段不同。具有NULL值的字段是在记录创建期间留空的字段。如何测试NUL......
  • 数据的查,改,删
    #先讲数据库中的数据全部展示到前端然后给每一个数据两个按钮一个编辑一个删除#查看defuserlist(request):#查询出用户表里面所有的数据#方式1#data=models.User.objects.filter()#print(data)#方式2user_queryset=models.User.objects.al......
  • SQL 中的 NULL 值:定义、测试和处理空数据,以及 SQL UPDATE 语句的使用
    SQLNULL值什么是NULL值?NULL值是指字段没有值的情况。如果表中的字段是可选的,那么可以插入新记录或更新记录而不向该字段添加值。此时,该字段将保存为NULL值。需要注意的是,NULL值与零值或包含空格的字段不同。具有NULL值的字段是在记录创建期间留空的字段。如何测试NU......
  • pycharm链接数据库 django链接MySQL
    #找到pycharmdatabase选项(三个地方查找)#选取对应的数据库下载对应的驱动"""明明链接上了数据库但是看不到表无法操作这个时候你只需要将刚刚创建的链接删除重新链接一次即可"""  #1.配置文件中配置DATABASES={'default':{'ENGINE':'django.db.back......
  • 配置impala自动同步HMS元数据
    由于Impala的AutomaticInvalidate/RefreshMetadata的功能在CDH6.3版本才有的功能,通过以上两个升级,已经具备的该功能,下面是需要配置该功能测试环境1.CM和CDH版本为6.1.1(hive的版本升级到了CDH6.3.2-2.1.1)2.操作系统版本为RedHat7.63.impala3.4版本操作步骤进入CM界面>Hive......
  • Redis:Key-Value的NoSQL数据库
    Redis:Key-Value的NoSQL数据库(基础)主要内容:数据库分类Redis入门客户端连接RedisRedis数据类型及操作Springboot整合RedisSpringCache学习目标:知识点要求数据库分类熟悉Redis使用场景熟悉Redis下载与安装了解Redis启动关闭掌握客户端连接Redis......
  • 【C++】【OpenCV】【NumPy】图像数据的访问
    接上一随笔,这次学习针对图像数据的访问(Numpy.array)在OpenCV中,使用imread()方法可以访问图像,其返回值是一个数组,而根据传入的不同图像,将会返回不同维度的数组。针对返回的图像数据,即数组,我们是可以进行操作的:1importcv223#MyPic.png图像自行随意创建一个原始字符转换......
  • sodick 成型机 沙迪克 数据采集 制程参数
    1、工业电脑主机设置2个共享文件夹比如在D盘:EXPORT和DATA  ,名字不能改。新建Window登录账号sodick和plustech,密码111 手册中有账号密码  2、sodick机台设置成型机的ip和工业电脑主机的ip,保证在同一个网段。 sodick自己会放共享里面写文件。账号密码如上面,不能改。......