当前位置: 首页 > wzjs >正文

怎么地wordpress深圳债务优化公司

怎么地wordpress,深圳债务优化公司,seo查询工具网站,商标注册查询系统第一章 使用说明 类别自己在代码中改,其他四个参数 --json-folder:json文件夹路径 --txt-folder:转换成功后txt的存放路径 --images-dir:图片文件夹路径 --save-dir:转换完成分割后所有文件的路径 终端命令行:p…

第一章  使用说明

类别自己在代码中改,其他四个参数

--json-folder:json文件夹路径

--txt-folder:转换成功后txt的存放路径

--images-dir:图片文件夹路径

--save-dir:转换完成分割后所有文件的路径

终端命令行:python split_conversion.py --json-folder "" --txt-folder "" --images-dir "" --save-dir ""

""处是四个路径

第二章 导包

import json
import os
from tqdm import tqdm
import shutil
import random
import argparse

第三章 转换格式函数

def labelme_json_to_yolo(txt_save_path, json_path, class_map):"""将LabelMe格式JSON转换为YOLO TXT标注文件:param txt_save_path: TXT保存路径:param json_path: LabelMe JSON文件路径:param class_map: 类别名映射字典(如{"person": 0})"""with open(json_path, 'r', encoding='utf-8') as f:data = json.load(f)img_height = data["imageHeight"]img_width = data["imageWidth"]img_name = os.path.basename(data["imagePath"])  # 提取图片文件名txt_name = os.path.splitext(img_name)[0] + ".txt"txt_path = os.path.join(txt_save_path, txt_name)lines = []for shape in data["shapes"]:label = shape["label"]if label not in class_map:continue  # 跳过未定义类别cls_id = class_map[label]# 解析多边形/矩形标注为边界框(xmin, ymin, xmax, ymax)points = shape["points"]x_coords = [p[0] for p in points]y_coords = [p[1] for p in points]xmin = min(x_coords)ymin = min(y_coords)xmax = max(x_coords)ymax = max(y_coords)# 计算YOLO格式坐标(归一化中心坐标+宽高)x_center = (xmin + xmax) / (2 * img_width)y_center = (ymin + ymax) / (2 * img_height)w = (xmax - xmin) / img_widthh = (ymax - ymin) / img_height# 过滤无效坐标(避免越界)if 0 <= x_center <= 1 and 0 <= y_center <= 1 and w > 0 and h > 0:lines.append(f"{cls_id} {x_center:.6f} {y_center:.6f} {w:.6f} {h:.6f}")# 保存TXT文件with open(txt_path, 'w') as f:f.write('\n'.join(lines))

第四章 划分函数

def batch_convert_labelme_to_yolo(json_folder, txt_folder, class_map):"""批量转换LabelMe格式JSON文件夹到YOLO TXT:param json_folder: JSON文件夹路径:param txt_folder: TXT保存路径:param class_map: 类别名映射字典(如{"person": 0})"""os.makedirs(txt_folder, exist_ok=True)json_files = [f for f in os.listdir(json_folder) if f.lower().endswith('.json')]for json_file in tqdm(json_files, desc="转换中"):json_path = os.path.join(json_folder, json_file)labelme_json_to_yolo(txt_folder, json_path, class_map)print(f"批量转换完成!共处理{len(json_files)}个JSON文件,保存到:{txt_folder}")

第五章 确认文件夹是否存在

def mkdir(path):if not os.path.exists(path):os.makedirs(path)

第六章主函数

def main(image_dir, txt_dir, save_dir):# 创建文件夹mkdir(save_dir)images_dir = os.path.join(save_dir, 'images')labels_dir = os.path.join(save_dir, 'labels')img_train_path = os.path.join(images_dir, 'train')img_test_path = os.path.join(images_dir, 'test')img_val_path = os.path.join(images_dir, 'val')label_train_path = os.path.join(labels_dir, 'train')label_test_path = os.path.join(labels_dir, 'test')label_val_path = os.path.join(labels_dir, 'val')mkdir(images_dir)mkdir(labels_dir)mkdir(img_train_path)mkdir(img_test_path)mkdir(img_val_path)mkdir(label_train_path)mkdir(label_test_path)mkdir(label_val_path)# 数据集划分比例,训练集80%,验证集10%,测试集10%,按需修改train_percent = 0.8val_percent = 0.1test_percent = 0.1total_txt = os.listdir(txt_dir)num_txt = len(total_txt)list_all_txt = range(num_txt)  # 范围 range(0, num)num_train = int(num_txt * train_percent)num_val = int(num_txt * val_percent)num_test = num_txt - num_train - num_valtrain = random.sample(list_all_txt, num_train)# 在全部数据集中取出trainval_test = [i for i in list_all_txt if not i in train]# 再从val_test取出num_val个元素,val_test剩下的元素就是testval = random.sample(val_test, num_val)print("训练集数目:{}, 验证集数目:{}, 测试集数目:{}".format(len(train), len(val), len(val_test) - len(val)))for i in list_all_txt:name = total_txt[i][:-4]srcImage = os.path.join(image_dir, name + '.jpg')srcLabel = os.path.join(txt_dir, name + '.txt')if i in train:dst_train_Image = os.path.join(img_train_path, name + '.jpg')dst_train_Label = os.path.join(label_train_path, name + '.txt')shutil.copyfile(srcImage, dst_train_Image)shutil.copyfile(srcLabel, dst_train_Label)elif i in val:dst_val_Image = os.path.join(img_val_path, name + '.jpg')dst_val_Label = os.path.join(label_val_path, name + '.txt')shutil.copyfile(srcImage, dst_val_Image)shutil.copyfile(srcLabel, dst_val_Label)else:dst_test_Image = os.path.join(img_test_path, name + '.jpg')dst_test_Label = os.path.join(label_test_path, name + '.txt')shutil.copyfile(srcImage, dst_test_Image)shutil.copyfile(srcLabel, dst_test_Label)

 第七章 主函数调用

if __name__ == "__main__":parser = argparse.ArgumentParser(description='Convert LabelMe JSON to YOLO TXT and split datasets')parser.add_argument('--json-folder', type=str, default=r'',help='LabelMe JSON folder path')parser.add_argument('--txt-folder', type=str, default=r'',help='YOLO TXT save path')parser.add_argument('--class-map', default={"自己的类别": 0}, type=dict,help='Class name mapping dictionary (e.g. {"person": 0})')parser.add_argument('--images-dir', type=str, default=r'', help='images path dir')parser.add_argument('--save-dir', default=r'', type=str, help='save dir')args = parser.parse_args()json_folder = args.json_foldertxt_folder = args.txt_folderclass_map = args.class_mapimage_dir = args.images_dirsave_dir = args.save_dir# 转换LabelMe JSON到YOLO TXTbatch_convert_labelme_to_yolo(json_folder, txt_folder, class_map)# 划分数据集main(image_dir, txt_folder, save_dir)

全部代码如下: 

import json
import os
from tqdm import tqdm  # 可选进度条库
import shutil
import random
import argparsedef labelme_json_to_yolo(txt_save_path, json_path, class_map):"""将LabelMe格式JSON转换为YOLO TXT标注文件:param txt_save_path: TXT保存路径:param json_path: LabelMe JSON文件路径:param class_map: 类别名映射字典(如{"person": 0})"""with open(json_path, 'r', encoding='utf-8') as f:data = json.load(f)img_height = data["imageHeight"]img_width = data["imageWidth"]img_name = os.path.basename(data["imagePath"])  # 提取图片文件名txt_name = os.path.splitext(img_name)[0] + ".txt"txt_path = os.path.join(txt_save_path, txt_name)lines = []for shape in data["shapes"]:label = shape["label"]if label not in class_map:continue  # 跳过未定义类别cls_id = class_map[label]# 解析多边形/矩形标注为边界框(xmin, ymin, xmax, ymax)points = shape["points"]x_coords = [p[0] for p in points]y_coords = [p[1] for p in points]xmin = min(x_coords)ymin = min(y_coords)xmax = max(x_coords)ymax = max(y_coords)# 计算YOLO格式坐标(归一化中心坐标+宽高)x_center = (xmin + xmax) / (2 * img_width)y_center = (ymin + ymax) / (2 * img_height)w = (xmax - xmin) / img_widthh = (ymax - ymin) / img_height# 过滤无效坐标(避免越界)if 0 <= x_center <= 1 and 0 <= y_center <= 1 and w > 0 and h > 0:lines.append(f"{cls_id} {x_center:.6f} {y_center:.6f} {w:.6f} {h:.6f}")# 保存TXT文件with open(txt_path, 'w') as f:f.write('\n'.join(lines))def batch_convert_labelme_to_yolo(json_folder, txt_folder, class_map):"""批量转换LabelMe格式JSON文件夹到YOLO TXT:param json_folder: JSON文件夹路径:param txt_folder: TXT保存路径:param class_map: 类别名映射字典(如{"person": 0})"""os.makedirs(txt_folder, exist_ok=True)json_files = [f for f in os.listdir(json_folder) if f.lower().endswith('.json')]for json_file in tqdm(json_files, desc="转换中"):json_path = os.path.join(json_folder, json_file)labelme_json_to_yolo(txt_folder, json_path, class_map)print(f"批量转换完成!共处理{len(json_files)}个JSON文件,保存到:{txt_folder}")# 检查文件夹是否存在
def mkdir(path):if not os.path.exists(path):os.makedirs(path)def main(image_dir, txt_dir, save_dir):# 创建文件夹mkdir(save_dir)images_dir = os.path.join(save_dir, 'images')labels_dir = os.path.join(save_dir, 'labels')img_train_path = os.path.join(images_dir, 'train')img_test_path = os.path.join(images_dir, 'test')img_val_path = os.path.join(images_dir, 'val')label_train_path = os.path.join(labels_dir, 'train')label_test_path = os.path.join(labels_dir, 'test')label_val_path = os.path.join(labels_dir, 'val')mkdir(images_dir)mkdir(labels_dir)mkdir(img_train_path)mkdir(img_test_path)mkdir(img_val_path)mkdir(label_train_path)mkdir(label_test_path)mkdir(label_val_path)# 数据集划分比例,训练集80%,验证集10%,测试集10%,按需修改train_percent = 0.8val_percent = 0.1test_percent = 0.1total_txt = os.listdir(txt_dir)num_txt = len(total_txt)list_all_txt = range(num_txt)  # 范围 range(0, num)num_train = int(num_txt * train_percent)num_val = int(num_txt * val_percent)num_test = num_txt - num_train - num_valtrain = random.sample(list_all_txt, num_train)# 在全部数据集中取出trainval_test = [i for i in list_all_txt if not i in train]# 再从val_test取出num_val个元素,val_test剩下的元素就是testval = random.sample(val_test, num_val)print("训练集数目:{}, 验证集数目:{}, 测试集数目:{}".format(len(train), len(val), len(val_test) - len(val)))for i in list_all_txt:name = total_txt[i][:-4]srcImage = os.path.join(image_dir, name + '.jpg')srcLabel = os.path.join(txt_dir, name + '.txt')if i in train:dst_train_Image = os.path.join(img_train_path, name + '.jpg')dst_train_Label = os.path.join(label_train_path, name + '.txt')shutil.copyfile(srcImage, dst_train_Image)shutil.copyfile(srcLabel, dst_train_Label)elif i in val:dst_val_Image = os.path.join(img_val_path, name + '.jpg')dst_val_Label = os.path.join(label_val_path, name + '.txt')shutil.copyfile(srcImage, dst_val_Image)shutil.copyfile(srcLabel, dst_val_Label)else:dst_test_Image = os.path.join(img_test_path, name + '.jpg')dst_test_Label = os.path.join(label_test_path, name + '.txt')shutil.copyfile(srcImage, dst_test_Image)shutil.copyfile(srcLabel, dst_test_Label)if __name__ == "__main__":parser = argparse.ArgumentParser(description='Convert LabelMe JSON to YOLO TXT and split datasets')parser.add_argument('--json-folder', type=str, default=r'',help='LabelMe JSON folder path')parser.add_argument('--txt-folder', type=str, default=r'',help='YOLO TXT save path')parser.add_argument('--class-map', default={"自己的类别": 0}, type=dict,help='Class name mapping dictionary (e.g. {"person": 0})')parser.add_argument('--images-dir', type=str, default=r'', help='images path dir')parser.add_argument('--save-dir', default=r'', type=str, help='save dir')args = parser.parse_args()json_folder = args.json_foldertxt_folder = args.txt_folderclass_map = args.class_mapimage_dir = args.images_dirsave_dir = args.save_dir# 转换LabelMe JSON到YOLO TXTbatch_convert_labelme_to_yolo(json_folder, txt_folder, class_map)# 划分数据集main(image_dir, txt_folder, save_dir)

http://www.dtcms.com/wzjs/319668.html

相关文章:

  • 做网站用小型机或服务器googleseo优化
  • 企业网站建设不够完善产品推广策划方案怎么做
  • 网站建设现在市场大不大东莞网站制作模板
  • 专做定制旅游网站有哪些郑州网站建设推广有限公司
  • 国内用react做的网站免费创建属于自己的网站
  • 邢台做移动网站公司电话小广告网站
  • 做的好的响应式网站网站关键词排名优化价格
  • 网站建设客户合同3天引流800个人技巧
  • 做网站推销话术生意参谋指数在线转换
  • 怎样在网上建网站做电商生意信阳seo优化
  • 上海中房建筑设计有限公司北京官网优化公司
  • 百度网址大全官网站长工具seo综合查询下载
  • 做网站教程pdf郑州计算机培训机构哪个最好
  • 邢台网络推广网站优化外包公司
  • 什么软件 做短视频网站软广告经典案例
  • 在淘宝做网站可以改域名吗怎么免费自己做推广
  • 网络营销的主要内容包括临沂seo推广外包
  • 公司网站百度推广手机百度登录入口
  • 广州网站建设流程西安网络推广外包公司
  • 网站开发兼容性网站快速上排名方法
  • 网站设计师和网页设计师近三天的国内外大事
  • 淘宝客如何做网站网站怎么优化推荐
  • 国内个人网站设计欣赏seo点击排名器
  • 营销软件站品牌广告视频
  • 微信公众号怎么创建要多少钱seo推广软件排名
  • b2b商务平台网站有哪些关键词排名seo
  • 做网站颜色类型是啥营销策划方案
  • 网站建设流程图visioseo刷关键词排名软件
  • 南宁模板建站平台泰安seo培训
  • 网站实现多语言怎么找当地的地推团队