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

做外贸单网上都做的那些网站北京网站建设运营

做外贸单网上都做的那些网站,北京网站建设运营,公司策划推广,东莞市技师学院01 labelme json 转 txt(w_convert_labelme_to_yolo.py) #WT 将labelme json标签格式转换为YOLO txt格式 # 导入所需模块 import cv2 # OpenCV用于图像处理 import os # 操作系统路径管理 import json # JSON文件解析 import glob # 文件通配符搜索…

01 labelme json 转 txt(w_convert_labelme_to_yolo.py)

#WT 将labelme json标签格式转换为YOLO txt格式
# 导入所需模块
import cv2  # OpenCV用于图像处理
import os   # 操作系统路径管理
import json  # JSON文件解析
import glob  # 文件通配符搜索
import numpy as np  # 数值计算def convert_json_label_to_yolov_seg_label():# 设置JSON文件路径(本地标注数据目录)json_path = r"data"  # 本地json路径# 获取所有JSON文件列表json_files = glob.glob(json_path + "/*.json")print(json_files)# 创建输出目录(YOLO格式标签存储路径)output_folder = "txt2"  # txt存放路径if not os.path.exists(output_folder):os.makedirs(output_folder)  # 递归创建目录# 遍历处理每个JSON文件for json_file in json_files:print(json_file)# 读取并解析JSON文件with open(json_file, 'r') as f:json_info = json.load(f)# 加载对应图像获取尺寸img = cv2.imread(os.path.join(json_path, json_info["imagePath"]))height, width, _ = img.shapenp_w_h = np.array([[width, height]], np.int32)  # 转换为numpy数组便于计算# 构建输出文件路径(保持与JSON同名,后缀改为.txt)txt_file = os.path.join(output_folder, os.path.basename(json_file).replace(".json", ".txt"))# 写入YOLO格式标签with open(txt_file, "w") as f:for point_json in json_info["shapes"]:txt_content = ""# 转换坐标点为numpy数组np_points = np.array(point_json["points"], np.int32)# 归一化坐标(除以图像宽高)norm_points = np_points / np_w_h# 转换为列表并格式化输出norm_points_list = norm_points.tolist()# 拼接类别标签和坐标点(YOLO格式要求:类别+归一化坐标)txt_content += "0 " + " ".join([" ".join([str(cell[0]), str(cell[1])]) for cell in norm_points_list]) + "\n"f.write(txt_content)if __name__ == "__main__":# 执行转换函数convert_json_label_to_yolov_seg_label()

02 检查图片和标签文件是否对应python脚本(w_check_img_to_label.py) 

#WT:检查图片和标签是否匹配2025-04-06
import osdef get_file_names(directory):file_names = os.listdir(directory)return file_nameslabels_path = 'train/labels'
labels_names_list = get_file_names(labels_path)
images_path = 'train/images'
images_names_list = get_file_names(images_path)
for i in range(len(images_names_list)):filename = images_names_list[i][0:-3] + 'txt'if filename in labels_names_list:continueelse:print(f"【{filename}】标签不存在!")continueprint("检查结束")

03 从labeme标注的json文件提取标签label类别class脚本(w_extract_json_label.py)

#WT:从labeme标注的json文件提取标签label类别class
#WT 将labelme json标签格式转换为YOLO txt格式
# 导入所需模块
import os   # 操作系统路径管理
import json  # JSON文件解析
import glob  # 文件通配符搜索def extract_labelme_json_label():# 设置JSON文件路径(本地标注数据目录)json_path = r"data"  # 本地json路径# 获取所有JSON文件列表json_files = glob.glob(json_path + "/*.json")#print(json_files)label_class_list = []# 遍历处理每个JSON文件for json_file in json_files:print(json_file)# 读取并解析JSON文件with open(json_file, 'r') as f:json_info = json.load(f)json_shapes = json_info["shapes"]#print(json_shapes,type(json_shapes))for i in range(len(json_shapes)):#print(json_shapes[i]["label"])if json_shapes[i]["label"] in label_class_list:continueelse:label_class_list.append(json_shapes[i]["label"])print("完成!")print(label_class_list)if __name__ == "__main__":# 执行函数extract_labelme_json_label()

04 重置图片格式大小【脚本存在一些问题】(w_resize_train_images.py)

#重置图片格式大小
import os
import cv2
import numpy as np# 定义letterbox函数,用于对图像进行缩放和填充,使其适应指定尺寸
def letterbox(img, new_shape=(640, 640), color=(255, 255, 255), auto=False, scale_fill=False, scale_up=False, stride=32):shape = img.shape[:2]  # 获取图像的原始高度和宽度if isinstance(new_shape, int):new_shape = (new_shape, new_shape)  # 如果new_shape是整数,则将其转换为方形尺寸# 计算缩放比例r,确保图像不会拉伸变形r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])if not scale_up:r = min(r, 1.0)  # 如果scale_up为False,则缩放比例不大于1,避免放大ratio = r, r  # 记录宽高的缩放比例new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))  # 根据比例计算缩放后的尺寸dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  # 计算需要填充的宽度和高度if auto:dw, dh = np.mod(dw, stride), np.mod(dh, stride)  # 如果auto为True,将填充尺寸调整为步长的倍数elif scale_fill:dw, dh = 0.0, 0.0  # 如果scale_fill为True,则不进行填充,直接拉伸到目标尺寸new_unpad = (new_shape[1], new_shape[0])ratio = new_shape[1] / shape[1], new_shape[0] / shape[0]  # 重新计算缩放比例dw /= 2  # 左右填充的宽度dh /= 2  # 上下填充的高度if shape[::-1] != new_unpad:  # 如果原始尺寸和缩放后的尺寸不同,进行图像缩放img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)  # 线性插值进行图像缩放top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))  # 上下填充像素数left, right = int(round(dw - 0.1)), int(round(dw + 0.1))  # 左右填充像素数img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # 使用指定颜色填充return img, ratio, (dw, dh)  # 返回处理后的图像、缩放比例以及填充的宽高# 保存经过letterbox处理的图像
def save_letterboxed_image(image_bytes, output_filename, new_shape=(1280, 1280)):im0 = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), cv2.IMREAD_COLOR)  # 从字节数据解码出图像if im0 is None:raise ValueError("Failed to decode image from image_bytes")  # 如果图像解码失败,抛出异常img, _, _ = letterbox(im0, new_shape=new_shape)  # 对图像进行letterbox处理# 保存处理后的图像cv2.imwrite(output_filename, img)  # 将处理后的图像写入文件print(f"Saved letterboxed image to {output_filename}")  # 输出保存成功的信息# 处理文件夹中的所有图像
def process_images_in_folder(folder_path, output_folder, new_shape=(1280, 1280)):if not os.path.exists(output_folder):os.makedirs(output_folder)  # 如果输出文件夹不存在,则创建# 遍历文件夹中的所有图像文件for filename in os.listdir(folder_path):if filename.endswith(('.jpg', '.png', '.jpeg')):  # 仅处理特定格式的图像文件image_path = os.path.join(folder_path, filename)  # 获取图像的完整路径with open(image_path, 'rb') as f:image_bytes = f.read()  # 读取图像文件的字节数据output_filename = os.path.join(output_folder, filename)  # 构建输出文件的完整路径save_letterboxed_image(image_bytes, output_filename, new_shape=new_shape)  # 保存处理后的图像# 使用示例
if __name__ == "__main__":folder_path = r'train/images'  # 替换为你的图片文件夹路径output_folder = 'resizeimg'  # 替换为保存处理后图片的文件夹路径process_images_in_folder(folder_path, output_folder, new_shape=(640, 640))  # 处理文件夹中的图像

05 class.txt

desquamatehollow
cracking

06 data.yaml

train: ../train/images
val: ../valid/images
test: ../test/imagesnc: 3
names: ['desquamate', 'hollow', 'cracking']

代码参考:

Labelme的安装与使用教程_labelme安装及使用教程-CSDN博客 

YOLO11 图像缩放 | 图像填充 | 自适应不同尺寸的图片_yolo训练 图片不同大小-CSDN博客

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

相关文章:

  • 男女做暧暧视频免费网站中国十大企业管理培训机构
  • 网易云wordpress代码哈尔滨seo服务
  • 网站便民服务平台怎么做成都专门做网络推广的公司
  • 深圳欧啦啦网站建设漯河网站seo
  • php 网站开发框架ap世界球队实力排名
  • 引流推广广告怎么写郑州网站建设方案优化
  • 网站开发模板下载排名第一的手机清理软件
  • 凤凰军事新闻头条推广资源seo
  • 网站模板首页百度新闻发布
  • 假淘宝网站怎么做网络营销的具体形式种类
  • 做简历网站 知乎百度搜索 手机
  • 购买了域名之后怎么做网站济南优化网站的哪家好
  • 响应式网站有什么区别新媒体营销案例
  • 中信建设有限责任公司陶扬品牌seo是什么
  • 深圳vi设计多少钱上海外贸网站seo
  • 玉溪网络推广 网站建设网络推广外包流程
  • 在万网上域名了怎么做网站企业培训心得
  • 基于php网站开发步骤网络推广公司专业网络
  • 南京cms建站系统上海短视频推广
  • 企业建站公司方案河南网站推广那家好
  • 国内自建的海淘网站seo资源
  • 怎么用php做网站搜索引擎关键词排名
  • 无锡电子商务网站建设公司seo网络推广公司
  • 大题小做网站无代码网站开发平台
  • 南昌定制网站公司推广策划
  • 制作旅游网站简单广州seo优化电话
  • 网站界面设计的步骤东莞网站制作外包
  • 大石桥网站webview播放视频
  • 网站建设方案书 内容管理制度广州网络推广服务商
  • 如何在淘宝上接单网站建设seo推广优化方案