Upload layer_diff_dataset/try_ani.py with huggingface_hub
Browse files
layer_diff_dataset/try_ani.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import random
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
image_folder = 'train/im'
|
| 7 |
+
mask_folder = 'train/gt'
|
| 8 |
+
image_list = os.listdir(image_folder)
|
| 9 |
+
root_img_dir = 'train/jpeg'
|
| 10 |
+
root_mask_dir = 'train/mask'
|
| 11 |
+
os.makedirs(root_img_dir, exist_ok=True)
|
| 12 |
+
os.makedirs(root_mask_dir, exist_ok=True)
|
| 13 |
+
for idx, image in enumerate(image_list):
|
| 14 |
+
# if idx == 0:
|
| 15 |
+
# continue
|
| 16 |
+
if idx == 10:
|
| 17 |
+
break
|
| 18 |
+
os.makedirs(os.path.join(root_img_dir, image.split('.jpg')[0]), exist_ok=True)
|
| 19 |
+
os.makedirs(os.path.join(root_mask_dir, image.split('.jpg')[0]), exist_ok=True)
|
| 20 |
+
image_name = os.path.join(image_folder, image)
|
| 21 |
+
mask_name = os.path.join(mask_folder, image.split('.jpg')[0]+'.png')
|
| 22 |
+
print(image_name, mask_name)
|
| 23 |
+
# image_name = 'train/im/1#Accessories#1#Bag#2339506821_83cf9f1d22_o.jpg'
|
| 24 |
+
# mask_name = 'train/gt/1#Accessories#1#Bag#2339506821_83cf9f1d22_o.png'
|
| 25 |
+
img = cv2.imread(image_name)
|
| 26 |
+
img = cv2.resize(img, (512, 512))
|
| 27 |
+
# cv2.imwrite('0.jpg', img)
|
| 28 |
+
H,W = img.shape[0], img.shape[1]
|
| 29 |
+
mask = cv2.imread(mask_name, cv2.IMREAD_GRAYSCALE)
|
| 30 |
+
mask = cv2.resize(mask, (512, 512))
|
| 31 |
+
# cv2.imwrite('1.jpg', mask)
|
| 32 |
+
# print(img.shape, mask.shape)
|
| 33 |
+
bboxes_y, bboxes_x = np.nonzero(mask)
|
| 34 |
+
|
| 35 |
+
sample_n_frames = 16
|
| 36 |
+
option = random.randint(0,2)
|
| 37 |
+
# option = 0
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# root_img = 'tmp_1/tmp_img'
|
| 41 |
+
# root_mask = 'tmp_1/tmp_mask'
|
| 42 |
+
# os.makedirs(root_img, exist_ok=True)
|
| 43 |
+
# os.makedirs(root_mask, exist_ok=True)
|
| 44 |
+
# root_img = 'tmp_1/tmp_img_1'
|
| 45 |
+
# root_mask = 'tmp_1/tmp_mask_1'
|
| 46 |
+
# os.makedirs(root_img, exist_ok=True)
|
| 47 |
+
# os.makedirs(root_mask, exist_ok=True)
|
| 48 |
+
# root_img = 'tmp_1/tmp_img_2'
|
| 49 |
+
# root_mask = 'tmp_1/tmp_mask_2'
|
| 50 |
+
# os.makedirs(root_img, exist_ok=True)
|
| 51 |
+
# os.makedirs(root_mask, exist_ok=True)
|
| 52 |
+
if option == 0:
|
| 53 |
+
small_ratio, large_ratio = 0.9, 1.2
|
| 54 |
+
for i in range(sample_n_frames):
|
| 55 |
+
|
| 56 |
+
# x1,x2,y1,y2 = bboxes_x.min(), bboxes_x.max(), bboxes_y.min(), bboxes_y.max()
|
| 57 |
+
# print(x1,x2,y1,y2)
|
| 58 |
+
# x1,x2,y1,y2 = x1*(small_ratio-i*1.0/sample_n_frames*small_ratio), min(x2*(large_ratio+i/sample_n_frames*small_ratio), W), y1*(small_ratio-i/sample_n_frames*small_ratio), min(y2*(large_ratio+i/sample_n_frames*small_ratio), H)
|
| 59 |
+
x1,x2,y1,y2 = (1-i*1.0/sample_n_frames)*0.1*W, 0.9*W+i*1.0/sample_n_frames*0.1*W, (1-i*1.0/sample_n_frames)*0.1*H, 0.9*H+i*1.0/sample_n_frames*0.1*H
|
| 60 |
+
x1,x2,y1,y2 = int(x1),int(x2),int(y1),int(y2)
|
| 61 |
+
|
| 62 |
+
# print(x1,x2,y1,y2)
|
| 63 |
+
img_ = cv2.resize(img[y1:y2, x1:x2, :], (H,W), cv2.INTER_CUBIC)
|
| 64 |
+
mask_ = cv2.resize(mask[y1:y2, x1:x2], (H,W), cv2.INTER_NEAREST)
|
| 65 |
+
cv2.imwrite(os.path.join(root_img_dir, image.split('.jpg')[0], '%05d'%i+'.jpg'), img_)
|
| 66 |
+
cv2.imwrite(os.path.join(root_mask_dir, image.split('.jpg')[0], '%05d'%i+'.png'), mask_)
|
| 67 |
+
elif option == 1:
|
| 68 |
+
small_ratio, large_ratio = 0.2, 0.9
|
| 69 |
+
for i in range(sample_n_frames):
|
| 70 |
+
# x1,x2,y1,y2 = bboxes_x.min(), bboxes_x.max(), bboxes_y.min(), bboxes_y.max()
|
| 71 |
+
# x1,x2,y1,y2 = x1*(small_ratio+i*1.0/sample_n_frames*large_ratio), min(x2*(2.0-i/sample_n_frames*large_ratio), W), y1*(small_ratio+i/sample_n_frames*large_ratio), min(y2*(2.0-i/sample_n_frames*large_ratio), H)
|
| 72 |
+
x1,x2,y1,y2 = i*1.0/sample_n_frames*0.1*W, W-i*1.0/sample_n_frames*0.1*W, i*1.0/sample_n_frames*0.1*H, H-i*1.0/sample_n_frames*0.1*H
|
| 73 |
+
x1,x2,y1,y2 = int(x1),int(x2),int(y1),int(y2)
|
| 74 |
+
img_ = cv2.resize(img[y1:y2, x1:x2, :], (H,W), cv2.INTER_CUBIC)
|
| 75 |
+
mask_ = cv2.resize(mask[y1:y2, x1:x2], (H,W), cv2.INTER_NEAREST)
|
| 76 |
+
cv2.imwrite(os.path.join(root_img_dir, image.split('.jpg')[0], '%05d'%i+'.jpg'), img_)
|
| 77 |
+
cv2.imwrite(os.path.join(root_mask_dir, image.split('.jpg')[0], '%05d'%i+'.png'), mask_)
|
| 78 |
+
else:
|
| 79 |
+
pad = min(random.randint(200,400), bboxes_x.min())
|
| 80 |
+
for i in range(sample_n_frames):
|
| 81 |
+
x1,x2,y1,y2 = bboxes_x.min(), bboxes_x.max(), bboxes_y.min(), bboxes_y.max()
|
| 82 |
+
# print(x1,x2,y1,y2)
|
| 83 |
+
x1,x2,y1,y2 = x1-pad+pad//sample_n_frames*i,x2-pad+pad//sample_n_frames*i,0,img.shape[1]
|
| 84 |
+
x1,x2,y1,y2 = int(x1),int(x2),int(y1),int(y2)
|
| 85 |
+
# print(x1,x2)
|
| 86 |
+
img_ = cv2.resize(img[y1:y2, x1:x2, :], (H,W), cv2.INTER_CUBIC)
|
| 87 |
+
mask_ = cv2.resize(mask[y1:y2, x1:x2], (H,W), cv2.INTER_NEAREST)
|
| 88 |
+
cv2.imwrite(os.path.join(root_img_dir, image.split('.jpg')[0], '%05d'%i+'.jpg'), img_)
|
| 89 |
+
cv2.imwrite(os.path.join(root_mask_dir, image.split('.jpg')[0], '%05d'%i+'.png'), mask_)
|