位置: IT常识 - 正文

目标检测数据预处理——根据部件类别按照特定位置拼图,缩小学习空间(目标检测现状)

发布时间:2024-01-14
目标检测数据预处理——根据部件类别按照特定位置拼图,缩小学习空间

推荐整理分享目标检测数据预处理——根据部件类别按照特定位置拼图,缩小学习空间(目标检测现状),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:目标检测数据增强方法,目标检测数据预处理,目标检测数据分析,目标检测数据预测分析,目标检测数据预测方法,目标检测数据增广,目标检测数据预测方法,目标检测数据预测方法,内容如对您有帮助,希望把文章链接给更多的朋友!

首先放效果图,更直观看到本片是要干嘛的: 如图,就是将大图划分为4×4宫格的,4个部件类的目标框按照固定位置拼图,其中head、body的大图为每个宫格一张图,hand、foot的小图为每个宫格2×2张图(因为hand、foot截下来的图片都普遍很小,为了不resize太多而太模糊)。 每个部件类别的小图拼在一起,实验目标检测算法是否会特定区域关注特定目标从而达到缩小学习空间的目的(为了控制变量,算法本身的位置变换类的数据增强要关闭)。 这里的的部件指的是一类目标,比如head包括head、hat等在头部区域内的目标。每类部件的图片是根据部件截图的方式获得的。

准备目标检测数据预处理——根据部件类别按照特定位置拼图,缩小学习空间(目标检测现状)

首先是将数据的json格式转化为txt格式的py文件json2txt.py:

import jsonimport osimport cv2print(cv2.__version__)def getBoundingBox(points): xmin = points[0][0] xmax = points[0][0] ymin = points[0][1] ymax = points[0][1] for p in points: if p[0] > xmax: xmax = p[0] elif p[0] < xmin: xmin = p[0] if p[1] > ymax: ymax = p[1] elif p[1] < ymin: ymin = p[1] return [int(xmin), int(xmax), int(ymin), int(ymax)]def json2txt(json_path, txt_path): json_data = json.load(open(json_path)) img_h = json_data["imageHeight"] img_w = json_data["imageWidth"] shape_data = json_data["shapes"] shape_data_len = len(shape_data) img_name = os.path.split(json_path)[-1].split(".json")[0] name = img_name + '.jpg' data = '' for i in range(shape_data_len): lable_name = shape_data[i]["label"] points = shape_data[i]["points"] [xmin, xmax, ymin, ymax] = getBoundingBox(points) if xmin <= 0: xmin = 0 if ymin <= 0: ymin = 0 if xmax >= img_w: xmax = img_w - 1 if ymax >= img_h: ymax = img_h - 1 b = name + ' ' + lable_name + ' ' + str(xmin) + ' ' + str(ymin) + ' ' + str(xmax) + ' ' + str(ymax) # print(b) data += b + '\n' with open(txt_path + '/' + img_name + ".txt", 'w', encoding='utf-8') as f: f.writelines(data)if __name__ == "__main__": json_path = "/data/cch/yolov5-augment/train/json" saveTxt_path = "/data/cch/yolov5-augment/train/txt" filelist = os.listdir(json_path) for file in filelist: old_dir = os.path.join(json_path, file) if os.path.isdir(old_dir): continue filetype = os.path.splitext(file)[1] if(filetype != ".json"): continue json2txt(old_dir, saveTxt_path)def main_import(json_path, txt_path): filelist = os.listdir(json_path) for file in filelist: old_dir = os.path.join(json_path, file) if os.path.isdir(old_dir): continue filetype = os.path.splitext(file)[1] if(filetype != ".json"): continue json2txt(old_dir, txt_path)

随机取了一个txt文件,查看其格式:

body_21.jpg cloth 51 12 255 270body_21.jpg hand 50 206 79 257body_21.jpg hand 195 217 228 269body_21.jpg other 112 0 194 1

格式:为图片名 类名 x1 y1 x2 y2(为目标框的左上右下角坐标,此txt格式并非yolo训练的darknet格式)。 然后是将数据的txt格式转化为darknet格式的py文件modeTxt.py:

import osfrom numpy.lib.twodim_base import triu_indices_fromimport pandas as pdfrom glob import globimport cv2import codecsdef txt2darknet(txt_path, img_path, saved_path): data = pd.DataFrame() filelist = os.listdir(txt_path) for file in filelist: if not os.path.splitext(file)[-1] == ".txt": continue # print(file) file_path = os.path.join(txt_path, file) filename = os.path.splitext(file)[0] imgName = filename + '.jpg' imgPath = os.path.join(img_path, imgName) img = cv2.imread(imgPath) [img_h, img_w, _] = img.shape data = "" with codecs.open(file_path, 'r', encoding='utf-8',errors='ignore') as f1: for line in f1.readlines(): line = line.strip('\n') a = line.split(' ') if a[1] == 'other' or a[1] == 'mask' or a[1] == 'del': continue # if a[1] == 'mouth': # a[1] = '0' # elif a[1] == 'wearmask': # a[1] = '1' if a[1] == 'head': a[1] = '0' elif a[1] == 'hat': a[1] = '1' elif a[1] == 'helmet': a[1] = '2' elif a[1] == 'eye': a[1] = '3' elif a[1] == 'glasses' or a[1] == 'glass': a[1] = '4' '''这里根据自己的类别名称及顺序''' x1 = float(a[2]) y1 = float(a[3]) w = float(a[4]) - float(a[2]) h = float(a[5]) - float(a[3]) # if w <= 15 and h <= 15: continue center_x = float(a[2]) + w / 2 center_y = float(a[3]) + h / 2 a[2] = str(center_x / img_w) a[3] = str(center_y / img_h) a[4] = str(w / img_w) a[5] = str(h / img_h) b = a[1] + ' ' + a[2] + ' ' + a[3] + ' ' + a[4] + ' ' + a[5] # print(b) data += b + '\n' with open(saved_path + '/' + filename + ".txt", 'w', encoding='utf-8') as f2: f2.writelines(data) print(data)txt_path = '/data/cch/yolov5/runs/detect/hand_head_resize/labels'saved_path = '/data/cch/yolov5/runs/detect/hand_head_resize/dr'img_path = '/data/cch/data/pintu/test/hand_head_resize/images'if __name__ == '__main__': txt2darknet(txt_path, img_path, saved_path)

以上两个转换代码都是在拼图当中会调用到。

拼图

下面开始我们的拼图代码:

'''4*4左上五个 1 2 3 5 6 head左下五个 9 10 11 13 14 body右上三个 4 7 8 各划分4宫格 hand右下三个 12 15 16 各划分4宫格 foot针对于部件拼图,每个部件一个文件夹,image和json的地址都取总地址'''import sysimport codecsimport randomimport PIL.Image as Imageimport osimport cv2sys.path.append("/data/cch/拼图代码/format_transform")import json2txtimport modeTxtimport shutil# 定义图像拼接函数def image_compose(imgsize, idx, ori_tmp, num, save_path, gt_resized_path, flag): to_image = Image.new('RGB', (imgsize, imgsize)) #创建一个新图 new_name = "" for y in range(idx): for x in range(idx): index = y*idx + x if index >= len(ori_tmp): break open_path = [gt_resized_path, small_pintu_foot, small_pintu_hand] for op in open_path: if os.path.exists(os.path.join(op, ori_tmp[index])): to_image.paste(Image.open(os.path.join(op, ori_tmp[index])), ( int(x * (imgsize / idx)), int(y * (imgsize / idx)))) break else: continue new_name = os.path.join(save_path, flag + str(num) + ".jpg") to_image.save(new_name) # 保存新图 # print(new_name) return new_namedef labels_merge(imgsize, idx, ori_tmp, new_name, txt_resized_path, txt_pintu_path): data = "" for y in range(idx): for x in range(idx): index = y*idx + x if index >= len(ori_tmp): break txt_path = os.path.join(txt_resized_path, ori_tmp[index].split(".")[0] + ".txt") if not os.path.exists(txt_path): txt_path = os.path.join(txt_pintu_path_small, ori_tmp[index].split(".")[0] + ".txt") try: os.path.exists(txt_path) except: print(txt_path, "file not exists!") if os.path.exists(txt_path): with codecs.open(txt_path, 'r', encoding='utf-8',errors='ignore') as f1: for line in f1.readlines(): line = line.strip('\n') a = line.split(' ') a[2] = str(float(a[2]) + (x * (imgsize / idx))) a[3] = str(float(a[3]) + (y * (imgsize / idx))) a[4] = str(float(a[4]) + (x * (imgsize / idx))) a[5] = str(float(a[5]) + (y * (imgsize / idx))) b =a[0] + ' ' + a[1] + ' ' + a[2] + ' ' + a[3] + ' ' + a[4] + ' ' + a[5] data += b + "\n" write_path = os.path.join(txt_pintu_path, os.path.splitext(new_name)[0].split("/")[-1] + ".txt") with open(write_path, 'w', encoding='utf-8') as f2: f2.writelines(data)def pintu2black(txt_pintu_path, save_path, to_black_num, to_black_min_num, label_black): files = os.listdir(txt_pintu_path) for file in files: img_path = os.path.join(save_path, os.path.splitext(file)[0] + ".jpg") img_origal = cv2.imread(img_path) data = "" with codecs.open(txt_pintu_path+"/"+file, encoding="utf-8", errors="ignore") as f1: for line in f1.readlines(): line = line.strip("\n") a = line.split(" ") xmin = int(eval(a[2])) ymin = int(eval(a[3])) xmax = int(eval(a[4])) ymax = int(eval(a[5])) if ((xmax - xmin < to_black_num) and (ymax - ymin < to_black_num)) or \ ((xmax - xmin < to_black_min_num) or (ymax - ymin < to_black_min_num)) \ or a[1] in label_black: img_origal[ymin:ymax, xmin:xmax, :] = (0, 0, 0) cv2.imwrite(img_path, img_origal) line = "" if line: data += line + "\n" with open(txt_pintu_path+"/"+file, 'w', encoding='utf-8') as f2: f2.writelines(data) # print(data)def gt_distribute(images_path, ori, gt_resized_path, txt_path, gt_range): image_names = os.listdir(images_path) for image_name in image_names: if not os.path.splitext(image_name)[-1] == ".jpg": continue imgPath = os.path.join(images_path, image_name) img = cv2.imread(imgPath) gt_resized_name = gt_resize(gt_resized_path, txt_path, image_name, img, gt_range, 2) ori.append(gt_resized_name)def gt_resize(gt_resized_path, txt_path, image_name, img, img_size, x): if not os.path.exists(gt_resized_path): os.mkdir(gt_resized_path) [img_h, img_w, _] = img.shape img_read = [0, 0, 0] if img_h < img_w: precent = img_size / img_w img_read = cv2.resize(img, (img_size, int(img_h * precent)), interpolation=cv2.INTER_CUBIC) else: precent = img_size / img_h img_read = cv2.resize(img, (int(img_w * precent), img_size), interpolation=cv2.INTER_CUBIC) img_resized = gt_resized_path + "/" + image_name.split(".")[0] + "_" + str(x) + ".jpg" cv2.imwrite(img_resized, img_read) txt_name = txt_path + "/" + image_name.split(".")[0] + ".txt" txt_resized_name = gt_resized_path + "/" + image_name.split(".")[0] + "_" + str(x) + ".txt" if os.path.exists(txt_name): data = "" with codecs.open(txt_name, 'r', encoding='utf-8',errors='ignore') as f1: for line in f1.readlines(): line = line.strip('\n') a = line.split(' ') a[2] = str(float(a[2]) * precent) a[3] = str(float(a[3]) * precent) a[4] = str(float(a[4]) * precent) a[5] = str(float(a[5]) * precent) b =a[0] + ' ' + a[1] + ' ' + a[2] + ' ' + a[3] + ' ' + a[4] + ' ' + a[5] data += b + "\n" with open(txt_resized_name, 'w', encoding='utf-8') as f2: f2.writelines(data) return img_resized.split("/")[-1]def pintu(idx, ori, img_threshold, imgsize, save_path, gt_resized_path, txt_pintu_path, flag): num = 0 if flag != "wear_" : random.shuffle(ori) picknum = idx * idx index = 0 while num < int(img_threshold): ori_tmp = [] # random.sample(ori, picknum) if index >= len(ori) and flag != "wear_" : random.shuffle(ori) index = 0 ori_tmp = ori[index:index+picknum] index = index + picknum new_name = image_compose(imgsize, idx, ori_tmp, num, save_path, gt_resized_path, flag) labels_merge(imgsize, idx, ori_tmp, new_name, gt_resized_path, txt_pintu_path) ori_tmp.clear() num += 1 print(flag, num, len(ori))if __name__ == "__main__": images_path = '/data/cch/test' # 图片集地址 json_path = "/data/cch/test" save_path = '/data/cch/save' if not os.path.exists(save_path): os.mkdir(save_path) else: shutil.rmtree(save_path) os.mkdir(save_path) tmp = "/data/cch/pintu_data/save/tmp" if not os.path.exists(tmp): os.mkdir(tmp) else: shutil.rmtree(tmp) os.mkdir(tmp) gt_resized_path = os.path.join(tmp, "gt_resized") txt_path = os.path.join(tmp, "txt") # 原数据txt txt_pintu_path = os.path.join(tmp, "txt_pintu") txt_pintu_path_small = os.path.join(tmp, "txt_pintu_small") small_pintu_foot = os.path.join(tmp, "pintu_foot") small_pintu_hand = os.path.join(tmp, "pintu_hand") os.mkdir(txt_path) os.mkdir(txt_pintu_path) os.mkdir(txt_pintu_path_small) os.mkdir(small_pintu_foot) os.mkdir(small_pintu_hand) label_black = ["other"] imgsize = 416 to_black_num = 15 to_black_min_num = 5 gt_range_large = int(imgsize / 4) gt_range_small = int(imgsize / 8) json_dirs = os.listdir(json_path) for json_dir in json_dirs: json_ori_dir = os.path.join(json_path, json_dir) txt_dir = os.path.join(txt_path, json_dir) os.mkdir(txt_dir) json2txt.main_import(json_ori_dir, txt_dir) # foot ori_foot = [] foot_images = os.path.join(images_path, "foot") foot_txt = os.path.join(txt_path, "foot") gt_distribute(foot_images, ori_foot, gt_resized_path, foot_txt, gt_range_small) img_threshold = int(len(ori_foot) / 4 * 1.6) idx = 2 pintu(idx, ori_foot, img_threshold, int(imgsize/4), small_pintu_foot, gt_resized_path,\ txt_pintu_path_small, "foot_") # hand ori_hand = [] hand_images = os.path.join(images_path, "hand") hand_txt = os.path.join(txt_path, "hand") gt_distribute(hand_images, ori_hand, gt_resized_path, hand_txt, gt_range_small) img_threshold = int(len(ori_hand) / 4 * 1.6) idx = 2 pintu(idx, ori_hand, img_threshold, int(imgsize/4), small_pintu_hand, gt_resized_path,\ txt_pintu_path_small, "hand_") # head ori_head = [] head_images = os.path.join(images_path, "head") head_txt = os.path.join(txt_path, "head") gt_distribute(head_images, ori_head, gt_resized_path, head_txt, gt_range_large) # body ori_body = [] body_images = os.path.join(images_path, "body") body_txt = os.path.join(txt_path, "body") gt_distribute(body_images, ori_body, gt_resized_path, body_txt, gt_range_large) # pintu ori = [] idx = 4 ori_foot = os.listdir(small_pintu_foot) ori_hand = os.listdir(small_pintu_hand) random.shuffle(ori_foot) random.shuffle(ori_hand) random.shuffle(ori_head) random.shuffle(ori_body) [idx_hand, idx_foot, idx_head, idx_body] = [0, 0, 0, 0] img_threshold = int((len(ori_hand) + len(ori_foot) + len(ori_head) + len(ori_body)) / (idx*idx) * 1.5) while True: for i in range(idx*idx): if i in [0,1,2,4,5]: if idx_head >= len(ori_head): random.shuffle(ori_head) idx_head = 0 ori.append(ori_head[idx_head]) idx_head += 1 elif i in [3,6,7]: if idx_hand >= len(ori_hand): random.shuffle(ori_hand) idx_hand = 0 ori.append(ori_hand[idx_hand]) idx_hand += 1 elif i in [8,9,10,12,13]: if idx_body >= len(ori_body): random.shuffle(ori_body) idx_body = 0 ori.append(ori_body[idx_body]) idx_body += 1 elif i in [11,14,15]: if idx_foot >= len(ori_foot): random.shuffle(ori_foot) idx_foot = 0 ori.append(ori_foot[idx_foot]) idx_foot += 1 if int(len(ori)/(idx*idx)) > img_threshold: break pintu(idx, ori, int(len(ori)/(idx*idx)), imgsize, save_path, gt_resized_path,\ txt_pintu_path, "wear_") pintu2black(txt_pintu_path, save_path, to_black_num, to_black_min_num, label_black) # input() modeTxt.txt2darknet(txt_pintu_path, save_path, save_path) shutil.rmtree(tmp)

这里的输入地址是4个部件的总地址,如图:

本文链接地址:https://www.jiuchutong.com/zhishi/295226.html 转载请保留说明!

上一篇:javaweb案例一(javaweb简单项目案例)

下一篇:【JSP课程设计】个人信息管理系统(代码保姆级)(jsp课程设计含源代码)

  • vivo手机下拉状态栏怎么设置(vivo手机下拉状态栏怎么关闭)

    vivo手机下拉状态栏怎么设置(vivo手机下拉状态栏怎么关闭)

  • 红米手环什么时候出(红米手环什么时候出2)

    红米手环什么时候出(红米手环什么时候出2)

  • ios13.5正式版什么时候发布(ios13.5正式版怎么样)

    ios13.5正式版什么时候发布(ios13.5正式版怎么样)

  • 微信紧急冻结是什么意思(微信紧急冻结是多长时间)

    微信紧急冻结是什么意思(微信紧急冻结是多长时间)

  • plc的工作过程分为几个阶段(PLC的工作过程分几个阶段? 各阶段的作用是什么?)

    plc的工作过程分为几个阶段(PLC的工作过程分几个阶段? 各阶段的作用是什么?)

  • 磁盘映像可以删掉吗(磁盘映像删掉影响软件吗)

    磁盘映像可以删掉吗(磁盘映像删掉影响软件吗)

  • 信号显示h代表什么(信号显示h代表什么意思)

    信号显示h代表什么(信号显示h代表什么意思)

  • 蓝牙耳机一个亮一个不亮(蓝牙耳机一个亮灯一个不亮灯是怎么回事)

    蓝牙耳机一个亮一个不亮(蓝牙耳机一个亮灯一个不亮灯是怎么回事)

  • 充q币充错账号怎么办(充q币充错账号怎么退)

    充q币充错账号怎么办(充q币充错账号怎么退)

  • 退出excel快捷键(退出excel最快的方法是)

    退出excel快捷键(退出excel最快的方法是)

  • 微信联系人上限(隐藏微信联系人)

    微信联系人上限(隐藏微信联系人)

  • oppoa11截屏是设置在哪里(oppo手机截屏开关在哪里打开)

    oppoa11截屏是设置在哪里(oppo手机截屏开关在哪里打开)

  • 如何查看微信历史登录地点(如何查看微信历史聊天记录)

    如何查看微信历史登录地点(如何查看微信历史聊天记录)

  • 淘宝补发是什么意思(淘宝补发是什么流程)

    淘宝补发是什么意思(淘宝补发是什么流程)

  • 小米9pro怎么开启双击亮屏(小米9pro怎么开启双音喇叭)

    小米9pro怎么开启双击亮屏(小米9pro怎么开启双音喇叭)

  • qq音乐怎么切换账号(qq音乐怎么切换手机号登录)

    qq音乐怎么切换账号(qq音乐怎么切换手机号登录)

  • 塞罕坝在哪里(塞罕坝在哪里?塞罕坝景区有什么好玩的?怎样去塞罕坝?)

    塞罕坝在哪里(塞罕坝在哪里?塞罕坝景区有什么好玩的?怎样去塞罕坝?)

  • 红米note8pro隐藏功能(红米note8pro隐藏的应用怎么找出来)

    红米note8pro隐藏功能(红米note8pro隐藏的应用怎么找出来)

  • 华为tltal00是什么型号(华为tlttl00什么型号)

    华为tltal00是什么型号(华为tlttl00什么型号)

  • 手机usb接口在哪(手机怎么打开usb连接)

    手机usb接口在哪(手机怎么打开usb连接)

  • 无屏助手怎么投屏(无屏助手怎么投屏到电视)

    无屏助手怎么投屏(无屏助手怎么投屏到电视)

  • iphonex怎样正确充电(苹果x操作技巧)

    iphonex怎样正确充电(苹果x操作技巧)

  • linux系统配置vsftpd服务后启动失败该怎么办?(linux系统配置ip地址命令)

    linux系统配置vsftpd服务后启动失败该怎么办?(linux系统配置ip地址命令)

  • ubuntu 13.04 u盘安装方法(ubuntu系统u盘安装)

    ubuntu 13.04 u盘安装方法(ubuntu系统u盘安装)

  • AI - stable-diffusion(AI绘画)的搭建与使用

    AI - stable-diffusion(AI绘画)的搭建与使用

  • 【微信小程序】选择器组件picker(微信小程序开发一个多少钱)

    【微信小程序】选择器组件picker(微信小程序开发一个多少钱)

  • 反避税定义
  • 个体户缴纳经营所得个税怎么算的
  • 个体工商户季度不超过30万免增值税吗
  • 车险退到对公账户会计分录是
  • 进口消费税为什么一定要组价
  • 必须一般纳税人
  • 小企业会计准则适用于哪些企业
  • 事业单位退休职业年金发放多少个月
  • 运输过程中的货损责任
  • 三证合一后银行开户许可证还有吗
  • 收到赞助费如何做分录
  • 车船使用税应该交哪里的税
  • 低价股权转让是利空还是利好
  • 为什么利润表的财务费用与利息费用逻辑不对
  • 税局代增值税专用发票 需要带什么
  • 公司与股东的往来款现金流量表
  • 卖护肤品赚钱吗
  • mac系统的桌面
  • win11比win10是更流畅了吗
  • 电脑中毒了怎么弄
  • 期初银行余额有误怎么调分录怎么写
  • php生成php文件
  • php tars
  • downloadplus.exe是什么进程 作用是什么 downloadplus进程是安全的吗
  • linux中安装命令
  • 原材料的运费计入什么科目
  • 其他业务收入对应的成本
  • 何为租赁合同
  • 拍卖有抵押的车子怎么处理
  • 编写一个php程序,展示双引号和单引号的区别
  • 一次性伤残就业补助金
  • 广东高速公路过路费官网
  • css选择器怎么用
  • 出差补贴是额外的吗
  • 罚款属于其他业务收入吗
  • mongodb视频教程
  • 记 vue-cli-plugin-dll 使用,优化vue-cli项目构建打包速度
  • sql server 实例
  • 支付航天信息服务费未收到发票怎么处理
  • 所得税费用需要结转损益吗
  • 异地设立分公司怎么办理?
  • 合作建房项目
  • 咨询费收入成本怎么算
  • 销售费用的会计科目
  • 代收运输费的会计分录
  • 平台使用费属于什么服务
  • 宾馆纳税怎么算的
  • 银行付款手续费发票如何入账
  • CREATE FUNCTION sqlserver用户定义函数
  • win7快速切换到桌面
  • 电脑svchost占用cpu很大
  • linux系统关闭ftp服务
  • window msconfig
  • wuamkop.exe - wuamkop 进程是什么意思
  • pps影音怎么没有了
  • win8系统一直重启
  • xp系统怎么更改用户权限
  • os x10.11el capitan公测版下载地址(公测版计划注册教程)
  • mac修改文件名
  • win8高级设置在哪里
  • 在linux系统中查看文件的内容命令
  • 删除同步中心图标
  • windows10移动
  • Win10桌面版红石预览版14295更新(修复)、已知问题及解决方案汇总
  • linux查内存信息
  • windows7默认网关不可用
  • Unity3D游戏开发标准教程
  • 经典都有什么
  • 使用Raygun对Node.js应用进行错误处理的方法
  • itween常用方法
  • jquery兼容ie
  • 方块大作战百科
  • Python线程进程协程
  • 微信气泡png
  • jquery的插件
  • 台州土地成交
  • 仓储物流用地属于商业用地吗
  • 重庆个人税务查询
  • 关于啤酒包装物押金,下列正确的是( )
  • 报税卡丢了要怎么处理
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号