def visualize_dt_and_gt(images, y_true, y_pred, config): """Takes a batch of images and creates bounding box visualization on top Arguments: images {[type]} -- numpy tensor of images y_true {[type]} -- tensor of ground truth y_pred {[type]} -- tensor of predictions config {[type]} -- dict of various hyperparameters Returns: [type] -- dict of various hyperparameters """ img_with_boxes = [] all_filtered_boxes, all_filtered_classes, all_filtered_scores = filter_batch( y_pred, config) box_input = y_true[:, :, 1:5] labels = y_true[:, :, 9:] font = cv2.FONT_HERSHEY_SIMPLEX for i, img in enumerate(images): non_zero_boxes = box_input[i][box_input[i] > 0].reshape((-1, 4)) non_zero_labels = [] for k, coords in enumerate(box_input[i, :]): if np.sum(coords) > 0: for j, l in enumerate(labels[i, k]): if l == 1: non_zero_labels.append(j) for j, det_box in enumerate(all_filtered_boxes[i]): det_box = bbox_transform_single_box(det_box) cv2.rectangle(img, (det_box[0], det_box[1]), (det_box[2], det_box[3]), (0, 0, 255), 1) cv2.putText(img, config.CLASS_NAMES[all_filtered_classes[i][j]] + " " + str( all_filtered_scores[i][j]), (det_box[0], det_box[1]), font, 0.5, (0, 0, 255), 1, cv2.LINE_AA) for j, gt_box in enumerate(non_zero_boxes): gt_box = bbox_transform_single_box(gt_box) cv2.rectangle(img, (gt_box[0], gt_box[1]), (gt_box[2], gt_box[3]), (0, 255, 0), 1) cv2.putText(img, config.CLASS_NAMES[int(non_zero_labels[j])], (gt_box[0], gt_box[1]), font, 0.5, (0, 255, 0), 1, cv2.LINE_AA) img_with_boxes.append(img[:, :, [2, 1, 0]]) return img_with_boxes
def eval(img_file): #create config object config = load_dict(CONFIG) config.BATCH_SIZE = 1 #文件数量 config.FINAL_THRESHOLD = 0.5 #conf阈值 ''' #open files with images and ground truths files with full path names with open(imgs_list) as imgs: img_names = imgs.read().splitlines() imgs.close() ''' #hide the other gpus so tensorflow only uses this one os.environ['CUDA_VISIBLE_DEVICES'] = CUDA_VISIBLE_DEVICES #tf config and session cfg = tf.ConfigProto(allow_soft_placement=True) sess = tf.Session(config=cfg) K.set_session(sess) #instantiate model squeeze = SqueezeDet(config) squeeze.model.load_weights(checkpoint_dir) img = cv2.imread(img_file).astype(np.float32, copy=False) orig_h, orig_w, _ = [float(v) for v in img.shape] # scale image draw_img = img img = cv2.resize(img, (config.IMAGE_WIDTH, config.IMAGE_HEIGHT)) img = (img - np.mean(img)) / np.std(img) img = np.reshape(img, (1, config.IMAGE_HEIGHT, config.IMAGE_WIDTH, 3)) y_pred = squeeze.model.predict(img) #filter batch with nms all_filtered_boxes, all_filtered_classes, all_filtered_scores = filter_batch( y_pred, config) font = cv2.FONT_HERSHEY_SIMPLEX for j, det_box in enumerate(all_filtered_boxes[0]): #transform into xmin, ymin, xmax, ymax det_box = bbox_transform_single_box(det_box) print(all_filtered_scores[0][j]) #add rectangle and text cv2.rectangle(draw_img, (det_box[0], det_box[1]), (det_box[2], det_box[3]), (0, 0, 255), 1) cv2.putText(draw_img, 'head' + str(all_filtered_scores[0][j]), (det_box[0], det_box[1]), font, 0.5, (0, 255, 0), 1, cv2.LINE_AA) cv2.imwrite('restul.png', draw_img)
def visualize_dt_and_gt(images, y_true, y_pred, config): """Takes a batch of images and creates bounding box visualization on top Arguments: images {[type]} -- numpy tensor of images y_true {[type]} -- tensor of ground truth y_pred {[type]} -- tensor of predictions config {[type]} -- dict of various hyperparameters Returns: [type] -- dict of various hyperparameters """ #normalize for printing #images = (images - np.min(images, axis=(1,2,3), keepdims=1)) / np.max(images, axis=(1,2,3), keepdims=1) - np.min(images, axis=(1,2,3), keepdims=1) img_with_boxes = [] #filter batch with nms all_filtered_boxes, all_filtered_classes, all_filtered_scores = filter_batch( y_pred, config) #print(len(all_filtered_scores)) #print(len(all_filtered_scores[0])) #get gt boxes box_input = y_true[:, :, 1:5] #and gt labels labels = y_true[:, :, 9:] font = cv2.FONT_HERSHEY_SIMPLEX #iterate images for i, img in enumerate(images): #get predicted boxes non_zero_boxes = box_input[i][box_input[i] > 0].reshape((-1, 4)) non_zero_labels = [] img = cv2.resize(img, (1280, 1080)) #!!!!! #get the predicted labels for k, coords in enumerate(box_input[i, :]): if np.sum(coords) > 0: for j, l in enumerate(labels[i, k]): if l == 1: non_zero_labels.append(j) #iterate predicted boxes for j, det_box in enumerate(all_filtered_boxes[i]): #transform into xmin, ymin, xmax, ymax det_box = bbox_transform_single_box(det_box) x_scale = 1280 / config.IMAGE_WIDTH y_scale = 1080 / config.IMAGE_HEIGHT det_box[0], det_box[2] = int(det_box[0] * x_scale), int( det_box[2] * x_scale) det_box[1], det_box[3] = int(det_box[1] * y_scale), int( det_box[3] * y_scale) #add rectangle and text cv2.rectangle(img, (det_box[0], det_box[1]), (det_box[2], det_box[3]), (255, 10, 10), 2) cv2.putText( img, config.CLASS_NAMES[all_filtered_classes[i][j]] + " " + str(all_filtered_scores[i][j]), (det_box[0], det_box[1]), font, 1, (255, 10, 10), 2, cv2.LINE_AA) #iterate gt boxes for j, gt_box in enumerate(non_zero_boxes): #transform into xmin, ymin, xmax, ymax gt_box = bbox_transform_single_box(gt_box) x_scale = 1280 / config.IMAGE_WIDTH y_scale = 1080 / config.IMAGE_HEIGHT gt_box[0], gt_box[2] = int(gt_box[0] * x_scale), int(gt_box[2] * x_scale) gt_box[1], gt_box[3] = int(gt_box[1] * y_scale), int(gt_box[3] * y_scale) #add rectangle and text cv2.rectangle(img, (gt_box[0], gt_box[1]), (gt_box[2], gt_box[3]), (0, 255, 0), 2) cv2.putText(img, config.CLASS_NAMES[int(non_zero_labels[j])], (gt_box[0], gt_box[1]), font, 1, (0, 255, 0), 1, cv2.LINE_AA) #chagne to #img_with_boxes.append(img[:,:, [2,1,0]]) #rgb img_with_boxes.append(img[:, :, [0, 1, 2]]) #bgr return img_with_boxes
img_name = "sm1.jpg" # open img img = cv2.imread(img_name).astype(np.float32, copy=False) orig_h =img.shape[0] orig_w =img.shape[1] # subtract means img = (img - np.mean(img)) / np.std(img) img = cv2.resize(img, (cfg.IMAGE_WIDTH, cfg.IMAGE_HEIGHT)) img_for_pred = np.expand_dims(img, axis=0) print (img_for_pred.shape) y_pred = model.predict(img_for_pred) print(" predicted something") all_filtered_boxes, all_filtered_classes, all_filtered_scores = filter_batch(y_pred, cfg) font = cv2.FONT_HERSHEY_SIMPLEX img = cv2.imread(img_name) for j, det_box in enumerate(all_filtered_boxes[0]): print det_box """ # transform into xmin, ymin, xmax, ymax det_box = bbox_transform_single_box(det_box) x_scale = orig_w / cfg.IMAGE_WIDTH y_scale = orig_h / cfg.IMAGE_HEIGHT det_box[0], det_box[2] = int(det_box[0] * x_scale), int(det_box[2] * x_scale) det_box[1], det_box[3] = int(det_box[1] * y_scale), int(det_box[3] * y_scale) # add rectangle and text