训练数据集的图片尺寸修改集标注标签修改

在针对同一角度拍摄的多张已完成标注图片使用深度学习的方式对数据集进行训练时,时长会出现感兴区域过小的情况,固定角度拍摄所得到的图片数据集往往存在部分位置区域可以直接去除,不用参与到模型的训练中,同时可以提高训练数据集的感兴区域,如下图,该图为SODIC2021道路路面病害智能分析算法中的一张图,在同一角度拍摄的图片有很多,但是标注基本只在图片的下半部进行标注,因此可以通过裁剪上半部图片的方式提高数据集在训练过程中的感兴区域。该处只是寻找一个合适的举例。

首先是对数据集中的图片进行查找,只在同一个文件夹下,可以直接用

os.listdir(path)

如果在一个文件夹内存在多个数据集,可以通过以下递归代码进行查找:

def imgs_seek(start_path):photo_list = []  # 图片列表def imgs_seek():os.chdir(start_path)  # 查找图片的途径items = os.listdir(os.curdir)  # 该路径下所有文件photo_type = ['.jpg', '.png', 'jpeg']  # 添加查找图片的格式   注意加.  如'.JPG'for each in items:if os.path.splitext(each)[1].lower() in photo_type:photo_list.append(os.getcwd() + os.sep + each)if os.path.isdir(each):imgs_seek(each)  # 递归函数查找下一目录下的文件os.chdir(os.pardir)  # 返回上一层文件目录return photo_listimgs_seek()return photo_list

然后是对图片的区域提取,可以通过opencv直接对读取的图片进行矩阵分割

dst = img[heigh_strat:heigh_end, width_start:width_end]

通过函数对分割后的图片进行图片保存和新图片的尺寸输出,代码如下

def img_cut(img, img_save_path, heigh_strat, heigh_end, width_start, width_end):'''img:为opencv读取后的图像数据'''dst = img[heigh_strat:heigh_end, width_start:width_end]  #now_h, now_w, _ = dst.shapecv2.imwrite(img_save_path, dst)return now_h, now_w

对通过labelimg标注以YOLO方式标注生产的标签文件,可以通过以下代码进行标签位置的改变:

def txt_check(txt_path, save_txt_path, heigh_strat, heigh_end, width_start, width_end, heigh, width):'''txt_path:原始标签所在的位置save_txt_path:裁剪后生成的新的标签保存的位置heigh_strat, heigh_end, width_start, width_end:裁剪的范围heigh, width:原始图片的高和宽'''fp1 = open(txt_path, 'r+')for line in fp1.readlines():line_new = []line = line.split(' ')with open(save_txt_path, "a+") as fp2:old_heigh_strat, old_heigh_end = float(line[2]) * heigh, (float(line[2]) + float(line[4])) * heighnew_heigh_strat = old_heigh_strat - heigh_strat if old_heigh_strat > heigh_strat else 0   #新的图片框所在的像数点高度起始位置new_heigh_end = old_heigh_end - heigh_strat if old_heigh_end > heigh_strat else 0   #新的图片框所在的像数点高度结束位置line_new2 = str(float(new_heigh_strat) / (heigh_end - heigh_strat))line_new4 = float(new_heigh_end - new_heigh_strat) / (heigh_end - heigh_strat)if line_new4 < 0:line_new4 = '0'elif line_new4 < 1:line_new4 = str(line_new4)else:line_new4 = '1'old_width_strat, old_width_end = float(line[1]) * width, (float(line[1]) + float(line[3])) * widthnew_width_strat = old_width_strat - width_start if old_width_strat > width_start else 0    #新的图片框所在的像数点宽度起始位置new_width_end = old_width_end - width_start if old_width_end > width_start else 0    #新的图片框所在的像数点宽度结束位置line_new1 = str(float(new_width_strat) / (width_end - width_start))   line_new3 = float(new_width_end - new_width_strat) / (width_end - width_start)if line_new3 < 0:line_new3 = '0'elif line_new3 < 1:line_new3 = str(line_new4)else:line_new3 = '1'line_new.append(line[0])line_new.append(line_new1)line_new.append(line_new2)line_new.append(line_new3)line_new.append(line_new4)line_new = map(str, line_new)line_new = " ".join(line_new)if float(line_new3) > 0.001 and float(line_new4) > 0.001:fp2.write(line_new + '\n')fp2.close()

通过labelimg对pascalVOC类型的标注生产的xml文件,可通过以下代码对裁剪的图片进行标注更新:

import xml.etree.ElementTree as ET
def xml_check(now_h, now_w, xml_path, save_xml_path,  heigh_start, heigh_end, width_start, width_end):# 打开xml文档doc = ET.parse(xml_path)root = doc.getroot()size_root = root.find("size")size_root[0].text = str(now_w)size_root[1].text = str(now_h)for child in root.findall('object'):  # 找到图片中的所有框sub = child.find('bndbox')  # 找到框的标注值并进行读取#  宽 xminif int(sub[0].text) < width_start:sub[0].text = '0'elif int(sub[0].text) > width_end:sub[0].text = str(width_end)else:sub[0].text = str(int(sub[0].text) - width_start)#  高 yminif int(sub[1].text) < heigh_start:sub[1].text = '0'elif int(sub[1].text) > heigh_end:sub[1].text = str(heigh_end)else:sub[1].text = str(int(sub[1].text) - heigh_start)#  宽 xmaxif int(sub[2].text) < width_start:sub[2].text = '0'elif int(sub[2].text) > width_end:sub[2].text = str(width_end)else:sub[2].text = str(int(sub[2].text) - width_start)#  高 ymaxif int(sub[3].text) < heigh_start:sub[3].text = '0'elif int(sub[3].text) > heigh_end:sub[3].text = str(heigh_end)else:sub[3].text = str(int(sub[3].text) - heigh_start)if int(sub[2].text) - int(sub[0].text) < 5 or int(sub[3].text) - int(sub[1].text) < 5:root.remove(child)doc.write(save_xml_path)  # 保存修改

整体代码如下:

import cv2
import os
import random
import shutil
from shutil import copyfile
import xml.etree.ElementTree as ET
from decimal import Decimaldef xml_check(now_h, now_w, xml_path, save_xml_path,  heigh_start, heigh_end, width_start, width_end):# 打开xml文档doc = ET.parse(xml_path)root = doc.getroot()size_root = root.find("size")size_root[0].text = str(now_w)size_root[1].text = str(now_h)for child in root.findall('object'):  # 找到图片中的所有框sub = child.find('bndbox')  # 找到框的标注值并进行读取#  宽 xminif int(sub[0].text) < width_start:sub[0].text = '0'elif int(sub[0].text) > width_end:sub[0].text = str(width_end)else:sub[0].text = str(int(sub[0].text) - width_start)#  高 yminif int(sub[1].text) < heigh_start:sub[1].text = '0'elif int(sub[1].text) > heigh_end:sub[1].text = str(heigh_end)else:sub[1].text = str(int(sub[1].text) - heigh_start)#  宽 xmaxif int(sub[2].text) < width_start:sub[2].text = '0'elif int(sub[2].text) > width_end:sub[2].text = str(width_end)else:sub[2].text = str(int(sub[2].text) - width_start)#  高 ymaxif int(sub[3].text) < heigh_start:sub[3].text = '0'elif int(sub[3].text) > heigh_end:sub[3].text = str(heigh_end)else:sub[3].text = str(int(sub[3].text) - heigh_start)if int(sub[2].text) - int(sub[0].text) < 5 or int(sub[3].text) - int(sub[1].text) < 5:root.remove(child)doc.write(save_xml_path)  # 保存修改def rm_mkdir(path):if os.path.exists(path):shutil.rmtree(path)os.mkdir(path)def txt_check(txt_path, save_txt_path, heigh_strat, heigh_end, width_start, width_end, heigh, width):fp1 = open(txt_path, 'r+')for line in fp1.readlines():line_new = []line = line.split(' ')with open(save_txt_path, "a+") as fp2:old_heigh_strat, old_heigh_end = float(line[2]) * heigh, (float(line[2]) + float(line[4])) * heighnew_heigh_strat = old_heigh_strat - heigh_strat if old_heigh_strat > heigh_strat else 0   #新的图片框所在的像数点高度起始位置new_heigh_end = old_heigh_end - heigh_strat if old_heigh_end > heigh_strat else 0   #新的图片框所在的像数点高度结束位置line_new2 = str(float(new_heigh_strat) / (heigh_end - heigh_strat))line_new4 = float(new_heigh_end - new_heigh_strat) / (heigh_end - heigh_strat)if line_new4 < 0:line_new4 = '0'elif line_new4 < 1:line_new4 = str(line_new4)else:line_new4 = '1'old_width_strat, old_width_end = float(line[1]) * width, (float(line[1]) + float(line[3])) * widthnew_width_strat = old_width_strat - width_start if old_width_strat > width_start else 0    #新的图片框所在的像数点宽度起始位置new_width_end = old_width_end - width_start if old_width_end > width_start else 0    #新的图片框所在的像数点宽度结束位置line_new1 = str(float(new_width_strat) / (width_end - width_start))   line_new3 = float(new_width_end - new_width_strat) / (width_end - width_start)if line_new3 < 0:line_new3 = '0'elif line_new3 < 1:line_new3 = str(line_new4)else:line_new3 = '1'line_new.append(line[0])line_new.append(line_new1)line_new.append(line_new2)line_new.append(line_new3)line_new.append(line_new4)line_new = map(str, line_new)line_new = " ".join(line_new)if float(line_new3) > 0.001 and float(line_new4) > 0.001:fp2.write(line_new + '\n')fp2.close()def img_cut(img, img_save_path, heigh_strat, heigh_end, width_start, width_end):dst = img[heigh_strat:heigh_end, width_start:width_end]  #now_h, now_w, _ = dst.shapecv2.imwrite(img_save_path, dst)return now_h, now_wdef imgs_seek(start_path):photo_list = []  # 图片列表def imgs_seek(start_path):os.chdir(start_path)  # 查找图片的途径items = os.listdir(os.curdir)  # 该路径下所有文件photo_type = ['.jpg', '.JPG', '.JEPG', 'jepg']  # 添加查找图片的格式   注意加.  如'.JPG'for each in items:if os.path.splitext(each)[1] in photo_type:photo_list.append(os.getcwd() + os.sep + each)if os.path.isdir(each):imgs_seek(each)  # 递归函数查找下一目录下的文件os.chdir(os.pardir)  # 返回上一层文件目录return photo_listimgs_seek(start_path)return photo_listif __name__ == "__main__":imgs_path = r'D:\data\road_datas\train\images'       #原始图片路径                                             old_txt_path = r'D:\data\road_datas\train\all_txt'             #原始txt标签路径(yolo)old_xml_path = r'D:\data\road_datas\train\all_xml'            #原始xml标签路径(VOC)save_path = r'D:\Desktop\test_cut\save2'                     #保存路径                                 rm_mkdir(save_path)cut_imgs_save = os.path.join(save_path, 'images')           #保存路径中图片路径                                      labels_path = os.path.join(save_path, 'labels')           #保存路径中txt标签路径                                        annotations_path = os.path.join(save_path, 'annotations')   #保存路径中xml标签路径                                      heigh_strat, heigh_end, width_start, width_end = 400, 1000, 200, 1200    #图片裁剪尺寸,参考opencv  img[y0:y1, x0:x1]                         rm_mkdir(cut_imgs_save)rm_mkdir(labels_path)rm_mkdir(annotations_path)imgs_list = imgs_seek(imgs_path)img_num = len(imgs_list)set_percent = 1       #图片裁剪数量比例                                                                               random.seed(8)random.shuffle(imgs_list)cut_imgs_list = imgs_list[:int(img_num * set_percent)]origin_img_list = imgs_list[int(img_num * set_percent):]cut_list_num = []finish_num = 0origin_num = 0total_cut_num = len(cut_imgs_list)origin_total_num = img_num - total_cut_numprint("在%s文件夹下共找到%d个图片文件,共裁剪%d个文件"%(imgs_path, img_num, total_cut_num))#裁剪图片及相应的标签for cut_img_path in cut_imgs_list:img_id = (cut_img_path.split('.'))[0].split('\\')[-1]cut_list_num.append(img_id)cut_img_save_path = os.path.join(cut_imgs_save, img_id + '.jpg')img = cv2.imread(cut_img_path)h, w, _ = img.shapeh_s, h_e, w_s, w_e = heigh_strat, h, 0, w    #############n_h, n_w = img_cut(img, cut_img_save_path, h_s, h_e, w_s, w_e) old_laebl_path = os.path.join(old_txt_path, img_id + '.txt')save_label_path = os.path.join(labels_path, img_id + '.txt')old_xml_path1 = os.path.join(old_xml_path, img_id + '.xml')save_xml_path = os.path.join(annotations_path, img_id + '.xml')xml_check(n_h, n_w, old_xml_path1, save_xml_path, h_s, h_e, w_s, w_e)txt_check(old_laebl_path, save_label_path, h_s, h_e, w_s, w_e, h, w)finish_num += 1print("裁剪图片和标签已完成:%s/%s,总共为%s/%s" % (finish_num, total_cut_num, finish_num, img_num))#copy图片及标签for origin_img_path in origin_img_list:img_id = (origin_img_path.split('.'))[0].split('\\')[-1]origin_img_save_path = os.path.join(cut_imgs_save, img_id + '.jpg')old_laebl_path = os.path.join(old_txt_path, img_id + '.txt')old_xml_path2 = os.path.join(old_xml_path, img_id + '.xml')origin_label_save_path = os.path.join(labels_path, img_id + '.txt')origin_xml_save_path = os.path.join(annotations_path, img_id + '.xml')copyfile(origin_img_path, origin_img_save_path)copyfile(old_laebl_path, origin_label_save_path)copyfile(old_xml_path2, origin_xml_save_path)finish_num += 1origin_num += 1print("保存原图片和标签已完成:%s/%s,总共为%s/%s" % (origin_num, origin_total_num, finish_num, img_num))

裁剪后的标签及图片结果如下:

训练数据集的图片尺寸修改集标注标签修改相关推荐

  1. ps如何修改图片大小尺寸_如何查看图片尺寸,大小及如何修改图片尺寸—淘宝美工入门课03...

    上一篇文章给大家分享了一下豆芽常用字体,也提供了下载链接,今天和大家聊聊有关图片尺寸和图片大小相关的问题,主要包含以下这3个方面. 一,如何查看图片的尺寸(像素px) 二,如何改变图片的尺寸 三,了解 ...

  2. 淘宝店图片尺寸是多少?怎么修改淘宝图片的尺寸?

    随着网络的发展现在的电商平台越来越多,淘宝作为常用的电商平台也是大家最熟悉的,同时也是比较成熟的平台,许多人都会选择在淘宝上开自己的网店,而很多新手在装修上传图片资料问题上会遇到图片大小不合适的情况, ...

  3. 如何调整图片尺寸大小?电脑怎么修改照片大小?

    生活和工作时经常遇到图片尺寸大小不合适的情况,尤其是在上传时,需要改图片大小(https://www.yasuotu.com/size),那么如何修改照片大小呢?试试这款在线修改图片工具吧,快速修改图 ...

  4. CSDN写博客字体颜色,空格,大小标题,图片尺寸大小及居中等修改命令

    @CSDN写博客编辑器字体颜色,空格,大小标题等修改命令 1.字体颜色 红色(size可以根据实际大小进行设置,一般不超过7.将代码中"红色"换成你的语句即可) <font ...

  5. 训练数据集如何划分验证测试集?train/test(val/dev) set和交叉验证(cross validation)

    普通train/test set 直接将训练数据划分为两部分,一部分用来做训练train set,一部分用来固定作为测试集test set.然后反复更换超参在训练集上进行训练,使用测试集依次测试,进行 ...

  6. 音乐标签修改 android,音乐标签修改(Star Music Tag Editor)

    Star Music Tag Editor可以对你音乐当中的各种标签信息来进行修改,在某些音乐信息出现错误之时你能够利用这款软件来直接的进行改正,让你的标签信息变得更加的容易进行分类,如果你感兴趣的话 ...

  7. html 怎样修改标签样式,html标签修改自带样式整理

    1用能境战求道,重件开又是正易里是了些之框. 标求圈分件圈浏第用代是水刚道.的它还签 (1)修改前demo代码新直能分支调二浏页器朋代说:...... .box{ width: 500px; heig ...

  8. ios快捷指令:修改图片尺寸、拼接长图

    软件版本要求:ios13及以上 演示机型:iPhone 12 mini 演示系统版本:ios14.6 文章目录 导言 核心步骤:修改单个图片尺寸 完善细节:提供宽度默认值 进阶功能:修改多个图片尺寸 ...

  9. 如何更改图片尺寸?教你一种简单的修改方法

    大家在工作生活中经常使用到图片,也会处理各种图片比如:修改图片尺寸.小伙伴发现有时修改尺寸后会出现图片显示不全的情况,非常影响使用.那如何在不裁剪图片的情况下修改图片尺寸大小呢?相信大家看完今天小编教 ...

最新文章

  1. iOS技术篇1-CocoaPods
  2. 教你用Android做二次开发,识别率达到科大讯飞语音输入水平 | 原力计划
  3. 模拟上帝之手的对抗博弈——GAN背后的数学原理
  4. 易语言单窗口单ip软件源码_好人多窗口同步器:多台电脑同步视频演示
  5. mysql存储过程打不开了_请问mysql存储过程的问题,我找了几个例子一个都运行不起来,...
  6. 设计模式 抽象工厂模式(Abstract Factory)
  7. python数据结构-栈和队列的实现
  8. win10清理_无需第三方,win10也可以实现自动清理垃圾
  9. mysql数据库主从同步的原理_mysql数据库主从同步复制原理
  10. opencv生成灰度图并保存
  11. 电子邮件正则表达式-集结号
  12. 【读书笔记】终极算法
  13. 视频教程-三课时精通matlab拉普拉斯变换和逆变换-Matlab
  14. SQLServer安装时哪些功能是必需的(sql server 2016安装时选择哪些功能)
  15. 多媒体计算机相关知识,多媒体基础知识题库
  16. 相似度融合网络:用于聚合不同的基因数据类型:Similarity network fusion for aggregating data types on a genomic scale
  17. 计算机exce常用功能,excel表格的基本操作 电脑基本操作知识(办公室常用).doc
  18. SAP系统PP模块常用事务代码
  19. mysql force index 语法_MySQL FORCE INDEX 强制索引使用
  20. 深大uooc学术道德与学术规范教育第十一章

热门文章

  1. python 将视频旋转
  2. 怎么在电脑上制作物流条码
  3. moviepy合成中文字幕
  4. 第三章 关系数据库标准语言SQL
  5. 印度是如何做软件开发的
  6. 关于SMO算法的一点点认识
  7. 更新Office出现0xc0000142解决方案
  8. 使用Fiddler和夜神浏览器对搜狐推荐新闻的抓取
  9. MVC4中基于bootstrap和HTML5的图片上传Jquery自定义控件
  10. RTSP/Onvif摄像机在做H5无插件直播中遇到对接海康摄像机发送OPTIONS心跳的问题