本片是针对部件截图篇的升级,具体的效果请跳转上一篇,本片只突出升级部分。
上一篇是按照固定的比例进行外扩,上下一个比例,左右一个比例。这里就对比例进行的升级,上下左右各一个比例,且随机比例(不让每张图的目标都是在图中间)。
'''
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
这里因为头、身体区域的部件较大,为了不让其太大要让4个部件的截图大小不差太多,所以还是固定比例,手、脚区域较小用随机比例(有上下限)。
看几个手、脚区域的截图效果: