データ拡張スクリプト:xmlタグファイルを使用して、ポジティブサンプルのターゲットフレーム位置をモザイク化します

使用する:

元のポジティブサンプルのターゲットをネガティブサンプルにコーディングします

コード(入力および出力フォルダーのパスは独自に変更できます):

import cv2
import xml.etree.ElementTree as ET
import os 

img_path = 'C:\\Users\\hq\\Desktop\\pikaqiu_data\\pikaqiu\\'
xml_path = 'C:\\Users\\hq\\Desktop\\pikaqiu_data\\pikaqiu_xml\\'
mask_img_path = 'C:\\Users\\hq\\Desktop\\pikaqiu_data\\mask_pikaqiu\\'

def xml_mask(img, start, end):
    '''
    depts: 马赛克的大小,图片尺寸小则设置小
    主要通过中间值的rgb对局部范围块的rgb做修改,depts值越小越精确
    '''
    depts = 80
    for i in range(start[0], start[1], depts):
        for j in range(end[0], end[1], depts):
             img[i:min(i + depts, img.shape[0]), j:min(j + depts, img.shape[1])] = \
                    img[(i + min(i + depts, img.shape[0]))//2][(j + min(j + depts, img.shape[1]))//2]
    return img

for img_file in os.listdir(img_path):
    img_filename = os.path.join(img_path, img_file)
    img_cv = cv2.imread(img_filename)
    
    img_name = (os.path.splitext(img_file)[0])
    xml_name = xml_path+'%s.xml'%img_name
    
    root = ET.parse(xml_name).getroot()
    for obj in root.iter('object'):
        name = obj.find('name').text

        xmlbox = obj.find('bndbox')
        x0 = xmlbox.find('xmin').text
        y0 = xmlbox.find('ymin').text
        x1 = xmlbox.find('xmax').text
        y1 = xmlbox.find('ymax').text
        
        xml_mask(img_cv, (int(y0), int(y1)), (int(x0), int(x1)))
        
    cv2.imwrite(mask_img_path + img_file, img_cv)

結果を生成します。

ここに画像の説明を挿入します
ここに画像の説明を挿入します

おすすめ

転載: blog.csdn.net/weixin_44414948/article/details/106080023