粘贴数据增强

本文介绍了一种用于计算机视觉任务的手部图像合成方法及相应的标注过程。该方法通过随机选取背景图片并合成不同数量的手部图像,实现数据增强的目的。合成后的图像会被标注为YOLO格式的数据,便于训练目标检测模型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# import os
# from PIL import Image
# import random
# import cv2
# import numpy as np
#
# shopdir = '/media/lixuan/Data/sku_data/sku110k/sku110k_yolov5_style/images/train'
# handdir = '/home/lixuan/Arduino/hand_detection/hand'
# bank = os.listdir(shopdir)
# hand = os.listdir(handdir)
# for i in range(len(hand)):
#     fw = open('/home/lixuan/Arduino/hand_detection/makehands/labels/trainhand_{}.txt'.format(i),'w')
#     handlist = []
#     handnum = random.randint(1,4)
#     try:
#         handlist.append(Image.open(os.path.join(handdir,hand[i])))
#     except:
#         pass
#     for j in range(handnum - 1):
#         try:
#             handlist.append(Image.open(os.path.join(handdir,hand[random.randint(0,len(hand))])))
#         except:
#             handlist.append(Image.open('/home/lixuan/Arduino/hand_detection/hand/hand.jpg'))
#     if handlist == []:
#         handlist.append(Image.open('/home/lixuan/Arduino/hand_detection/hand/hand.jpg'))
#     img2 = Image.open(os.path.join(shopdir, bank[random.randint(0, len(bank) - 1)]))
#     w2, h2 = img2.size
#     for img1 in handlist:
#         w1,h1 = img1.size
#         ratemin = min(w2/w1,h2/h1)
#         if ratemin < 20:
#             new_w = int(w2 // random.randint(20,25))
#             new_rate = w1 / new_w
#             new_h = int(h1 // new_rate)
#             img1 = img1.resize((new_w, new_h), Image.ANTIALIAS)
#             w1 = new_w
#             h1 = new_h
#         past_x = random.randint(0,w2 - w1)
#         past_y = random.randint(0,h2 - h1)
#         img2.paste(img1,(past_x,past_y))
#     #     img2 = np.array(img2)
#         x1 = past_x
#         y1 = past_y
#         x2 = past_x + w1
#         y2 = past_y + h1
#         x = ((x1 + x2) / 2) / w2
#         y = ((y1 + y2) / 2) / h2
#         w = w1 / w2
#         h = h1 / h2
#         fw.write('0 ' + str(x) + ' ' + str(y) + ' ' + str(w) + ' ' + str(h) + '\n')
#     #     img2 = cv2.rectangle(img2,(x1,y1),(x2,y2),(0,255,255,0),3)
#     img2.save('/home/lixuan/Arduino/hand_detection/makehands/images/trainhand_{}.jpg'.format(i))
#     # img2 = np.array(img2)
#     # img2 = cv2.resize(img2, (w2 // 2, h2 // 2))
#     # cv2.imshow('',img2)
#     # cv2.waitKey(0)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值