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

关于我们网站模板会员制营销方案

关于我们网站模板,会员制营销方案,网站域名备案和做网站,网站开发设计招聘目录 1、labelme安装和打开2、边界框和关键点标注3、将lamelme的json格式转成yolo可以使用的txt格式4、将数据和标签按照9比1分为训练集和测试集 1、labelme安装和打开 在python3.9及以上环境中安装labelme,labelme要用到pyqt,所以在使用labelme之前要安…

目录

  • 1、labelme安装和打开
  • 2、边界框和关键点标注
  • 3、将lamelme的json格式转成yolo可以使用的txt格式
  • 4、将数据和标签按照9比1分为训练集和测试集

1、labelme安装和打开

在python3.9及以上环境中安装labelme,labelme要用到pyqt,所以在使用labelme之前要安装pyqt。

pip install pyqt
pip install labelme

进入存放标注图像文件夹的目录,提前设置要标注的文件夹、具体标签以及存放的文件夹。其中image是图像文件夹,labels是要存放标签的文件夹,labels.txt是提前设置的标签。

labelme image --output labels --labels labels.txt

打开的界面如下图所示
在这里插入图片描述

2、边界框和关键点标注

在标注时,要将目标框和对应的关键点分为一组,关键点也类似
在这里插入图片描述
在这里插入图片描述
标注完后记得点击上面save按钮

3、将lamelme的json格式转成yolo可以使用的txt格式

转换代码如下,可以将json格式根据group_id将目标和关键点对应起来,然后转成txt格式,每行格式为<class_id> <x_center> <y_center> <kp1_x> <kp1_y> <kp1_vis> <kp2_x> <kp2_y> <kp2_vis> … <kpN_x> <kpN_y> <kpN_vis>

import os
import json
from PIL import Imagedef convert_labelme_to_yolo_keypoints(json_path, image_dir, output_dir, class_id=0, num_keypoints=7):os.makedirs(output_dir, exist_ok=True)with open(json_path, 'r') as f:data = json.load(f)image_file = data.get("imagePath", os.path.basename(json_path).replace(".json", ".jpg"))image_path = os.path.join(image_dir, image_file)image = Image.open(image_path)width, height = image.sizeshapes = data['shapes']objects = {}  # key: group_id, value: {'bbox': [...], 'keypoints': {}}for shape in shapes:g_id = shape.get("group_id")if g_id is None:continuelabel = shape['label']shape_type = shape['shape_type']pts = shape['points']if g_id not in objects:objects[g_id] = {'bbox': None, 'keypoints': {}}if shape_type == "rectangle" and label == "bolt":x1, y1 = pts[0]x2, y2 = pts[1]objects[g_id]['bbox'] = [min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2)]elif shape_type == "point":try:kp_id = int(label)objects[g_id]['keypoints'][kp_id] = pts[0]except ValueError:pass  # skip if label is not a number# Write YOLO-style .txttxt_name = os.path.basename(json_path).replace('.json', '.txt')out_path = os.path.join(output_dir, txt_name)with open(out_path, 'w') as f:for obj in objects.values():if obj['bbox'] is None:continue  # skip if no boxx1, y1, x2, y2 = obj['bbox']x_center = (x1 + x2) / 2 / widthy_center = (y1 + y2) / 2 / heightbox_w = (x2 - x1) / widthbox_h = (y2 - y1) / heightkeypoints = []for i in range(1, num_keypoints + 1):if i in obj['keypoints']:x, y = obj['keypoints'][i]keypoints += [x / width, y / height, 2]else:keypoints += [0.0, 0.0, 0]  # 不存在该关键点line = f"{class_id} {x_center:.6f} {y_center:.6f} {box_w:.6f} {box_h:.6f} " + \" ".join([f"{kp:.6f}" if isinstance(kp, float) else str(kp) for kp in keypoints])f.write(line + '\n')print(f"✅ 转换完成: {out_path}")json_dir = r"path\to\labels"
image_dir = r"path\to\images"
output_dir = r"path\to\labels"for file in os.listdir(json_dir):if file.endswith(".json"):json_path = os.path.join(json_dir, file)convert_labelme_to_yolo_keypoints(json_path, image_dir, output_dir)

4、将数据和标签按照9比1分为训练集和测试集

代码如下

import os
import random
import shutil# 原始文件夹路径
images_dir = 'path\to\images'
labels_dir = 'path\to\labels'# 目标文件夹路径
train_image_dir = r'path\to\images\train'
val_image_dir = r'path\to\images\val'
train_label_dir = r'path\to\labels\train'
val_label_dir = r'path\to\labels\val'# 创建输出文件夹
os.makedirs(train_image_dir, exist_ok=True)
os.makedirs(val_image_dir, exist_ok=True)
os.makedirs(train_label_dir, exist_ok=True)
os.makedirs(val_label_dir, exist_ok=True)# 获取图像文件列表
image_files = [f for f in os.listdir(images_dir) if f.lower().endswith(('.jpg', '.jpeg', '.png'))]# 打乱并划分
random.shuffle(image_files)
split_idx = int(0.9 * len(image_files))
train_files = image_files[:split_idx]
val_files = image_files[split_idx:]# 拷贝函数
def copy_split(file_list, img_src, lbl_src, img_dst, lbl_dst):for fname in file_list:# 拷贝图像shutil.copy(os.path.join(img_src, fname), os.path.join(img_dst, fname))# 拷贝标签label_name = os.path.splitext(fname)[0] + '.txt'label_src_path = os.path.join(lbl_src, label_name)label_dst_path = os.path.join(lbl_dst, label_name)if os.path.exists(label_src_path):shutil.copy(label_src_path, label_dst_path)else:print(f"⚠️ 缺少标签: {label_src_path}")# 执行拷贝
copy_split(train_files, images_dir, labels_dir, train_image_dir, train_label_dir)
copy_split(val_files, images_dir, labels_dir, val_image_dir, val_label_dir)print("✅ 划分完成!图像和标签已按 9:1 存放到 images/train, images/val 和 labels/train, labels/val。")
http://www.dtcms.com/a/519196.html

相关文章:

  • 永春网站建设房地产销售工作总结
  • seo推广软件排行榜保定网站seo技术
  • 成都平台网站建设公司WordPress背景图片自适应
  • 自己做的网站打开速度慢网站项目建设策划方案
  • wordpress站点统计插件哈尔滨香坊
  • 网站制作公司制作网站的流程是怎样的呢服装网站建设的技术可行性
  • 百度网盘0基础网站开发教程旅游网络推广怎么做
  • 人工智能网站应怎么做网站模板视频教程
  • 网站制作性价比哪家好邯郸旅游
  • wordpress数据录入平台seo 优化公司
  • 团购鲜花的网站建设深圳网站开发
  • 门户网站做等保需要备案哪些柳传志 潘石屹做水果网站
  • 服装厂做1688网站效果好不好最好的电商平台
  • 公司建设网站的申请网上申报食品经营许可证流程
  • 公司网站建设技术一个女装店网站建设的策划模板
  • 公众号做 视频网站wordpress不好用
  • 网站建设人力调配范文网站服务器 数据库服务器
  • 网站正在建设中 免费可信赖的赣州网站建设
  • 墨西哥网站后缀商业网站的建设
  • 西安手机商城网站建设西安app开发软件定制
  • 有做软件的网站有哪些域名解析 网站
  • cn域名注册网站网站建设状况
  • seo针对网站做策划长沙网红打卡点
  • 那个网站有题做沈阳行业网站
  • 武进常州做网站宁波seo怎么做优化公司
  • 山东省建筑住房和城乡建设厅网站长沙免费网站排名
  • 网站费用怎么做帐建设英文网站费用
  • 网站建设框架构建中交路桥建设有限公司电话
  • 自己想做网站上海网络营销广告单位
  • 影视公司网站模板wordpress 视频分集