def rnet_boxes(img, rnet, bounding_boxes, thresholds=THRESHOLDS, nms_thresholds=NMS_THRESHOLDS, show_boxes=True): rnet.eval() img_boxes = get_image_boxes(bounding_boxes, img, size=24) img_boxes = torch.FloatTensor(img_boxes) img_boxes = img_boxes.to( torch.device('cuda' if torch.cuda.is_available() else 'cpu')) output = rnet(img_boxes) probs = output[0].data.cpu().numpy() # shape [n_boxes, 1] offsets = output[1].data.cpu().numpy() # shape [n_boxes, 4] keep = np.where(probs[:, 0] > thresholds[1])[0] bounding_boxes = bounding_boxes[keep] bounding_boxes[:, 4] = probs[keep, 0].reshape((-1, )) offsets = offsets[keep] keep = nms(bounding_boxes, nms_thresholds[1]) bounding_boxes = bounding_boxes[keep] bounding_boxes = calibrate_box(bounding_boxes, offsets[keep]) bounding_boxes = convert_to_square(bounding_boxes) bounding_boxes[:, 0:4] = np.round(bounding_boxes[:, 0:4]) if show_boxes: show_bboxes(img, bounding_boxes, []).show() return bounding_boxes
def onet_boxes(img, onet, bounding_boxes, thresholds=THRESHOLDS, nms_thresholds=NMS_THRESHOLDS, show_boxes=True): onet.eval() img_boxes = get_image_boxes(bounding_boxes, img, size=48) if len(img_boxes) == 0: return [], [] img_boxes = torch.FloatTensor(img_boxes) img_boxes = img_boxes.to( torch.device('cuda' if torch.cuda.is_available() else 'cpu')) output = onet(img_boxes) probs = output[0].data.cpu().numpy() # shape [n_boxes, 1] offsets = output[1].data.cpu().numpy() # shape [n_boxes, 4] landmarks = output[2].data.cpu().numpy() # shape [n_boxes, 10] keep = np.where(probs[:, 0] > thresholds[2])[0] bounding_boxes = bounding_boxes[keep] # 用更大模型的置信度对原置信度进行更新 bounding_boxes[:, 4] = probs[keep, 0].reshape((-1, )) offsets = offsets[keep] landmarks = landmarks[keep] # compute landmark points width = bounding_boxes[:, 2] - bounding_boxes[:, 0] + 1.0 height = bounding_boxes[:, 3] - bounding_boxes[:, 1] + 1.0 xmin, ymin = bounding_boxes[:, 0], bounding_boxes[:, 1] # print('width:{},\nheight:{},\nxmin:{},\nymin:{}\n'.format(width, height, xmin, ymin)) # landmark[,前5个为x,后5个为y] # 在左上角坐标的基础上,通过 w,h 确定脸各关键点的坐标。 landmarks_pixel = np.zeros(landmarks.shape) landmarks_pixel[:, 0:5] = ( np.expand_dims(xmin, 1) + np.expand_dims(width, 1) * landmarks[:, 0::2]).copy() landmarks_pixel[:, 5:10] = ( np.expand_dims(ymin, 1) + np.expand_dims(height, 1) * landmarks[:, 1::2]).copy() # for i in landmarks:print(i) bounding_boxes = calibrate_box(bounding_boxes, offsets) keep = nms(bounding_boxes, nms_thresholds[2], mode='min') bounding_boxes = bounding_boxes[keep] landmarks_pixel = landmarks_pixel[keep] if show_boxes: show_bboxes(img, bounding_boxes, landmarks_pixel).show() return bounding_boxes, landmarks_pixel
os.path.join('..', 'open_images_', 'validation-images-with-rotation.csv')) meta.set_index('ImageID', inplace=True) print('data loaded') for index, row in meta.iterrows(): if index not in id2url: continue #if not np.isnan(row.Rotation): if row.Rotation != 270: continue ''' image_url = id2url[index] try: image = get_image(image_url, rotate=id2rot[index]) except: print('error downloading: ' + image_url) continue ''' image = get_image_from_s3(index) result = get_image_boxes(objects, index) print(row.Rotation) print(result) image_with_boxes = draw_boxes(image, result["detection_boxes"], result["detection_class_names"]) display_image(image_with_boxes)