def augment_for_one_image(annotation_line, size): annotation = annotation_line.strip().split() img_path = config.root+'/data/%s/'%config.landmark_img_set+annotation[0] #print img_path img = cv2.imread(img_path) width = img.shape[1] height = img.shape[0] landmark = np.array(annotation[1:213],dtype=np.float32) landmark_x = landmark[0::2] landmark_y = landmark[1::2] max_x = max(landmark_x) min_x = min(landmark_x) max_y = max(landmark_y) min_y = min(landmark_y) cx = 0.5*(max_x+min_x) cy = 0.5*(max_y+min_y) h = max_x-min_x w = max_y-min_y bbox_size = max(h,w) x1 = int(cx - bbox_size*0.5) y1 = int(cy - bbox_size*0.5) w = bbox_size h = bbox_size cur_angle = npr.randint(int(config.min_rot_angle),int(config.max_rot_angle)+1) try_num = 0 cur_sample_num = 0 base_num = 1 force_accept = 0 while cur_sample_num < base_num: try_num += 1 if try_num > base_num*1000: force_accept = 1 break rot_landmark_x,rot_landmark_y = image_processing.rotateLandmark106(cx,cy,landmark_x,landmark_y, cur_angle,1) cur_size = int(npr.randint(10, 21)*0.1*bbox_size) up_border_size = int(-cur_size*0.15) down_border_size = int(-cur_size*0.15) left_border_size = int(-cur_size*0.15) right_border_size = int(-cur_size*0.15) # delta here is the offset of box center delta_x = npr.randint(-int(w * 0.35), int(w * 0.35)+1) delta_y = npr.randint(-int(h * 0.35), int(h * 0.35)+1) nx1 = int(max(x1 + w / 2 + delta_x - cur_size / 2, 0)) ny1 = int(max(y1 + h / 2 + delta_y - cur_size / 2, 0)) nx2 = nx1 + cur_size ny2 = ny1 + cur_size if nx2 > width or ny2 > height: continue ignore = 0 max_rot_landmark_x = max(rot_landmark_x) min_rot_landmark_x = min(rot_landmark_x) max_rot_landmark_y = max(rot_landmark_y) min_rot_landmark_y = min(rot_landmark_y) if min_rot_landmark_x < nx1+left_border_size or max_rot_landmark_x >= nx1 + cur_size-right_border_size: ignore = 1 if min_rot_landmark_y < ny1+up_border_size or max_rot_landmark_y >= ny1 + cur_size-down_border_size: ignore = 1 if ignore == 1: continue landmark_x_dis = max_rot_landmark_x - min_rot_landmark_x landmark_y_dis = max_rot_landmark_y - min_rot_landmark_y tmp_dis = landmark_x_dis*landmark_x_dis + landmark_y_dis*landmark_y_dis if tmp_dis < 0.64*cur_size*cur_size: continue offset_x = (rot_landmark_x - nx1+0.5)/float(cur_size) offset_y = (rot_landmark_y - ny1+0.5)/float(cur_size) rot_img,_,_ = image_processing.rotateWithLandmark106(img,cx,cy,landmark_x,landmark_y, cur_angle,1) cropped_im = rot_img[ny1 : ny2, nx1 : nx2, :] resized_im = cv2.resize(cropped_im, (size, size), interpolation=cv2.INTER_LINEAR) cur_sample_num += 1 if force_accept == 1: ny1 = max(0,y1) ny2 = int(min(height,y1+h)) nx1 = max(0,x1) nx2 = int(min(width,x1+w)) w = nx2-nx1 h = ny2-ny1 #print ny1,ny2,nx1,nx2 cropped_im = img[ny1 : ny2, nx1 : nx2, :] resized_im = cv2.resize(cropped_im, (size, size), interpolation=cv2.INTER_LINEAR) offset_x = (landmark_x - nx1+0.5)/float(cur_size) offset_y = (landmark_y - ny1+0.5)/float(cur_size) if config.landmark106_select23: select_landmark=[0 for i in range(46)] select_landmark[0:16:2] = offset_x[66:74] select_landmark[1:17:2] = offset_y[66:74] select_landmark[16] = offset_x[104] select_landmark[17] = offset_y[104] select_landmark[18:34:2] = offset_x[75:83] select_landmark[19:35:2] = offset_y[75:83] select_landmark[34] = offset_x[105] select_landmark[35] = offset_y[105] select_landmark[36] = offset_x[60] select_landmark[37] = offset_y[60] select_landmark[38] = offset_x[84] select_landmark[39] = offset_y[84] select_landmark[40] = offset_x[96] select_landmark[41] = offset_y[96] select_landmark[42] = offset_x[90] select_landmark[43] = offset_y[90] select_landmark[44] = offset_x[100] select_landmark[45] = offset_y[100] landmark = select_landmark else: landmark[0::2] = offset_x landmark[1::2] = offset_y if config.enable_blur: #kernel_size = npr.randint(-5,4)*2+1 kernel_size = npr.randint(-5,13)*2+1 if kernel_size >= 3: blur_im = cv2.GaussianBlur(resized_im,(kernel_size,kernel_size),0) resized_im = blur_im return resized_im,landmark
def augment_for_one_image(annotation_line, size): annotation = annotation_line.strip().split() img_path = config.root + '/data/%s/' % config.landmark_img_set + annotation[ 0] #print img_path img = cv2.imread(img_path) width = img.shape[1] height = img.shape[0] landmark = np.array(annotation[1:213], dtype=np.float32) landmark_x = landmark[0::2] landmark_y = landmark[1::2] max_x = max(landmark_x) min_x = min(landmark_x) max_y = max(landmark_y) min_y = min(landmark_y) cx = 0.5 * (max_x + min_x) cy = 0.5 * (max_y + min_y) h = max_x - min_x w = max_y - min_y bbox_size = max(h, w) x1 = int(cx - bbox_size * 0.5) y1 = int(cy - bbox_size * 0.5) w = bbox_size h = bbox_size cur_angle = npr.randint(int(config.min_rot_angle), int(config.max_rot_angle) + 1) try_num = 0 cur_sample_num = 0 base_num = 1 force_accept = 0 while cur_sample_num < base_num: try_num += 1 if try_num > base_num * 1000: force_accept = 1 break rot_landmark_x, rot_landmark_y = image_processing.rotateLandmark106( cx, cy, landmark_x, landmark_y, cur_angle, 1) #cur_size = int(npr.randint(10, 21)*0.1*bbox_size) cur_size = int(npr.randint(10, 16) * 0.1 * bbox_size) up_border_size = int(-cur_size * 0.15) down_border_size = int(-cur_size * 0.15) left_border_size = int(-cur_size * 0.15) right_border_size = int(-cur_size * 0.15) # delta here is the offset of box center #delta_x = npr.randint(-int(w * 0.35), int(w * 0.35)+1) #delta_y = npr.randint(-int(h * 0.35), int(h * 0.35)+1) delta_x = npr.randint(-int(w * 0.20), int(w * 0.20) + 1) delta_y = npr.randint(-int(h * 0.20), int(h * 0.20) + 1) nx1 = int(max(x1 + w / 2 + delta_x - cur_size / 2, 0)) ny1 = int(max(y1 + h / 2 + delta_y - cur_size / 2, 0)) nx2 = nx1 + cur_size ny2 = ny1 + cur_size if nx2 > width or ny2 > height: continue ignore = 0 max_rot_landmark_x = max(rot_landmark_x) min_rot_landmark_x = min(rot_landmark_x) max_rot_landmark_y = max(rot_landmark_y) min_rot_landmark_y = min(rot_landmark_y) if min_rot_landmark_x < nx1 + left_border_size or max_rot_landmark_x >= nx1 + cur_size - right_border_size: ignore = 1 if min_rot_landmark_y < ny1 + up_border_size or max_rot_landmark_y >= ny1 + cur_size - down_border_size: ignore = 1 if ignore == 1: continue landmark_x_dis = max_rot_landmark_x - min_rot_landmark_x landmark_y_dis = max_rot_landmark_y - min_rot_landmark_y tmp_dis = landmark_x_dis * landmark_x_dis + landmark_y_dis * landmark_y_dis #if tmp_dis < 0.64*cur_size*cur_size: if tmp_dis < 1.00 * cur_size * cur_size: continue offset_x = (rot_landmark_x - nx1 + 0.5) / float(cur_size) offset_y = (rot_landmark_y - ny1 + 0.5) / float(cur_size) rot_img, _, _ = image_processing.rotateWithLandmark106( img, cx, cy, landmark_x, landmark_y, cur_angle, 1) cropped_im = rot_img[ny1:ny2, nx1:nx2, :] resized_im = cv2.resize(cropped_im, (size, size), interpolation=cv2.INTER_LINEAR) cur_sample_num += 1 if force_accept == 1: ny1 = max(0, y1) ny2 = int(min(height, y1 + h)) nx1 = max(0, x1) nx2 = int(min(width, x1 + w)) w = nx2 - nx1 h = ny2 - ny1 #print ny1,ny2,nx1,nx2 cropped_im = img[ny1:ny2, nx1:nx2, :] resized_im = cv2.resize(cropped_im, (size, size), interpolation=cv2.INTER_LINEAR) offset_x = (landmark_x - nx1 + 0.5) / float(cur_size) offset_y = (landmark_y - ny1 + 0.5) / float(cur_size) if config.landmark106_migu_weighting: landmark[0:66:2] = offset_x[0:33] landmark[1:67:2] = offset_y[0:33] landmark[66:86:2] = offset_x[33:43] * 2 #eyebrow up landmark[67:87:2] = offset_y[33:43] * 2 #eyebrow up landmark[86:128:2] = offset_x[43:64] landmark[87:129:2] = offset_y[43:64] landmark[128:144:2] = offset_x[64:72] * 2 #eyebrow down landmark[129:145:2] = offset_y[64:72] * 2 #eyebrow down landmark[144:168:2] = offset_x[72:84] landmark[145:169:2] = offset_y[72:84] landmark[168:208:2] = offset_x[84:104] * 2 #lips landmark[169:209:2] = offset_y[84:104] * 2 #lips landmark[208:212:2] = offset_x[104:106] landmark[209:213:2] = offset_y[104:106] else: landmark[0::2] = offset_x landmark[1::2] = offset_y if config.enable_blur: #kernel_size = npr.randint(-5,4)*2+1 kernel_size = npr.randint(-5, 13) * 2 + 1 if kernel_size >= 3: blur_im = cv2.GaussianBlur(resized_im, (kernel_size, kernel_size), 0) resized_im = blur_im return resized_im, landmark
def augment_for_one_image(annotation_line, size): annotation = annotation_line.strip().split() img_path = config.root + '/data/%s/' % config.landmark_img_set + annotation[ 0] #print img_path img = cv2.imread(img_path) width = img.shape[1] height = img.shape[0] landmark = np.array(annotation[1:213], dtype=np.float32) landmark_x = landmark[0::2] landmark_y = landmark[1::2] max_x = max(landmark_x) min_x = min(landmark_x) max_y = max(landmark_y) min_y = min(landmark_y) cx = 0.5 * (max_x + min_x) cy = 0.5 * (max_y + min_y) w = max_x - min_x h = max_y - min_y bbox_size = max(h, w) x1 = int(cx - bbox_size * 0.5) y1 = int(cy - bbox_size * 0.5) w = bbox_size h = bbox_size init_rot = 0 if config.landmark106_migu_init_rot: eye_cx = 0.25 * (landmark_x[52] + landmark_x[55] + landmark_x[58] + landmark_x[61]) eye_cy = 0.25 * (landmark_y[52] + landmark_y[55] + landmark_y[58] + landmark_y[61]) mouth_cx = 0.25 * (landmark_x[84] + landmark_x[96] + landmark_x[100] + landmark_x[90]) mouth_cy = 0.25 * (landmark_y[84] + landmark_y[96] + landmark_y[100] + landmark_y[90]) dir_x = mouth_cx - eye_cx dir_y = mouth_cy - eye_cy init_rot = 90 - math.atan2(dir_y, dir_x) / math.pi * 180 cur_angle = npr.randint(int(config.min_rot_angle - init_rot), int(config.max_rot_angle - init_rot) + 1) try_num = 0 cur_sample_num = 0 base_num = 1 force_accept = 0 while cur_sample_num < base_num: try_num += 1 if try_num > base_num * 1000: force_accept = 1 break rot_landmark_x, rot_landmark_y = image_processing.rotateLandmark106( cx, cy, landmark_x, landmark_y, cur_angle, 1) rot_max_x = max(rot_landmark_x) rot_min_x = min(rot_landmark_x) rot_max_y = max(rot_landmark_y) rot_min_y = min(rot_landmark_y) rot_cx = 0.5 * (rot_max_x + rot_min_x) rot_cy = 0.5 * (rot_max_y + rot_min_y) rot_w = rot_max_x - rot_min_x rot_h = rot_max_y - rot_min_y rot_bbox_size = max(rot_h, rot_w) rot_x1 = int(rot_cx - rot_bbox_size * 0.5) rot_y1 = int(rot_cy - rot_bbox_size * 0.5) rot_w = rot_bbox_size rot_h = rot_bbox_size #cur_size = int(npr.randint(10, 21)*0.1*rot_bbox_size) cur_size = int(npr.randint(10, 16) * 0.1 * rot_bbox_size) #cur_size = int(npr.randint(110, 126)*0.01*rot_bbox_size) up_border_size = int(-cur_size * 0.15) down_border_size = int(-cur_size * 0.15) left_border_size = int(-cur_size * 0.15) right_border_size = int(-cur_size * 0.15) #up_border_size = int(cur_size*0.05) #down_border_size = int(cur_size*0.05) #left_border_size = int(cur_size*0.05) #right_border_size = int(cur_size*0.05) # delta here is the offset of box center #delta_x = npr.randint(-int(rot_w * 0.35), int(rot_w * 0.35)+1) #delta_y = npr.randint(-int(rot_h * 0.35), int(rot_h * 0.35)+1) delta_x = npr.randint(-int(rot_w * 0.20), int(rot_w * 0.20) + 1) delta_y = npr.randint(-int(rot_h * 0.20), int(rot_h * 0.20) + 1) #delta_x = npr.randint(-int(rot_w * 0.02), int(rot_w * 0.02)+1) #delta_y = npr.randint(-int(rot_h * 0.02), int(rot_h * 0.02)+1) nx1 = int(max(rot_x1 + rot_w / 2 + delta_x - cur_size / 2, 0)) ny1 = int(max(rot_y1 + rot_h / 2 + delta_y - cur_size / 2, 0)) nx2 = nx1 + cur_size ny2 = ny1 + cur_size if nx2 > width or ny2 > height: continue ignore = 0 max_rot_landmark_x = max(rot_landmark_x) min_rot_landmark_x = min(rot_landmark_x) max_rot_landmark_y = max(rot_landmark_y) min_rot_landmark_y = min(rot_landmark_y) if min_rot_landmark_x < nx1 + left_border_size or max_rot_landmark_x >= nx1 + cur_size - right_border_size: ignore = 1 if min_rot_landmark_y < ny1 + up_border_size or max_rot_landmark_y >= ny1 + cur_size - down_border_size: ignore = 1 if ignore == 1: continue landmark_x_dis = max_rot_landmark_x - min_rot_landmark_x landmark_y_dis = max_rot_landmark_y - min_rot_landmark_y tmp_dis = landmark_x_dis * landmark_x_dis + landmark_y_dis * landmark_y_dis #if tmp_dis < 0.64*cur_size*cur_size: if tmp_dis < 1.00 * cur_size * cur_size: continue offset_x = (rot_landmark_x - nx1 + 0.5) / float(cur_size) offset_y = (rot_landmark_y - ny1 + 0.5) / float(cur_size) rot_img, _, _ = image_processing.rotateWithLandmark106( img, cx, cy, landmark_x, landmark_y, cur_angle, 1) cropped_im = rot_img[ny1:ny2, nx1:nx2, :] resized_im = cv2.resize(cropped_im, (size, size), interpolation=cv2.INTER_LINEAR) cur_sample_num += 1 if force_accept == 1: ny1 = max(0, y1) ny2 = int(min(height, y1 + h)) nx1 = max(0, x1) nx2 = int(min(width, x1 + w)) w = nx2 - nx1 h = ny2 - ny1 #print ny1,ny2,nx1,nx2 cropped_im = img[ny1:ny2, nx1:nx2, :] resized_im = cv2.resize(cropped_im, (size, size), interpolation=cv2.INTER_LINEAR) offset_x = (landmark_x - nx1 + 0.5) / float(cur_size) offset_y = (landmark_y - ny1 + 0.5) / float(cur_size) if config.landmark106_migu_random_flip: flip_val = npr.randint(0, 2) if flip_val == 1: tmp_arr1_x, tmp_arr1_y = offset_x[0:33], offset_y[0:33] offset_x[0:33], offset_y[ 0:33] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y = offset_x[33:43], offset_y[33:43] offset_x[33:43], offset_y[ 33:43] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y = offset_x[43:47], offset_y[43:47] offset_x[43:47], offset_y[43:47] = 1 - tmp_arr1_x, tmp_arr1_y tmp_arr1_x, tmp_arr1_y = offset_x[47:52], offset_y[47:52] offset_x[47:52], offset_y[ 47:52] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y, tmp_arr2_x, tmp_arr2_y = offset_x[ 52:56], offset_y[52:56], offset_x[58:62], offset_y[58:62] offset_x[52:56], offset_y[52:56], offset_x[58:62], offset_y[ 58: 62] = 1 - tmp_arr2_x[:: -1], tmp_arr2_y[:: -1], 1 - tmp_arr1_x[:: -1], tmp_arr1_y[:: -1] tmp_arr1_x, tmp_arr1_y, tmp_arr2_x, tmp_arr2_y = offset_x[ 56:58], offset_y[56:58], offset_x[62:64], offset_y[62:64] offset_x[56:58], offset_y[56:58], offset_x[62:64], offset_y[ 62: 64] = 1 - tmp_arr2_x[:: -1], tmp_arr2_y[:: -1], 1 - tmp_arr1_x[:: -1], tmp_arr1_y[:: -1] tmp_arr1_x, tmp_arr1_y = offset_x[64:72], offset_y[64:72] offset_x[64:72], offset_y[ 64:72] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y, tmp_arr2_x, tmp_arr2_y = offset_x[ 72:75], offset_y[72:75], offset_x[75:78], offset_y[75:78] offset_x[72:75], offset_y[72:75], offset_x[75:78], offset_y[ 75:78] = 1 - tmp_arr2_x, tmp_arr2_y, 1 - tmp_arr1_x, tmp_arr1_y offset_x[78], offset_y[78], offset_x[79], offset_y[ 79] = 1 - offset_x[79], offset_y[ 79], 1 - offset_x[78], offset_y[78] offset_x[80], offset_y[80], offset_x[81], offset_y[ 81] = 1 - offset_x[81], offset_y[ 81], 1 - offset_x[80], offset_y[80] offset_x[82], offset_y[82], offset_x[83], offset_y[ 83] = 1 - offset_x[83], offset_y[ 83], 1 - offset_x[82], offset_y[82] tmp_arr1_x, tmp_arr1_y = offset_x[84:91], offset_y[84:91] offset_x[84:91], offset_y[ 84:91] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y = offset_x[91:96], offset_y[91:96] offset_x[91:96], offset_y[ 91:96] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y = offset_x[96:101], offset_y[96:101] offset_x[96:101], offset_y[ 96:101] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y = offset_x[101:104], offset_y[101:104] offset_x[101:104], offset_y[ 101:104] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] offset_x[104], offset_y[104], offset_x[105], offset_y[ 105] = 1 - offset_x[105], offset_y[ 105], 1 - offset_x[104], offset_y[104] resized_im = resized_im[:, ::-1, :] if config.landmark106_migu_weighting: landmark[0:66:2] = offset_x[0:33] landmark[1:67:2] = offset_y[0:33] landmark[66:86:2] = offset_x[33:43] * 2 #eyebrow up landmark[67:87:2] = offset_y[33:43] * 2 #eyebrow up landmark[86:128:2] = offset_x[43:64] landmark[87:129:2] = offset_y[43:64] landmark[128:144:2] = offset_x[64:72] * 2 #eyebrow down landmark[129:145:2] = offset_y[64:72] * 2 #eyebrow down landmark[144:168:2] = offset_x[72:84] landmark[145:169:2] = offset_y[72:84] landmark[168:208:2] = offset_x[84:104] * 2 #lips landmark[169:209:2] = offset_y[84:104] * 2 #lips landmark[208:212:2] = offset_x[104:106] landmark[209:213:2] = offset_y[104:106] else: landmark[0::2] = offset_x landmark[1::2] = offset_y if config.enable_blur: #kernel_size = npr.randint(-5,4)*2+1 kernel_size = npr.randint(-5, 13) * 2 + 1 if kernel_size >= 3: blur_im = cv2.GaussianBlur(resized_im, (kernel_size, kernel_size), 0) resized_im = blur_im if config.enable_black_border: black_size = npr.randint(0, int(size * 0.5)) if npr.randint(0, 2) == 0: resized_im[:, 0:black_size, :] = 128 else: resized_im[:, (size - black_size):size, :] = 128 return resized_im, landmark
def gen_landmark_for_one_image(size, idx, img, landmark_save_dir, landmarks, base_num = 1): landmark_names = list() landmark_num = 0 width = img.shape[1] height = img.shape[0] landmark_x = landmarks[0::2] landmark_y = landmarks[1::2] max_x = max(landmark_x) min_x = min(landmark_x) max_y = max(landmark_y) min_y = min(landmark_y) cx = 0.5*(max_x+min_x) cy = 0.5*(max_y+min_y) w = max_x-min_x h = max_y-min_y bbox_size = max(h,w) x1 = int(cx - bbox_size*0.5) y1 = int(cy - bbox_size*0.5) w = bbox_size h = bbox_size init_rot = 0 if config.landmark106_migu_init_rot: eye_cx = 0.25*(landmark_x[52]+landmark_x[55]+landmark_x[58]+landmark_x[61]) eye_cy = 0.25*(landmark_y[52]+landmark_y[55]+landmark_y[58]+landmark_y[61]) mouth_cx = 0.25*(landmark_x[84]+landmark_x[96]+landmark_x[100]+landmark_x[90]) mouth_cy = 0.25*(landmark_y[84]+landmark_y[96]+landmark_y[100]+landmark_y[90]) dir_x = mouth_cx - eye_cx dir_y = mouth_cy - eye_cy init_rot = 90 - math.atan2(dir_y, dir_x)/math.pi*180 cur_angle = npr.randint(int(config.min_rot_angle - init_rot),int(config.max_rot_angle - init_rot)+1) try_num = 0 force_accept = 0 while landmark_num < base_num: try_num += 1 if try_num > base_num*1000: force_accept = 1 break rot_landmark_x,rot_landmark_y = image_processing.rotateLandmark106(cx,cy,landmark_x,landmark_y, cur_angle,1) rot_max_x = max(rot_landmark_x) rot_min_x = min(rot_landmark_x) rot_max_y = max(rot_landmark_y) rot_min_y = min(rot_landmark_y) rot_cx = 0.5*(rot_max_x+rot_min_x) rot_cy = 0.5*(rot_max_y+rot_min_y) rot_w = rot_max_x-rot_min_x rot_h = rot_max_y-rot_min_y rot_bbox_size = max(rot_h,rot_w) rot_x1 = int(rot_cx - rot_bbox_size*0.5) rot_y1 = int(rot_cy - rot_bbox_size*0.5) rot_w = rot_bbox_size rot_h = rot_bbox_size #cur_size = int(npr.randint(10, 21)*0.1*rot_bbox_size) #cur_size = int(npr.randint(10, 16)*0.1*rot_bbox_size) cur_size = int(npr.randint(110, 126)*0.01*rot_bbox_size) #up_border_size = int(-cur_size*0.15) #down_border_size = int(-cur_size*0.15) #left_border_size = int(-cur_size*0.15) #right_border_size = int(-cur_size*0.15) up_border_size = int(cur_size*0.05) down_border_size = int(cur_size*0.05) left_border_size = int(cur_size*0.05) right_border_size = int(cur_size*0.05) # delta here is the offset of box center #delta_x = npr.randint(-int(rot_w * 0.35), int(rot_w * 0.35)+1) #delta_y = npr.randint(-int(rot_h * 0.35), int(rot_h * 0.35)+1) #delta_x = npr.randint(-int(rot_w * 0.20), int(rot_w * 0.20)+1) #delta_y = npr.randint(-int(rot_h * 0.20), int(rot_h * 0.20)+1) delta_x = npr.randint(-int(rot_w * 0.02), int(rot_w * 0.02)+1) delta_y = npr.randint(-int(rot_h * 0.02), int(rot_h * 0.02)+1) nx1 = int(max(x1 + rot_w / 2 + delta_x - cur_size / 2, 0)) ny1 = int(max(y1 + rot_h / 2 + delta_y - cur_size / 2, 0)) nx2 = nx1 + cur_size ny2 = ny1 + cur_size if nx2 > width or ny2 > height: continue ignore = 0 max_rot_landmark_x = max(rot_landmark_x) min_rot_landmark_x = min(rot_landmark_x) max_rot_landmark_y = max(rot_landmark_y) min_rot_landmark_y = min(rot_landmark_y) if min_rot_landmark_x < nx1+left_border_size or max_rot_landmark_x >= nx1 + cur_size-right_border_size: ignore = 1 if min_rot_landmark_y < ny1+up_border_size or max_rot_landmark_y >= ny1 + cur_size-down_border_size: ignore = 1 if ignore == 1: continue landmark_x_dis = max_rot_landmark_x - min_rot_landmark_x landmark_y_dis = max_rot_landmark_y - min_rot_landmark_y tmp_dis = landmark_x_dis*landmark_x_dis + landmark_y_dis*landmark_y_dis #if tmp_dis < 0.64*cur_size*cur_size: if tmp_dis < 1.00*cur_size*cur_size: continue offset_x = (rot_landmark_x - nx1+0.5)/float(cur_size) offset_y = (rot_landmark_y - ny1+0.5)/float(cur_size) rot_img,_,_ = image_processing.rotateWithLandmark106(img,cx,cy,landmark_x,landmark_y, cur_angle,1) cropped_im = rot_img[ny1 : ny2, nx1 : nx2, :] resized_im = cv2.resize(cropped_im, (size, size), interpolation=cv2.INTER_LINEAR) if config.landmark106_migu_random_flip: flip_val = npr.randint(0,2) if flip_val == 1: tmp_arr1_x, tmp_arr1_y = offset_x[0:33], offset_y[0:33] offset_x[0:33], offset_y[0:33] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y = offset_x[33:43], offset_y[33:43] offset_x[33:43], offset_y[33:43] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y = offset_x[43:47], offset_y[43:47] offset_x[43:47], offset_y[43:47] = 1 - tmp_arr1_x, tmp_arr1_y tmp_arr1_x, tmp_arr1_y = offset_x[47:52], offset_y[47:52] offset_x[47:52], offset_y[47:52] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y, tmp_arr2_x, tmp_arr2_y = offset_x[52:56], offset_y[52:56], offset_x[58:62], offset_y[58:62] offset_x[52:56], offset_y[52:56], offset_x[58:62], offset_y[58:62] = 1 - tmp_arr2_x[::-1], tmp_arr2_y[::-1], 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y, tmp_arr2_x, tmp_arr2_y = offset_x[56:58], offset_y[56:58], offset_x[62:64], offset_y[62:64] offset_x[56:58], offset_y[56:58], offset_x[62:64], offset_y[62:64] = 1 - tmp_arr2_x[::-1], tmp_arr2_y[::-1], 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y = offset_x[64:72], offset_y[64:72] offset_x[64:72], offset_y[64:72] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y, tmp_arr2_x, tmp_arr2_y = offset_x[72:75], offset_y[72:75], offset_x[75:78], offset_y[75:78] offset_x[72:75], offset_y[72:75], offset_x[75:78], offset_y[75:78] = 1 - tmp_arr2_x, tmp_arr2_y, 1 - tmp_arr1_x, tmp_arr1_y offset_x[78], offset_y[78], offset_x[79], offset_y[79] = 1 - offset_x[79], offset_y[79], 1 - offset_x[78], offset_y[78] offset_x[80], offset_y[80], offset_x[81], offset_y[81] = 1 - offset_x[81], offset_y[81], 1 - offset_x[80], offset_y[80] offset_x[82], offset_y[82], offset_x[83], offset_y[83] = 1 - offset_x[83], offset_y[83], 1 - offset_x[82], offset_y[82] tmp_arr1_x, tmp_arr1_y = offset_x[84:91], offset_y[84:91] offset_x[84:91], offset_y[84:91] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y = offset_x[91:96], offset_y[91:96] offset_x[91:96], offset_y[91:96] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y = offset_x[96:101], offset_y[96:101] offset_x[96:101], offset_y[96:101] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] tmp_arr1_x, tmp_arr1_y = offset_x[101:104], offset_y[101:104] offset_x[101:104], offset_y[101:104] = 1 - tmp_arr1_x[::-1], tmp_arr1_y[::-1] offset_x[104], offset_y[104], offset_x[105], offset_y[105] = 1 - offset_x[105], offset_y[105], 1 - offset_x[104], offset_y[104] resized_im = resized_im[:,::-1,:] if config.enable_black_border: black_size = npr.randint(0,int(size*0.5)) if npr.randint(0,2) == 0: resized_im[:,0:black_size,:] = 128 else: resized_im[:,(size-black_size):size,:] = 128 save_file = '%s/%d_%d.jpg'%(landmark_save_dir,idx,landmark_num) if cv2.imwrite(save_file, resized_im): line = '%s/%d_%d'%(landmark_save_dir,idx,landmark_num) for j in range(106): line = line + ' %.7f %.7f'%(offset_x[j],offset_y[j]) landmark_names.append(line) landmark_num += 1 return landmark_names