def _crop(image, boxes, labels, img_shape): height, width, _ = image.shape pad_image_flag = True for _ in range(250): if random.uniform(0, 1) <= 0.2: scale = 1 else: scale = random.uniform(0.3, 1.) short_side = min(width, height) w = int(scale * short_side) h = w if width == w: l = 0 else: l = random.randrange(width - w) if height == h: t = 0 else: t = random.randrange(height - h) roi = np.array((l, t, l + w, t + h)) # area_boxes_U_roi / area_boxes value = matrix_iof(boxes, roi[np.newaxis]) flag = (value >= 1) if not flag.any(): continue centers = (boxes[:, :2] + boxes[:, 2:]) / 2 mask_a = np.logical_and(roi[:2] < centers, centers < roi[2:]).all(axis=1) boxes_t = boxes[mask_a].copy() labels_t = labels[mask_a].copy() if boxes_t.shape[0] == 0: continue image_t = image[roi[1]:roi[3], roi[0]:roi[2]] boxes_t[:, :2] = np.maximum(boxes_t[:, :2], roi[:2]) boxes_t[:, :2] -= roi[:2] boxes_t[:, 2:] = np.minimum(boxes_t[:, 2:], roi[2:]) boxes_t[:, 2:] -= roi[:2] # boxes_t[:, 2:] -= boxes_t[:, :2] b_w_t = (boxes_t[:, 2] - boxes_t[:, 0] + 1) / w * img_shape b_h_t = (boxes_t[:, 3] - boxes_t[:, 1] + 1) / h * img_shape mask_b = np.minimum(b_w_t, b_h_t) > 16.0 boxes_t = boxes_t[mask_b] labels_t = labels_t[mask_b] if boxes_t.shape[0] == 0: continue pad_image_flag = False # cv2.imwrite('crop.jpg', image_t) # print('there are %01d faces in crop' % len(boxes_t)) return image_t, boxes_t, labels_t, pad_image_flag return image, boxes, labels, pad_image_flag
def _crop(image, boxes, labels, img_dim): height, width, _ = image.shape pad_image_flag = True for _ in range(250): if random.uniform(0, 1) <= 0.2: scale = 1 else: scale = random.uniform(0.3, 1.) short_side = min(width, height) w = int(scale * short_side) h = w if width == w: l = 0 else: l = random.randrange(width - w) if height == h: t = 0 else: t = random.randrange(height - h) roi = np.array((l, t, l + w, t + h)) value = matrix_iof(boxes, roi[np.newaxis]) flag = (value >= 1) if not flag.any(): continue centers = (boxes[:, :2] + boxes[:, 2:]) / 2 mask_a = np.logical_and(roi[:2] < centers, centers < roi[2:]).all(axis=1) boxes_t = boxes[mask_a].copy() labels_t = labels[mask_a].copy() if boxes_t.shape[0] == 0: continue image_t = image[roi[1]:roi[3], roi[0]:roi[2]] boxes_t[:, :2] = np.maximum(boxes_t[:, :2], roi[:2]) boxes_t[:, :2] -= roi[:2] boxes_t[:, 2:] = np.minimum(boxes_t[:, 2:], roi[2:]) boxes_t[:, 2:] -= roi[:2] # 这里不再对小于16 的人脸进行过滤 # make sure that the cropped image contains at least one face > 16 pixel at training image scale # b_w_t = (boxes_t[:, 2] - boxes_t[:, 0] + 1) / w * img_dim # b_h_t = (boxes_t[:, 3] - boxes_t[:, 1] + 1) / h * img_dim # mask_b = np.minimum(b_w_t, b_h_t) > 16.0 # boxes_t = boxes_t[mask_b] # labels_t = labels_t[mask_b] if boxes_t.shape[0] == 0: continue pad_image_flag = False return image_t, boxes_t, labels_t, pad_image_flag return image, boxes, labels, pad_image_flag
def _crop(image, boxes, labels, img_dim, rgb_means): height, width, _ = image.shape for _ in range(250): if random.uniform(0, 1) <= 0.2: scale = 1 else: scale = random.uniform(0.3, 1.) short_side = min(width, height) w = int(scale * short_side) h = w if width == w: l = 0 else: l = random.randrange(width - w) if height == h: t = 0 else: t = random.randrange(height - h) roi = np.array((l, t, l + w, t + h)) value = matrix_iof(boxes, roi[np.newaxis]) flag = (value >= 1) if not flag.any(): continue centers = (boxes[:, :2] + boxes[:, 2:]) / 2 mask_a = np.logical_and(roi[:2] < centers, centers < roi[2:]).all(axis=1) boxes_t = boxes[mask_a].copy() labels_t = labels[mask_a].copy() # ignore tiny faces b_w_t = (boxes_t[:, 2] - boxes_t[:, 0] + 1) / w * img_dim b_h_t = (boxes_t[:, 3] - boxes_t[:, 1] + 1) / h * img_dim mask_b = np.minimum(b_w_t, b_h_t) > 16.0 boxes_t = boxes_t[mask_b] labels_t = labels_t[mask_b].copy() if boxes_t.shape[0] == 0: continue image_t = image[roi[1]:roi[3], roi[0]:roi[2]] boxes_t[:, :2] = np.maximum(boxes_t[:, :2], roi[:2]) boxes_t[:, :2] -= roi[:2] boxes_t[:, 2:] = np.minimum(boxes_t[:, 2:], roi[2:]) boxes_t[:, 2:] -= roi[:2] return image_t, boxes_t, labels_t long_side = max(width, height) image_t = np.empty((long_side, long_side, 3), dtype=image.dtype) image_t[:, :] = rgb_means image_t[0:0 + height, 0:0 + width] = image return image_t, boxes, labels
def _crop(image, boxes, labels, landm, img_dim): height, width, _ = image.shape pad_image_flag = True for _ in range(250): """ if random.uniform(0, 1) <= 0.2: scale = 1.0 else: scale = random.uniform(0.3, 1.0) """ PRE_SCALES = [0.3, 0.45, 0.6, 0.8, 1.0] scale = random.choice(PRE_SCALES) short_side = min(width, height) w = int(scale * short_side) h = w if width == w: l = 0 else: l = random.randrange(width - w) if height == h: t = 0 else: t = random.randrange(height - h) roi = np.array((l, t, l + w, t + h)) value = matrix_iof(boxes, roi[np.newaxis]) flag = (value >= 1) if not flag.any(): continue centers = (boxes[:, :2] + boxes[:, 2:]) / 2 mask_a = np.logical_and(roi[:2] < centers, centers < roi[2:]).all(axis=1) boxes_t = boxes[mask_a].copy() labels_t = labels[mask_a].copy() landms_t = landm[mask_a].copy() landms_t = landms_t.reshape([-1, 5, 2]) if boxes_t.shape[0] == 0: continue image_t = image[roi[1]:roi[3], roi[0]:roi[2]] boxes_t[:, :2] = np.maximum(boxes_t[:, :2], roi[:2]) boxes_t[:, :2] -= roi[:2] boxes_t[:, 2:] = np.minimum(boxes_t[:, 2:], roi[2:]) boxes_t[:, 2:] -= roi[:2] # landm landms_t[:, :, :2] = landms_t[:, :, :2] - roi[:2] landms_t[:, :, :2] = np.maximum(landms_t[:, :, :2], np.array([0, 0])) landms_t[:, :, :2] = np.minimum(landms_t[:, :, :2], roi[2:] - roi[:2]) landms_t = landms_t.reshape([-1, 10]) # make sure that the cropped image contains at least one face > 16 pixel at training image scale b_w_t = (boxes_t[:, 2] - boxes_t[:, 0] + 1) / w * img_dim b_h_t = (boxes_t[:, 3] - boxes_t[:, 1] + 1) / h * img_dim mask_b = np.minimum(b_w_t, b_h_t) > 0.0 boxes_t = boxes_t[mask_b] labels_t = labels_t[mask_b] landms_t = landms_t[mask_b] if boxes_t.shape[0] == 0: continue pad_image_flag = False return image_t, boxes_t, labels_t, landms_t, pad_image_flag return image, boxes, labels, landm, pad_image_flag
def __call__(self, img, target): """ Args: img (PIL Image): Image to be cropped. Returns: """ img_ = np.array(img) height, width, _ = img_.shape pad_image_flag = True boxes = target['boxes'] labels = target['labels'] landmarks = target['landmarks'] if DEBUG_SHOW: image_ = img_.copy() for i in range(len(boxes)): box = boxes[i].astype(np.int) cv2.rectangle(image_, (box[0], box[1]), (box[2], box[3]), (255, 255, 0)) cv2.imshow('srcimg', image_) cv2.waitKey() for _ in range(250): """ if random.uniform(0, 1) <= 0.2: scale = 1.0 else: scale = random.uniform(0.3, 1.0) """ PRE_SCALES = [0.3, 0.45, 0.6, 0.8, 1.0] scale = random.choice(PRE_SCALES) short_side = min(width, height) w = int(scale * short_side) h = w if width == w: l = 0 else: l = random.randrange(width - w) if height == h: t = 0 else: t = random.randrange(height - h) roi = np.array((l, t, l + w, t + h)) value = matrix_iof(boxes, roi[np.newaxis]) flag = (value >= 1) if not flag.any(): continue centers = (boxes[:, :2] + boxes[:, 2:]) / 2 mask_a = np.logical_and(roi[:2] < centers, centers < roi[2:]).all(axis=1) boxes_t = boxes[mask_a].copy() labels_t = labels[mask_a].copy() landms_t = None if landmarks: landms_t = landmarks[mask_a].copy() landms_t = landms_t.reshape([-1, 5, 2]) if boxes_t.shape[0] == 0: continue image_t = img_[roi[1]:roi[3], roi[0]:roi[2]] boxes_t[:, :2] = np.maximum(boxes_t[:, :2], roi[:2]) boxes_t[:, :2] -= roi[:2] boxes_t[:, 2:] = np.minimum(boxes_t[:, 2:], roi[2:]) boxes_t[:, 2:] -= roi[:2] # landmarks if landmarks: landms_t[:, :, :2] = landms_t[:, :, :2] - roi[:2] landms_t[:, :, :2] = np.maximum(landms_t[:, :, :2], np.array([0, 0])) landms_t[:, :, :2] = np.minimum(landms_t[:, :, :2], roi[2:] - roi[:2]) landms_t = landms_t.reshape([-1, 10]) # make sure that the cropped image contains at least one face > 16 pixel at training image scale b_w_t = (boxes_t[:, 2] - boxes_t[:, 0] + 1) / w * self.size[1] b_h_t = (boxes_t[:, 3] - boxes_t[:, 1] + 1) / h * self.size[0] mask_b = np.minimum(b_w_t, b_h_t) > 0.0 boxes_t = boxes_t[mask_b] labels_t = labels_t[mask_b] if landmarks: landms_t = landms_t[mask_b] if boxes_t.shape[0] == 0: continue pad_image_flag = False img = Image.fromarray(image_t) target['boxes'] = boxes_t target['labels'] = labels_t target['landmarks'] = landms_t return img, target return img, target
def _crop(image, boxes, labels, img_dim): """Randmoly crops image and makes it square :param image - image as a np.array boxes - ground truth boxes for a image labels - class labels ('1' by default) img_dim - necessary dim :return: cropped image, cropped gt boxes, boxes' labels, flag shows whether image need padding or not """ height, width, _ = image.shape pad_image_flag = True for _ in range(250): if random.uniform(0, 1) <= 0.2: scale = 1 else: scale = random.uniform(0.3, 1.) short_side = min(width, height) w = int(scale * short_side) h = w if width == w: l = 0 else: l = random.randrange(width - w) if height == h: t = 0 else: t = random.randrange(height - h) roi = np.array((l, t, l + w, t + h)) value = matrix_iof(boxes, roi[np.newaxis]) flag = (value >= 1) if not flag.any(): continue centers = (boxes[:, :2] + boxes[:, 2:]) / 2 mask_a = np.logical_and(roi[:2] < centers, centers < roi[2:]).all(axis=1) boxes_t = boxes[mask_a].copy() labels_t = labels[mask_a].copy() if boxes_t.shape[0] == 0: continue image_t = image[roi[1]:roi[3], roi[0]:roi[2]] boxes_t[:, :2] = np.maximum(boxes_t[:, :2], roi[:2]) boxes_t[:, :2] -= roi[:2] boxes_t[:, 2:] = np.minimum(boxes_t[:, 2:], roi[2:]) boxes_t[:, 2:] -= roi[:2] # make sure that the cropped image contains at least one face > 16 pixel at training image scale b_w_t = (boxes_t[:, 2] - boxes_t[:, 0] + 1) / w * img_dim b_h_t = (boxes_t[:, 3] - boxes_t[:, 1] + 1) / h * img_dim mask_b = np.minimum(b_w_t, b_h_t) > 16.0 boxes_t = boxes_t[mask_b] labels_t = labels_t[mask_b] if boxes_t.shape[0] == 0: continue pad_image_flag = False return image_t, boxes_t, labels_t, pad_image_flag return image, boxes, labels, pad_image_flag
def _crop_(image, boxes, coords, labels, img_dim): # bboxes, [n, 4],x1, y1, x2, y2 height, width, _ = image.shape pad_image_flag = True for _ in range(250): if random.uniform(0, 1) <= 0.2: scale = 1 else: scale = random.uniform(0.3, 1.) short_side = min(width, height) # ��scale��С��С�ߣ��������� w = int(scale * short_side) h = w if width == w: l = 0 else: l = random.randrange(width - w) # ��ʣ���������ѡ��ƽ�ƾ��� if height == h: t = 0 else: t = random.randrange(height - h) # �߶����� roi = np.array((l, t, l + w, t + h)) # ��ȡƽ�ƺ��roi:(l, t),(��) roi_coords = np.array((l, t, l+w, t, l+int(w/2), t+int(h/2), l, t+h, l+w, t+h)) value = matrix_iof(boxes, roi[np.newaxis]) flag = (value >= 1) # crop box contain max(bbox)? if not flag.any(): continue # print("roi:", roi, "bbox", boxes) centers = (boxes[:, :2] + boxes[:, 2:]) / 2 # ��ʵbbox������ mask_a = np.logical_and(roi[:2] < centers, centers < roi[2:]).all(axis=1) # all roi containe bbox center, is bool boxes_t = boxes[mask_a].copy() labels_t = labels[mask_a].copy() # its correspond box label coords_t = coords[mask_a].copy() # ѡȡroi����bbox���ĵ�bbox, ����Ӧ��label, coord # print("mask_a",mask_a,"boxes_t", boxes_t, "\n","labels_t", labels_t) if boxes_t.shape[0] == 0: # means no box in roi continue image_t = image[roi[1]:roi[3], roi[0]:roi[2]] # img is h, w # print("before+++bbox", boxes_t, "====roi", roi, "||||roi_coords", roi_coords) boxes_t[:, :2] = np.maximum(boxes_t[:, :2], roi[:2]) # return bigger, means inner coordinate boxes_t[:, :2] -= roi[:2] # bbox ��roi����Ծ��뱣��һ��(����img�ѱ���ȡ) # coords[:, ] ????????????????????????? boxes_t[:, 2:] = np.minimum(boxes_t[:, 2:], roi[2:]) # return smaller, means inner coordinate boxes_t[:, 2:] -= roi[:2] # ֻ�����ƶ�!!!!!! # print("after+++++bbox", boxes_t) # print("relative value",boxes_t) # coords[:, ] ????????????????????????? # print("before+++coords:", coords ) coords_t -= np.tile(roi[:2], 5) # print("after+++++coords",coords_t) ## coords """ coords_t[:, :2] = np.maximum(coords_t[:, :2], roi_coords[:2]) coords_t[:,:2] -= roi_coords[:2] coords_t[:, 2:4] = np.maximum(coords_t[:, 2:4], roi_coords[2:4]) coords_t[:,:2] -= roi_coords[2:4] ## coords center ### left_rigt_center = coords_t[:, 4] - roi_coords[4] #w or h for flag in left_rigt_center: if flag: coords_t[:, 4:6] = np.maximum(coords_t[:, 4:6], roi_coords[4:6]) coords_t[:,4:6] -= roi_coords[4:6] else: coords_t[:, 4:6] = np.minimum(coords_t[:, 4:6], roi_coords[4:6]) coords_t[:,4:6] -= roi_coords[4:6] ## coords center ### coords_t[:, 6:8] = np.minimum(coords_t[:, 6:8], roi_coords[6:8]) coords_t[:,6:8] -= roi_coords[6:8] coords_t[:, 8:] = np.minimum(coords_t[:, 8:], roi_coords[8:]) coords_t[:,8:] -= roi_coords[8:] """ # coords # make sure that the cropped image contains at least one face > 16 pixel at training image scale b_w_t = (boxes_t[:, 2] - boxes_t[:, 0] + 1) / w * img_dim # 1024=img_dim scale b_h_t = (boxes_t[:, 3] - boxes_t[:, 1] + 1) / h * img_dim mask_b = np.minimum(b_w_t, b_h_t) > 16.0 # why in img_dim?? boxes_t = boxes_t[mask_b] labels_t = labels_t[mask_b] coords_t = coords_t[mask_b] if boxes_t.shape[0] == 0: # no box satisfy above condition continue pad_image_flag = False return image_t, boxes_t, coords_t, labels_t, pad_image_flag return image, boxes, coords, labels, pad_image_flag