Target detection data preprocessing - component screenshots, external expansion according to random proportions

This film is an upgrade for the screenshots of parts. For the specific effect, please jump to the previous article. This film only highlights the upgraded part.

The previous article was expanded according to a fixed ratio, the upper and lower ratios, and the left and right ratios. Here is an upgrade to the ratio, with a ratio of up, down, left, and right, and a random ratio (don't let the target of each picture be in the middle of the picture).

'''
cut 4 part in [head, body, hand, foot]
generate small pic of some rate extend
and generate corresponding json

Update points from the No.1 generation:the rate of extend of foot & hand is random,
                                        and the target not in center,also random
'''
from copy import deepcopy
import cv2
import json
import os
import random

img_path = "/data/weardata/images"
json_path = "/data/weardata/json"
save_path = "/data/weardata/save"
head_save = os.path.join(save_path, "head")
body_save = os.path.join(save_path, "body")
hand_save = os.path.join(save_path, "hand")
foot_save = os.path.join(save_path, "foot")

# 4部件分类
head_label = ["head", "hat", "workhat", "helmet"]
body_label = ["blueworkclothes", "cloth", "refvest", "apron", "whiteworkclothes"]
hand_label = ["glove", "inglove", "hand"]
foot_label = ["shoes", "inshoes", "noshoes"]

save_path = [head_save, body_save, hand_save, foot_save]
for s_p in save_path:
    if not os.path.exists(s_p):
        os.mkdir(s_p)

def random_cut():
    total_random = random.uniform(1, 3)
    top_random = random.uniform(0, total_random)
    left_random = random.uniform(0, total_random)
    bottom_random = total_random - top_random
    right_random = total_random - left_random
    return [top_random, bottom_random, left_random, right_random]

def cut_part(json_data, part_label, save_path, img_file, img_h, img_w, l_ran, r_ran, t_ran, b_ran):
    i = 0
    for shape in json_data["shapes"]:
        json_data_1 = deepcopy(json_data)
        if shape["label"] in part_label:
            img_save = os.path.join(save_path, os.path.splitext(os.path.split(img_file)[-1])[0] + ".jpg")
            json_save = save_path + "/" + file
            if os.path.exists(json_save):
                json_save = save_path + "/" + str(i) + file
                img_save = save_path + "/" + str(i) + os.path.splitext(os.path.split(img_file)[-1])[0] + ".jpg"
                json_data_1["imagePath"] = str(i) + os.path.splitext(os.path.split(img_file)[-1])[0] + ".jpg"
                i += 1
            p = shape["points"]
            x_l = int(abs(p[1][0] - p[0][0]) * l_ran)
            x_r = int(abs(p[1][0] - p[0][0]) * r_ran)
            y_t = int(abs(p[1][1] - p[0][1]) * t_ran)
            y_b = int(abs(p[1][1] - p[0][1]) * b_ran)
            x1 = int(min(p[0][0], p[1][0])) - x_l
            y1 = int(min(p[0][1], p[1][1])) - y_t
            x2 = int(max(p[0][0], p[1][0])) + x_r
            y2 = int(max(p[0][1], p[1][1])) + y_b
            if x1 < 0:
                x1 = 0
            if y1 < 0:
                y1 = 0
            if x2 > img_w:
                x2 = img_w
            if y2 > img_h:
                y2 = img_h
            inpart_label = []
            for shape1 in json_data_1["shapes"]:
                m_p = shape1["points"]
                m_x1 = int(min(m_p[0][0], m_p[1][0]))
                m_y1 = int(min(m_p[0][1], m_p[1][1]))
                m_x2 = int(max(m_p[0][0], m_p[1][0]))
                m_y2 = int(max(m_p[0][1], m_p[1][1]))
                m_p[0][0] = m_x1
                m_p[0][1] = m_y1
                m_p[1][0] = m_x2
                m_p[1][1] = m_y2
                if shape1["label"] == "other" and ((x1 < m_x1 < x2 or x1 < m_x2 < x2)\
                    and (y1 < m_y1 < y2 or y1 < m_y2 < y2)):
                    inpart_label.append(shape1)
                if x1 < (m_x1 + m_x2)/2 < x2 and y1 < (m_y1 + m_y2)/2 <y2:
                    if part_label == hand_label:
                        if shape1["label"] == "phone" or shape1["label"] in hand_label:
                            inpart_label.append(shape1)
                    else:
                        inpart_label.append(shape1)
                else:
                    continue
                # print(m_x1, m_y1, m_x2, m_y2)
            img = cv2.imread(img_file)
            try:
                img = img[y1:y2, x1:x2, :]
            except TypeError:
                print(img_file, shape["label"])
                input()
            json_data_1["shapes"] = []
            for p_label in inpart_label:
                m_p = p_label["points"]
                if p_label["shape_type"] == "polygon":
                    print("-------",p_label)
                    p_label["shape_type"] = "rectangle"
                    x_p, y_p= [], []
                    for p in m_p:
                        x_p.append(p[0])
                        y_p.append(p[1])
                    m_p = []
                    m_p = [[0,0], [0,0]]
                    m_p[0][0], m_p[0][1], m_p[1][0], m_p[1][1] = min(x_p), min(y_p), max(x_p), max(y_p)
                    p_label["points"] = m_p
                    print(m_p)
                    print(p_label["points"])
                m_p[0][0] = m_p[0][0] - x1
                m_p[0][1] = m_p[0][1] - y1
                m_p[1][0] = m_p[1][0] - x1
                m_p[1][1] = m_p[1][1] - y1
                if m_p[0][0] < 0:
                    m_p[0][0] = 0
                if m_p[0][1] < 0:
                    m_p[0][1] = 0
                if m_p[1][0] > x2 - x1:
                    m_p[1][0] = x2 - x1
                if m_p[1][1] > y2 - y1:
                    m_p[1][1] = y2 - y1
                json_data_1["shapes"].append(p_label)
            json_data_1["imageHeight"] = y2 - y1
            json_data_1["imageWidth"] = x2 -x1
            json_data_1['imageData'] = None
            json.dump(json_data_1, open(json_save, "w"), ensure_ascii=False, indent=2)
            cv2.imwrite(img_save, img)
            print(img_file)

files = os.listdir(json_path)
for file in files:
    if os.path.splitext(file)[-1] != ".json":
        continue
    img_file = os.path.join(img_path, file.split(".json")[0] + ".jpg")
    if not os.path.exists(img_file):
        img_file = os.path.join(img_path, file.split(".json")[0] + ".png")
        if not os.path.exists(img_file):
            img_file = os.path.join(img_path, file.split(".json")[0] + ".jpeg")
    json_file = os.path.join(json_path, file)
    json_data = json.load(open(json_file))
    img_h = json_data["imageHeight"]
    img_w = json_data["imageWidth"]
    cut_part(json_data, head_label, head_save, img_file, img_h, img_w, 0.3, 0.3, 0.2, 0.2) # head
    cut_part(json_data, body_label, body_save, img_file, img_h, img_w, 0.25, 0.25, 0.05, 0.05) # body
    [t_ran, b_ran, l_ran, r_ran] = random_cut()
    cut_part(json_data, hand_label, hand_save, img_file, img_h, img_w, l_ran, r_ran, t_ran, b_ran) # hand
    [t_ran, b_ran, l_ran, r_ran] = random_cut()
    cut_part(json_data, foot_label, foot_save, img_file, img_h, img_w, l_ran, r_ran, t_ran, b_ran) # foot

Here, because the parts in the head and body areas are relatively large, in order not to make them too large, the size of the screenshots of the four parts should not differ too much, so the proportion is still fixed, and the hand and foot areas are small and use random proportions (with upper and lower limits).
Look at the screenshots of several hands and feet:
insert image description here
insert image description here

Acho que você gosta

Origin blog.csdn.net/weixin_45354497/article/details/130804874
Recomendado
Clasificación