import os
import os.path as osp
import glob
import cv2
import random
import numpy as np
os.environ['show'] = '0';
show = int(os.getenv('show'));
def letterbox(img, new_shape=(640, 640),scaleup=False):
print(f"new_shape[0]:{new_shape[0]},new_shape[1]:{new_shape[1]}");
shape = img.shape[:2]
print(f"letterbox shape:{shape}");
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
ratio = r, r
print(f"r:{r}");
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
if 1:
print("resize start");
print(f"new_unpad:{new_unpad}");
img1 = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
print(f"img1.shape[:2]:{img1.shape[:2]}");
else:
print("else");
return img
return img1
dir_path = "./train_val/";
dir_txt_path = "./train_val_labels_yning/"
save_path = "./save-txt-dir/";
save_img_path = "./save-img-dir";
os.makedirs(save_path, exist_ok=True);
os.makedirs(save_img_path, exist_ok=True);
class_name = "palm";
imgs_path = osp.join(dir_path,f"{class_name}");
txts_path = osp.join(dir_txt_path,f"{class_name}");
imgs_list = glob.glob(osp.join(imgs_path,"*"));
print("imgs_list-len / 2:",int(len(imgs_list) / 2));
len_img = int(len(imgs_list) / 2);
imgs_list_sample = random.sample(imgs_list, len_img);
txts_list = glob.glob(osp.join(txts_path,"*"));
class_id = 7;
i = 0;
while i < len(imgs_list_sample):
scale_factor = random.uniform(0.7, 0.8);
print("scale_factor",scale_factor);
box_x1 = 0;
box_y1 = 0;
flag = 0;
flag2 = 0;
j = i + 1;
if j >= len(imgs_list_sample):
break;
one_img_path = imgs_list_sample[i];
two_img_path = imgs_list_sample[j];
img1 = cv2.imread(one_img_path, -1);
w1 = img1.shape[1];
h1 = img1.shape[0];
img2 = cv2.imread(two_img_path, -1);
w2 = img2.shape[1];
h2 = img2.shape[0];
img_copy1 = img1.copy();
img_copy2 = img2.copy();
one_txt_path = osp.join(txts_path, osp.splitext(osp.basename(one_img_path))[0]+".txt");
two_txt_path = osp.join(txts_path, osp.splitext(osp.basename(two_img_path))[0]+".txt");
with open(one_txt_path,"r") as fr:
lines = fr.readlines()
z = 0;
print("len(lines)",len(lines))
for line in lines:
line = line.strip()
try:
lable,cnx,cny,nw,nh = list(map(float,line.split(" ")))
except:
print(line)
if lable == class_id:
color = (0,255,0)
box_w = int(nw * w1)
box_h = int(nh * h1)
box_x = int(cnx * w1 - box_w / 2)
box_y = int(cny * h1 - box_h / 2)
roi = img_copy1[box_y:box_y+box_h,box_x:box_x+box_w];
if lable == 15:
box_w1 = int(nw * w1)
box_h1 = int(nh * h1)
box_x1 = int(cnx * w1 - box_w1 / 2)
box_y1 = int(cny * h1 - box_h1 / 2)
if box_w1 >=20 or box_h1 >=20:
flag = 1;
with open(two_txt_path,"r") as fr:
lines = fr.readlines()
print("len(lines)",len(lines))
for line in lines:
line = line.strip()
try:
lable,cnx,cny,nw,nh = list(map(float,line.split(" ")))
except:
print(line)
if lable == class_id:
color = (0,255,0)
box_w2 = int(nw * w2)
box_h2 = int(nh * h2)
box_x2 = int(cnx * w2 - box_w2 / 2)
box_y2 = int(cny * h2 - box_h2 / 2)
roi2 = img_copy2[box_y2:box_y2+box_h2,box_x2:box_x2+box_w2];
if lable == 15:
box_w3 = int(nw * w2)
box_h3 = int(nh * h2)
box_x3 = int(cnx * w2 - box_w3 / 2)
box_y3 = int(cny * h2 - box_h3 / 2)
box_y3right = int(cny * h2 + box_h3 / 2);
if box_w3 >=20 or box_h3 >=20:
flag2 = 1;
if roi is not None and flag2 == 1:
flag2 = 0;
if box_w3 > box_h3:
new_resize = int(box_w3 * scale_factor);
else:
new_resize = int(box_h3 * scale_factor);
print(f"box_w3:{box_w3}, box_h3:{box_h3}");
print(f"box3 new_resize:{new_resize}");
roi = letterbox(roi, (new_resize,new_resize));
roi_h1, roi_w1 = roi.shape[:2];
print(f"roi_h1:{roi_h1},roi_w1:{roi_w1}");
bottom = random.randrange(int(box_w3 / 2) + 5, int(box_w3 / 2) + 15);
print(f"img_copy2.shape[:2]:{img_copy2.shape[:2]}");
try:
img_copy2[box_y3 + bottom:roi_h1 + bottom + box_y3, box_x3:box_x3 + roi_w1] = roi;
x1_centre = (box_x3 + roi_w1 + box_x3) / 2 / w2;
y1_centre = (roi_h1 + bottom + box_y3 + box_y3 + bottom) / 2 / h2;
roi1_w = (box_x3 + roi_w1 - box_x3) / w2;
roi1_h = (roi_h1 + bottom + box_y3 - box_y3 - bottom) / h2;
str_coco = f"{class_id} {x1_centre} {y1_centre} {roi1_w} {roi1_h}";
print("str_coco", str_coco);
with open(osp.join(save_path, osp.basename(two_txt_path)),"w") as f2s:
f2s.write(str_coco);
f2s.write("\n");
cv2.imwrite(osp.join(save_img_path,osp.basename(two_img_path)),img_copy2);
if show:
cv2.imshow(osp.join(save_img_path,osp.basename(two_img_path)),img_copy2);
cv2.waitKey(0);
cv2.destroyAllWindows()
except:
pass;
if roi2 is not None and flag == 1:
if box_w1 > box_h1:
new_resize = int(box_w1 * scale_factor);
else:
new_resize = int(box_h1 * scale_factor);
print(f"box_w1:{box_w1}, box_h1:{box_h1}");
print(f"box1 new_resize:{new_resize}");
roi2 = letterbox(roi2, (new_resize,new_resize));
roi_h, roi_w = roi2.shape[:2];
print(f"roi2_h:{roi_h},roi2_w:{roi_w}");
bottom = random.randrange(int(box_w1 / 2) + 5, int(box_w1 / 2) + 15);
print(f"img_copy1.shape[:2]:{img_copy1.shape[:2]}");
try:
img_copy1[box_y1 + bottom : roi_h + bottom + box_y1, box_x1 : box_x1 + roi_w] = roi2;
flag = 0;
x1_centre = (box_x1 + roi_w + box_x1) / 2 / w1;
y1_centre = (roi_h + bottom + box_y1 + box_y1 + bottom) / 2 / h1;
roi2_w = (box_x1 + roi_w - box_x1) / w1;
roi2_h = (roi_h + bottom + box_y1 - box_y1 - bottom) / h1;
str_coco = f"{class_id} {x1_centre} {y1_centre} {roi2_w} {roi2_h}";
print("str_coco2",str_coco);
with open(osp.join(save_path, osp.basename(one_txt_path)),"w") as f1s:
f1s.write(str_coco);
f1s.write("\n");
cv2.imwrite(osp.join(save_img_path, osp.basename(one_img_path)),img_copy1);
if show:
cv2.imshow("",img_copy1);
cv2.waitKey(0);
cv2.destroyAllWindows()
except:
pass
i+=2;