常用py脚本

#经常使用的小脚本

##1.json格式转换为xml

#!/usr/bin/env python
# coding: utf-8

import json
import os
import numpy as np
from xml.etree.ElementTree import Element, ElementTree

ROOT = r"C:/Users/Huaxifeng/Desktop/7.13/data1212/data/purchase/"
LABEL_ROOT = "Label"

# img_suffix = IMG_FILES[0][-3:]


def check_img_exist(root, json_files, image_files):
    for file in json_files:
        file_prefix = file[:-4]
        if file_prefix + 'jpg' not in image_files and file_prefix + 'png' not in image_files:
            os.remove(os.path.join(root, file))
            json_files.remove(file)
            print('no img {}'.format(file_prefix))
    return json_files


# 字典转xml元素数据
def dict_to_xml(tag, d, sub_elem=None):
    elem = Element(tag)
    for key, value in d.items():
        child = Element(key)
        child.text = str(value)
        elem.append(child)
    if sub_elem:
        for e in sub_elem:
            elem.append(e)
    return elem


# 保存json文件
def save_json(data_name, j_data):
    with open(data_name, "w", encoding="utf-8") as fp:
        json.dump(j_data, fp, ensure_ascii=False, indent=4)


# json文件转换,删除imageData内容
def json_convert(root, json_path):
    with open(json_path, encoding='utf-8', mode='r') as f:
        f_read = f.read()  # f_read是字符串

    data = json.loads(f_read)
    data["imageData"] = None
    data['shapes'][0]['label'] = root[2:]
    save_json(json_path, data)


def main():
    i = 0
    image_list = []
    xml_list = []
    # LABEL_ROOT检测创建
    if not os.path.exists(LABEL_ROOT):
        os.makedirs(LABEL_ROOT)
    assert os.path.exists(LABEL_ROOT), "不存在%s目录" % LABEL_ROOT

    label_list = open("label_list", "w+")
    label_list.write("trade")
    for root, dirs, files in os.walk(ROOT, topdown=True):
        print(root)
        print(dirs)
        json_files = [item for item in files if item[-4:] == 'json']
        image_files = [item for item in files if item[-3:] == 'png' or item[-3:] == 'jpg']
        print(json_files)
        if len(json_files) != 0:
            label_list.write("\n")
            label_list.write(root[2:])
            print(root[2:])
            label_dir = os.path.join(LABEL_ROOT, root[2:])
            # print(label_dir)
            # 检测目录是否存在并创建
            if not os.path.exists(label_dir):
                os.makedirs(label_dir)
                print("新建%s目录" % label_dir)
            assert os.path.exists(label_dir), "不存在%s目录" % label_dir
            # 检测json文件对应的图像文件是否存在
            json_files = check_img_exist(root, json_files, image_files)
            # 处理json文件,读取并进行转换,读取到数组内
            for json_file in json_files:
                json_path = os.path.join(root[2:], json_file)
                # json_convert(root, json_path)
                with open(json_path, encoding='utf-8', mode='r') as f:
                    f_read = f.read()  # f_read是字符串
                json_dict = json.loads(f_read)
                # 根据json获取所需json数据
                item = json_dict['shapes'][0]
                [xmin, ymin], [xmax, ymax] = item['points']
                # 生成xml数据
                bbox = dict_to_xml('bndbox',
                                   {'xmin': int(xmin), 'xmax': int(xmax), 'ymin': int(ymin), 'ymax': int(ymax)})
                object_ = dict_to_xml('object', {'name': item["label"], 'difficult': 0}, [bbox])
                anno = dict_to_xml('annotation', {"tmp": "tmp"}, [object_])
                # 根据xml数据生成xml文件格式
                tree = ElementTree(anno)
                xml_file = json_file[:-4] + "xml"
                xml_path = os.path.join(LABEL_ROOT, root[2:], xml_file)
                # 保存对应xml文件
                tree.write(xml_path, encoding='utf-8')
                image_path = json_path[:-4] + "png"

                image_path = '/'.join(image_path.split('\\'))
                image_list.append(image_path)
                xml_path = '/'.join(xml_path.split('\\'))
                xml_list.append(xml_path)
                print(xml_path)
    label_list.close()
    num = len(image_list)
    train_num = int(num * 0.8)
    eval_num = num - train_num
    arr_tmp = np.arange(num)
    np.random.shuffle(arr_tmp)
    count = 0
    train_txt = open("road_train.txt", "w+")
    eval_txt = open("road_eval.txt", "w+")
    for i in arr_tmp:
        if count < train_num:
            train_txt.write(image_list[i])
            train_txt.write(" ")
            train_txt.write(xml_list[i])
            if count < train_num - 1:
                train_txt.write("\n")
        else:
            eval_txt.write(image_list[i])
            eval_txt.write(" ")
            eval_txt.write(xml_list[i])
            if count < num - 1:
                eval_txt.write("\n")
        count = count + 1
    train_txt.close()
    eval_txt.close()
    print(num, train_num, eval_num)


if __name__ == '__main__':
    main()

2.文件批量重命名,序号依次增加

import os
import re


def ReName(dir_Path, pattern):
    i = 1
    for filename in os.listdir(dir_Path):
        print(filename)
        new_filename = str(i) + ".jpg"
        print(new_filename)
        os.rename(os.path.join(dir_Path, filename), os.path.join(dir_Path, new_filename))
        i = i + 1

    print("----------Success!-------------")


if __name__ == '__main__':
    dir_Path = r"C:\Users\Huaxifeng\Desktop\7.13\data\trade"
    pattern = re.compile(r'.*')
    ReName(dir_Path, pattern)

3.jpg后缀的照片更改为png

import os
import cv2

def transform(input_path, output_path):
    for root, dirs, files in os.walk(input_path):
        for name in files:
            file = os.path.join(root, name)
            print('transform' + name)
            im = cv2.imread(file)
            if output_path:
                cv2.imwrite(os.path.join(output_path, name.replace('jpg', 'png')), im)
            else:
                cv2.imwrite(file.replace('jpg', 'png'), im)


if __name__ == '__main__':
    input_path = input("请输入目标文件夹:")

    output_path = input("请输入输出文件夹: (回车则输出到原地址)")
    if not os.path.exists(input_path):
        print("文件夹不存在!")
    else:
        print("Start to transform!")
        transform(input_path, output_path)
        print("Transform end!")


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值