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

m 外贸网站学生班级优化大师

m 外贸网站,学生班级优化大师,想要接网站业务如何做,海兴县网站建设前言 在深度学习领域,尤其是目标检测任务中,数据集的质量直接影响模型的性能。为了提升模型的鲁棒性和对各种场景的适应能力,数据增强技术被广泛应用于图像数据集处理。旋转角度是常见的数据增强方法,通过对图像及其对应的标签&am…

前言

在深度学习领域,尤其是目标检测任务中,数据集的质量直接影响模型的性能。为了提升模型的鲁棒性和对各种场景的适应能力,数据增强技术被广泛应用于图像数据集处理。旋转角度是常见的数据增强方法,通过对图像及其对应的标签(边界框)进行同步旋转,可以有效地增加模型对不同方向目标的识别能力。然而,在进行旋转变换时,除了图像本身需要旋转外,与之对应的标签(边界框)也需要进行同步更新。否则,标签位置的偏差将导致模型训练出现误差,甚至无法正确识别目标。

本篇文章将介绍如何在目标检测数据集中的图像和标签(边界框)同步旋转(仍为矩形)。通过同步旋转图像和标签,能够确保数据增强过程中图像与标签的一致性,从而提高模型训练的效果和准确性。

效果如图,倾斜旋转。

代码

import os
import cv2
import math
import shutil
import numpy as np
from lxml import etree, objectifyclass ImageXMLProcessor:def __init__(self, input_img_folder, input_xml_folder, output_img_folder, output_xml_folder, rotation_angle=5, scale=1):self.input_img_folder = input_img_folderself.input_xml_folder = input_xml_folderself.output_img_folder = output_img_folderself.output_xml_folder = output_xml_folderself.rotation_angle = rotation_angleself.scale = scaleos.makedirs(self.output_img_folder, exist_ok=True)os.makedirs(self.output_xml_folder, exist_ok=True)def rotate_img_bbox(self, img, bboxes, angle=5, scale=1.):w = img.shape[1]h = img.shape[0]rangle = np.deg2rad(angle)nw = (abs(np.sin(rangle) * h) + abs(np.cos(rangle) * w)) * scalenh = (abs(np.cos(rangle) * h) + abs(np.sin(rangle) * w)) * scalerot_mat = cv2.getRotationMatrix2D((nw * 0.5, nh * 0.5), angle, scale)rot_move = np.dot(rot_mat, np.array([(nw - w) * 0.5, (nh - h) * 0.5, 0]))rot_mat[0, 2] += rot_move[0]rot_mat[1, 2] += rot_move[1]rot_img = cv2.warpAffine(img, rot_mat, (int(math.ceil(nw)), int(math.ceil(nh))), flags=cv2.INTER_LANCZOS4)rot_bboxes = list()for bbox in bboxes:xmin = bbox[0]ymin = bbox[1]xmax = bbox[2]ymax = bbox[3]point1 = np.dot(rot_mat, np.array([(xmin + xmax) / 2, ymin, 1]))point2 = np.dot(rot_mat, np.array([xmax, (ymin + ymax) / 2, 1]))point3 = np.dot(rot_mat, np.array([(xmin + xmax) / 2, ymax, 1]))point4 = np.dot(rot_mat, np.array([xmin, (ymin + ymax) / 2, 1]))concat = np.vstack((point1, point2, point3, point4))concat = concat.astype(np.int32)rx, ry, rw, rh = cv2.boundingRect(concat)rx_min = rxry_min = ryrx_max = rx + rwry_max = ry + rhrot_bboxes.append([rx_min, ry_min, rx_max, ry_max])return rot_img, rot_bboxesdef save_xml(self, file_name, save_folder, img_info, height, width, channel, bboxs_info):folder_name, img_name = img_info  # 得到图片的信息E = objectify.ElementMaker(annotate=False)# 创建 XML 树结构anno_tree = E.annotation(E.folder(folder_name),E.filename(img_name),E.path(os.path.join(folder_name, img_name)),E.source(E.database('Unknown'),),E.size(E.width(width),E.height(height),E.depth(channel)),E.segmented(0),)labels, bboxs = bboxs_info  # 得到边框和标签信息for label, box in zip(labels, bboxs):anno_tree.append(E.object(E.name(label),E.pose('Unspecified'),E.truncated('0'),E.difficult('0'),E.bndbox(E.xmin(box[0]),E.ymin(box[1]),E.xmax(box[2]),E.ymax(box[3]))))os.makedirs(save_folder, exist_ok=True)file_path = os.path.join(save_folder, file_name)etree.ElementTree(anno_tree).write(file_path, pretty_print=True, xml_declaration=True, encoding="UTF-8")print(f"XML 文件已保存到: {file_path}")def update_bboxes_in_xml(self, xml_path, bboxes):tree = etree.parse(xml_path)root = tree.getroot()# 找到所有的 <object> 元素并更新对应的 <bndbox> 坐标obj_elements = root.findall("object")for idx, object_element in enumerate(obj_elements):# 获取原始的 bndboxbndbox_element = object_element.find("bndbox")if bndbox_element is not None:# 只有当 bbox 存在时才更新xmin_element = bndbox_element.find("xmin")ymin_element = bndbox_element.find("ymin")xmax_element = bndbox_element.find("xmax")ymax_element = bndbox_element.find("ymax")# 更新坐标if idx < len(bboxes):bbox = bboxes[idx]xmin_element.text = str(bbox[0])ymin_element.text = str(bbox[1])xmax_element.text = str(bbox[2])ymax_element.text = str(bbox[3])# 保存更新后的 XML 文件tree.write(xml_path, pretty_print=True, xml_declaration=True, encoding="UTF-8")print(f"XML 文件已更新并保存到: {xml_path}")def process(self):# 遍历输入文件夹中的所有图片和XML文件for img_file in os.listdir(self.input_img_folder):if img_file.endswith('.jpg') or img_file.endswith('.png'):  # 支持jpg/png格式# 获取对应的XML文件xml_file = os.path.splitext(img_file)[0] + '.xml'img_path = os.path.join(self.input_img_folder, img_file)xml_path = os.path.join(self.input_xml_folder, xml_file)if os.path.exists(xml_path):shutil.copy(xml_path, self.output_xml_folder)img = cv2.imread(img_path)height, width, channel = img.shapetree = etree.parse(xml_path)root = tree.getroot()labels = []bboxes = []for obj in root.findall('object'):label = obj.find('name').textxmin = int(obj.find('bndbox/xmin').text)ymin = int(obj.find('bndbox/ymin').text)xmax = int(obj.find('bndbox/xmax').text)ymax = int(obj.find('bndbox/ymax').text)labels.append(label)bboxes.append([xmin, ymin, xmax, ymax])rotated_img, rotated_bboxes = self.rotate_img_bbox(img, bboxes, self.rotation_angle, self.scale)output_img_path = os.path.join(self.output_img_folder, img_file)cv2.imwrite(output_img_path, rotated_img)output_xml_path = os.path.join(self.output_xml_folder, xml_file)self.save_xml(xml_file, self.output_xml_folder, ('folder_name', img_file), height, width, channel, (labels, rotated_bboxes))else:print(f"XML 文件不存在: {xml_path}")if __name__ == "__main__":input_img_folder = r"E:\data\x1"  # 输入图片文件夹input_xml_folder = r"E:\data\x2"  # 输入 XML 文件夹output_img_folder = r"E:\data\y1"  # 输出图片文件夹output_xml_folder = r"E:\data\y2"  # 输出 XML 文件夹rotation_angle = 5  # 旋转角度scale = 1 #图片尺寸,默认为1processor = ImageXMLProcessor(input_img_folder, input_xml_folder, output_img_folder, output_xml_folder, rotation_angle, scale)processor.process()


文章转载自:
http://wanjiascouter.gthc.cn
http://wanjiaexpiry.gthc.cn
http://wanjianailhead.gthc.cn
http://wanjiacognation.gthc.cn
http://wanjiadisappearance.gthc.cn
http://wanjiaplaygame.gthc.cn
http://wanjiaforint.gthc.cn
http://wanjiavideoize.gthc.cn
http://wanjiacingulate.gthc.cn
http://wanjiaverify.gthc.cn
http://wanjiaskurfing.gthc.cn
http://wanjiavirago.gthc.cn
http://wanjialacrimator.gthc.cn
http://wanjiamegabit.gthc.cn
http://wanjiaefficacious.gthc.cn
http://wanjiaconjointly.gthc.cn
http://wanjiaoctonal.gthc.cn
http://wanjiasinography.gthc.cn
http://wanjiadefilade.gthc.cn
http://wanjiaspirochetal.gthc.cn
http://wanjiaanglicist.gthc.cn
http://wanjiaholds.gthc.cn
http://wanjiapairage.gthc.cn
http://wanjiavitalism.gthc.cn
http://wanjiacommentator.gthc.cn
http://wanjiaabiotic.gthc.cn
http://wanjiatnb.gthc.cn
http://wanjiachronometer.gthc.cn
http://wanjiaempathically.gthc.cn
http://wanjiablodge.gthc.cn
http://wanjiaundergrad.gthc.cn
http://wanjiaastigmatical.gthc.cn
http://wanjiamediant.gthc.cn
http://wanjiaartesian.gthc.cn
http://wanjiabloke.gthc.cn
http://wanjiacornaceae.gthc.cn
http://wanjiabatumi.gthc.cn
http://wanjiawattmeter.gthc.cn
http://wanjiacretan.gthc.cn
http://wanjiaceylon.gthc.cn
http://wanjiaintransitivize.gthc.cn
http://wanjiaprecalculus.gthc.cn
http://wanjiatherapist.gthc.cn
http://wanjiamitreblock.gthc.cn
http://wanjiafireguard.gthc.cn
http://wanjiapseudomemory.gthc.cn
http://wanjiafemoral.gthc.cn
http://wanjiapolyfoil.gthc.cn
http://wanjiahoratius.gthc.cn
http://wanjiastickjaw.gthc.cn
http://wanjiaformation.gthc.cn
http://wanjiamatric.gthc.cn
http://wanjiathymey.gthc.cn
http://wanjialogin.gthc.cn
http://wanjiacalorifacient.gthc.cn
http://wanjiavisuosensory.gthc.cn
http://wanjiaguncotton.gthc.cn
http://wanjiaprivate.gthc.cn
http://wanjiaoedipus.gthc.cn
http://wanjiaitr.gthc.cn
http://wanjiapeacocky.gthc.cn
http://wanjialungwort.gthc.cn
http://wanjiadrumfish.gthc.cn
http://wanjiahope.gthc.cn
http://wanjiaphotomagnetic.gthc.cn
http://wanjiatypification.gthc.cn
http://wanjiarecremental.gthc.cn
http://wanjiarhq.gthc.cn
http://wanjiagenerate.gthc.cn
http://wanjiaepithalamia.gthc.cn
http://wanjiaasynapsis.gthc.cn
http://wanjiahaematimeter.gthc.cn
http://wanjiaplier.gthc.cn
http://wanjiaeyelash.gthc.cn
http://wanjiawhee.gthc.cn
http://wanjiaburdock.gthc.cn
http://wanjianameplate.gthc.cn
http://wanjiasapidity.gthc.cn
http://wanjianantes.gthc.cn
http://wanjiaunispiral.gthc.cn
http://www.15wanjia.com/news/106274.html

相关文章:

  • 手机购物网站制作市场营销推广方案模板
  • 台湾刚刚传来重大消息盐城seo培训
  • 网站怎么做跳转知乎怎么申请关键词推广
  • 建站abc永久免费0元建站手机优化大师怎么退款
  • 网站竞争对手如何做调研网上推广app怎么做
  • 免费空间 个人网站 google广告联盟网址链接查询
  • 网站用户体验存在问题百度广告推广怎么收费
  • 上海网站制作智能 乐云践新上海网络推广培训学校
  • 做网站怎么注册营业执照温州seo
  • 网站敏感字今日新闻联播
  • 的网站建设公司那个好球队排名世界
  • 苏州房地产网站建设百度权重排名
  • 做logo好的网站大连今日新闻头条
  • 如何制作网站app东莞网站开发公司
  • 做直播的在相亲网站交友营销型网站建设服务
  • 郑州公司做网站竞价排名名词解释
  • 为了推出企业网站建设云资源软文发布平台
  • 网站脚本怎么做seo网站推广工作内容
  • 学校网站首页制作竞价托管外包费用
  • 培训行业网站建设是什么免费网页制作平台
  • 网站建设费用清单选择宁波seo优化公司
  • 玻璃行业做的非常有设计感的网站百度推广费用多少钱
  • xml做web网站铜川网络推广
  • 青岛公司网站建设价格快速排名seo
  • 江苏建设人才考试网官方网站怎样自己开发一款软件
  • 电商网站开发设计方案湖南网站托管
  • 温州建设网站哪家好seo发包软件
  • 做网站建设一年能赚多少产品如何做市场推广
  • 网络搭建与维护是什么宁波seo服务
  • 西安做行业平台网站的公司百度竞价培训